Creating Dynamic controls when changing the dropdown list items. - c#

87 Views Asked by At

I have to show a ModalPopupExtender when changing the items of dropdownlist. The controls showing inside the ModalPopupExtender are dynamic controls including buttons. To fire the button click event for this dynamic buttons, I have introduced view state in the code. Now when changing the item of dropdown list, ModalPopupExtender will come for first item correctly. And then, when changing the item, it throws error like below. How can I clear the previous dynamic controls when changing the dropdown list items?

Multiple controls with the same ID 'lbl1' were found. FindControl requires that controls have unique IDs.

     protected async void Page_Load(object sender, EventArgs e)
        {
                  if (ddl_Forms.SelectedItem.Value != "-1")
                  if (ViewState["viewstate_dynamic"] != null)
                        CreateDynamicControls();
        }
      protected  void ddl_Forms_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateDynamicControls();
        }   
private async void CreateDynamicControls()
    {
        if (ddl_Forms.SelectedItem.Value != "-1")
        {
           url = baseUrl + "GetStructureJson?form_id=" + ddl_Forms.SelectedItem.Value.ToString();

                    using (var objClient = new HttpClient())
                    {
                        objClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + objVariables.login_token);

                        using (var response = await objClient.GetAsync(url))
                        {
                            if ((int)response.StatusCode == 401)//unauthorised or token expired
                            {
                                Response.Redirect("Default.aspx");
                            }
                            if (response.IsSuccessStatusCode)
                            {
                                var jsonString = await response.Content.ReadAsStringAsync();
                                JavaScriptSerializer j = new JavaScriptSerializer();
                                object a = j.Deserialize(jsonString, typeof(object));
                                dict_values = JsonConvert.DeserializeObject<Dictionary<string, string>>(a.ToString());
                                int flag = 0;
                                foreach (KeyValuePair<string, string> entry in dict_values)
                                {
                                    flag++;

                                    TableRow row = new TableRow();
                                    TableCell cell1 = new TableCell();

                                    Label lbl = new Label();
                                    lbl.ID = "lbl" + flag;
                                    lbl.Text = entry.Key;
                                    cell1.Controls.Add(lbl);
                                    row.Cells.Add(cell1);
                                    tbl_forms.Rows.Add(row);
                                }
                                TableRow row3 = new TableRow();
                                TableCell cell3 = new TableCell();

                                Button btn = new Button();
                                btn.ID = "btnSubmit2";
                                btn.Text = "Submit";
                                btn.Click += new System.EventHandler(this.ButtonsubmitForm2_Click);

                                btn.CssClass = "button_green";
                                cell3.Controls.Add(btn);

                                Button btn2 = new Button();
                                btn2.ID = "btnCancel2";
                                btn2.Text = "Cancel";
                                btn2.Click += new EventHandler(btn_Cancel_allocate2_Click);
                                btn2.Style.Add("margin-left", "20px");

                                cell3.Controls.Add(btn2);
                                row3.Cells.Add(cell3);
                                tbl_forms.Rows.Add(row3);
                          
                            **ViewState.Add("viewstate_dynamic", true);**
                                mp2.Show(); //modalpopuextender

                            }
                        }
                    }             
            }
    }
0

There are 0 best solutions below