Asp.net 将空文本框行附加到网格
我有一个两列的网格,第一列带有复选框,第二列带有文本框。我在网格下面有一个添加和保存按钮。您能告诉我如何获取吗?如果我单击“添加”按钮,我需要在网格中添加一行空文本框和复选框,以便我可以键入并单击“保存”Asp.net 将空文本框行附加到网格,asp.net,Asp.net,我有一个两列的网格,第一列带有复选框,第二列带有文本框。我在网格下面有一个添加和保存按钮。您能告诉我如何获取吗?如果我单击“添加”按钮,我需要在网格中添加一行空文本框和复选框,以便我可以键入并单击“保存” 需要在没有java脚本的情况下执行如果我正确地理解了您的意思,您需要下面这样的内容 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %> &
需要在没有java脚本的情况下执行如果我正确地理解了您的意思,您需要下面这样的内容
<%@ 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"];
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.Caching;
公共部分类Default5:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
grdDemo.DataSource=new Demo().GetData();
grdDemo.DataBind();
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
var list=new Demo().GetData();
添加(new Demo(){IsCheckBox=false,IsTextBox=”“});
缓存[“列表”]=列表;
grdDemo.DataSource=列表;
grdDemo.DataBind();
}
}
公开课演示
{
公共bool IsCheckBox{get;set;}
公共字符串IsTextBox{get;set;}
公共列表GetData()
{
if(HttpContext.Current.Cache[“list”]==null)
{
列表=新列表()
{
new Demo(){IsCheckBox=true,IsTextBox=“text1”},
new Demo(){IsCheckBox=false,IsTextBox=“text2”},
};
退货清单;
}
返回(列表)HttpContext.Current.Cache[“List”];
}
}
感谢您的回复。只需将其替换为“添加”,而不是上面的“保存”按钮。单击后,只需在该行的网格中再添加一行,我需要添加值,然后单击“保存”。。你能帮帮我吗。