动态添加的按钮不是触发事件,并且正在丢失状态,尽管在事件中重新添加它pre_init但正在丢失状态

本文关键字:状态 事件 添加 新添加 init pre 按钮 动态 | 更新日期: 2023-09-27 18:37:27

我有一个绑定到数据库的下拉列表。在其索引更改时,有一个功能可以根据所选值在面板中添加一些按钮。

我正在page_init事件中读取这些按钮,但仍然得到null值,即与按钮绑定的事件永远不会触发。

这是我的代码,dropdownlist1是添加动态按钮的下拉列表。

protected void Page_Load(object sender, EventArgs e)
{
     if (!Page.IsPostBack)
     {
          colorgroupsTableAdapters.master_color_groupTableAdapter ta
              = new colorgroupsTableAdapters.master_color_groupTableAdapter();
          DataTable dt = ta.GetData();
          DropDownList1.DataSource = dt;
          DropDownList1.DataTextField = dt.Columns[1].ToString();
          DropDownList1.DataValueField = dt.Columns[0].ToString();
          DropDownList1.DataBind();
          DropDownList1.Items.Insert(0, new ListItem("Select One", "0"));
     }
}
protected void Page_Init(object sender, EventArgs e)
{
     if (Page.IsPostBack)
     {
          bindcolors();
     }
}
protected void DropDownList1_DataBound(object sender, EventArgs e)
{        
}
protected void DropDownList2_DataBound(object sender, EventArgs e)
{
     if (DropDownList1.SelectedIndex < 1)
     {
          DropDownList2.Items.Clear();
     }
     DropDownList2.Items.Insert(0, new ListItem("Select One", "0"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
     ViewState["dd"] = DropDownList1.SelectedIndex;
     bindcolors();
}
void bindcolors()
{
     if (DropDownList1.SelectedIndex > 0)
     {
          addcolorgroupsTableAdapters.groupavailablecolorTableAdapter ta
              = new addcolorgroupsTableAdapters.groupavailablecolorTableAdapter();
          DataTable dt = ta.GetData(int.Parse(DropDownList1.SelectedValue));
          HtmlTable ht = new HtmlTable();
          ht.Width = "90%";
          ht.Border = 1;
          for (int i = 0; i < dt.Rows.Count; i++)
          {
               HtmlTableRow tr = new HtmlTableRow();
               HtmlTableCell tc1 = new HtmlTableCell();
               HtmlTableCell tc2 = new HtmlTableCell();
               HtmlTableCell tc3 = new HtmlTableCell();
               object[] ob = dt.Rows[i].ItemArray;
               tc1.InnerHtml = ob[0].ToString();
               tc2.InnerHtml = ob[1].ToString();
               tc2.BgColor = "#" + ob[1].ToString();
               Button b = new Button();
               b.Text = "Remove";
               b.CommandArgument = ob[0].ToString();
               AjaxControlToolkit.ConfirmButtonExtender cb
                   = new AjaxControlToolkit.ConfirmButtonExtender();
               cb.ConfirmText = "Are You Sure To Delete This Color From The Group?";
               b.ID = "Bo" + ob[0].ToString();
               b.EnableViewState = true;
               b.Click += new EventHandler(b_Click);
               cb.TargetControlID = "Bo" + ob[0].ToString();
               tc3.Controls.Add(b);
               tc3.Controls.Add(cb);
               tr.Cells.Add(tc1);
               tr.Cells.Add(tc2);
               tr.Cells.Add(tc3);
               ht.Rows.Add(tr);
          }
          Panel1.Controls.Add(ht);
     }
}
void b_Click(object sender, EventArgs e)
{
     Button b = (Button)sender;
     int grp = int.Parse(DropDownList1.SelectedValue);
     int clid = int.Parse(b.CommandArgument);
     addcolorgroupsTableAdapters.QueriesTableAdapter ta
         = new addcolorgroupsTableAdapters.QueriesTableAdapter();
     ta.DeleteQuery_group_color(grp, clid);
     DropDownList2.DataBind();
     bindcolors();
}
protected void Button1_Click(object sender, EventArgs e)
{
     if (DropDownList1.SelectedIndex > 0 && DropDownList2.SelectedIndex > 0)
     {
          int grp = int.Parse(DropDownList1.SelectedValue);
          int clid = int.Parse(DropDownList2.SelectedValue);
          addcolorgroupsTableAdapters.QueriesTableAdapter ta
              = new addcolorgroupsTableAdapters.QueriesTableAdapter();
          ta.Insert_into_group_color(grp, clid);
          DropDownList2.DataBind();
          bindcolors();
     }
}

请告诉我做错了什么?

动态添加的按钮不是触发事件,并且正在丢失状态,尽管在事件中重新添加它pre_init但正在丢失状态

我认为问题是在绑定控件中检查 SelectedIndex> 0。原因是视图状态是在 Init 和 Load 之间计算的,因此尚未设置 SelectedIndex 属性的值(因此 = -1)。
可以尝试其他方法:使用数据绑定到数据库查询结果的 Repeater 控件。这样,可以在中继器及其项模板中定义常规结构。此外,事件处理程序也在项模板中注册。
只要组合框的 SelectedIndex 发生更改,就可以重置转发器的数据源,而无需在 init 中动态重新创建控件。
您的代码应该更短,并且行为更具确定性。