开发者

Append empty text box Rows to a grid

开发者 https://www.devze.com 2023-03-13 09:56 出处:网络
I have a grid with two columns first column with check box and second column with text box. I have a add and save button down the grid.Can you please tell how to obtain if i click add button i need to

I have a grid with two columns first column with check box and second column with text box. I have a add and save button down the grid.Can you please tell how to obtain if i click add button i need to appen开发者_开发百科d one more row to the grid with empty text box and check box so that i can type and click save

need to do possibly without java script


If I am getting you correctly you want something like below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grdDemo" runat="server" AutoGenerateColumns="False" EnableModelValidation="True">
            <Columns>
                <asp:TemplateField HeaderText="CheckBox">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("IsCheckBox") %>' Enabled="false" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="TextBox">
                    <ItemTemplate>
                        <asp:TextBox ID="Label1" runat="server" Text='<%# Bind("IsTextBox") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" />
    </form>
</body>
</html>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grdDemo.DataSource = new Demo().GetData();
            grdDemo.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var list = new Demo().GetData();
        list.Add(new Demo() {IsCheckBox = false, IsTextBox = ""});
        Cache["list"] = list;
        grdDemo.DataSource = list;
        grdDemo.DataBind();
    }
}

public class Demo
{

    public bool IsCheckBox { get; set; }
    public string IsTextBox { get; set; }

    public List<Demo> GetData()
    {
        if (HttpContext.Current.Cache["list"] == null)
        {
            List<Demo> list = new List<Demo>()
                   {
                       new Demo(){IsCheckBox=true,IsTextBox = "text1"},
                       new Demo(){IsCheckBox=false,IsTextBox = "text2"},
                   };

            return list;
        }
        return (List<Demo>)HttpContext.Current.Cache["list"];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消