在这个程序中,我在CheckBoxList中动态添加CheckBox(通过从文本框中获取输入),CheckBoxList本身是动态创建的。
现在,当我添加复选框并将其删除时,一切正常。当我再次尝试添加复选框时,它会添加到ArrayList以及CheckboxList中,但不会显示在页面上。
从过去2天开始研究,但无法弄清楚。请帮助!!!
谢谢!
<!--chkboxlist.aspx-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtchkboxname" runat="server" AutoPostBack="true"></asp:TextBox>
</div>
</form>
</body>
</html>
//chkboxlist.aspx.cs
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class chkboxlist : System.Web.UI.Page
{
static ArrayList arlst = new ArrayList();
CheckBoxList chklst = new CheckBoxList();
protected void Page_Load(object sender, EventArgs e)
{
if (txtchkboxname.Text != "")
{
arlst.Add(txtchkboxname.Text);
txtchkboxname.Text = "";
}
chklst.DataSource = arlst;
chklst.DataBind();
chklst.AutoPostBack = true;
chklst.SelectedIndexChanged += new EventHandler(this.chklst_OnSelectedIndexChanged);
form1.Controls.Add(chklst);
}
protected void chklst_OnSelectedIndexChanged(object sender, EventArgs e)
{
arlst.RemoveAt(chklst.SelectedIndex);
chklst.DataSource = arlst;
chklst.DataBind();
}
}