我应该创建一个事件触发Label
的TextBox
时间。CheckBoxList
SelectedIndexChanged
我尝试使用以下代码,但它给了我这个错误:
找到具有相同 ID 'mylab1' 的多个控件
protected void chkCar_SelectedIndexChanged(object sender, EventArgs e)
{
// Panel2.Controls.Clear();
string dat = DateTime.Parse(GridView1.SelectedRow.Cells[4].Text).ToString("dd-MMM-yyyy");
lblName.Text = "";
for (int i = 0; i < chkCar.Items.Count; i++)
{
int sum = 0;
if (chkCar.Items[i].Selected == true)
{
Panel tt = new Panel();
Panel2.Controls.Add(new LiteralControl("<br />"));
Label mylab = new Label();
mylab.ID = "mylab" + NextID1;
mylab.Text = "No of Persons in " + chkCar.Items[i].Value;
Panel2.Controls.Add(mylab);
ControlsList1.Add(mylab.ID);
TextBox mytext = new TextBox();
mytext.ID = "mytext" + NextID2;
mytext.Text = "";
Panel2.Controls.Add(mytext);
ControlsList2.Add(mytext.ID);
}
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
foreach (string lbID in ControlsList1)
{
Label lb = new Label();
lb.ID = lbID;
Panel2.Controls.Add(lb);
Panel2.Controls.Add(new LiteralControl("<br>"));
}
foreach (string txtID in ControlsList2)
{
TextBox tb = new TextBox();
tb.ID = txtID;
Panel2.Controls.Add(tb);
Panel2.Controls.Add(new LiteralControl("<br>"));
}
}
private List<string> ControlsList1 //labels
{
get
{
if (ViewState["controls"] == null)
{
ViewState["controls"] = new List<string>();
}
return (List<string>)ViewState["controls"];
}
}
private List<string> ControlsList2 //textboxes
{
get
{
if (ViewState["controls"] == null)
{
ViewState["controls"] = new List<string>();
}
return (List<string>)ViewState["controls"];
}
}
private int NextID1//label
{
get
{
return ControlsList1.Count + 1;
}
}
private int NextID2//textbox
{
get
{
return ControlsList2.Count + 1;
}
}
谁能帮助我知道我哪里出错了?提前致谢。