0

I have a dropdown which is bound to a database. On its index change there is a function that add some button in a panel based upon selected value.

I am reading those button in page_init event but still I get null values, i.e. event bound with the button never fires.

Here is my code and dropdownlist1 is the dropdown that is adding dynamic button.

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();
     }
}

Please tell what I am doing wrong?

Ratna
  • 2,289
  • 3
  • 26
  • 50
  • I think the problem is the check for SelectedIndex > 0 in bindControls. Have you already run the page in Debug mode and verified that bindControls is run as expected after a postback? Are the controls really created in case of a postback? Do you ever remove the buttons if the users selects another entry of the Combobox? – Markus Oct 29 '13 at 12:31
  • SelectedIndex = -1. But why is that? I dont know. and yes i have to remove and add other buttons if selection changes. – Ratna Oct 29 '13 at 12:42
  • Please see my answer below. – Markus Oct 29 '13 at 13:18

1 Answers1

0

I think the problem is the check for SelectedIndex > 0 in bindControls. The reason is that ViewState is evaluated between the Init and Load so the value for the SelectedIndex property is not set yet (and therefore = -1).
You could try a different approach: use a Repeater control that is databound to the results of the database query. This way, the general structure can be defined in the Repeater and its ItemTemplate. Also the EventHandlers are registered in the ItemTemplate.
You can reset the DataSource of the Repeater whenever the SelectedIndex of the Combobox changes and do not need to recreate the controls dynamically in init.
Your code should be much shorter and behave more deterministic.

Markus
  • 20,838
  • 4
  • 31
  • 55