Creating Dynamic Controls

Posted by Venkat | Labels: ,

Good Day to All.

When i came across one issue ie: while creating a dynamic control ie: Dropdownlist on my case , if i choose some value on Dropdownlist onClick of the button i want the selected value that has to be stored in Database. But on postback the Control is not visible.

so here i am going discuss how to create a dynamic control , onpostback how to retain  its value. its just simple ie: Re-Create the Dynamic Control OnPostback so it will retain the selectedvalue.

HTML Code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="True" OnRowDataBound="GridView1_RowDataBound">
        <Columns >
        <asp:TemplateField >
        <ItemTemplate >
        <table >
        <tr>
        <td>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        </td>
        <br />
        <td>
         <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
        </td>
        <br />
        
        <td>
         <asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:PlaceHolder>
        </td>
        </tr>
        </table>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

   <asp:Button ID="Button1" runat="server" Text="Get Value" OnClick="Button1_Click"  />


So here in Gridview i am going to create a dynamic control on page_Load.
so i am placing the Placeholder to hold the Control. Its a Container for that controls.

Code-Behind

private void BindArraylist()
    {
        List<string> bindG = new List<string>() ;
        bindG .Add ("Mani");
        bindG .Add ("Shivaram");
        bindG .Add ("Gopi");
        bindG.Add ("Venu");
        GridView1 .DataSource = bindG ;
        GridView1 .DataBind ();
     
    }
//page_OnLoad

 if (!Page.IsPostBack)
        {
            
            BindArraylist();
        }

//On Row databound we are creation a dynamic dropdownlist, by adding some items and added to the Placholder Container

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            PlaceHolder p1 = new PlaceHolder();
            PlaceHolder p2 = new PlaceHolder();
            PlaceHolder p3 = new PlaceHolder();

            p1 = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
            p2 = (PlaceHolder)e.Row.FindControl("PlaceHolder2");
            p3 = (PlaceHolder)e.Row.FindControl("PlaceHolder3");

            DropDownList d1 = new DropDownList();
            DropDownList d2 = new DropDownList();
            DropDownList d3 = new DropDownList();
            d1.ID = "d1";
            d1.Items.Add("123");
            d1.Items.Add("00");
            d1.Items.Add("dfgdf");
            d2.ID = "d2";
            d2.Items.Add("34f");
            d2.Items.Add("h456");
            d2.Items.Add("ngfy3e35");
            d3.ID = "d3";
            d3.Items.Add("3478");
            d3.Items.Add("0fghdj0");
            d3.Items.Add("2d43g5gh5");

            p1.Controls.Add(d1);
            p2.Controls.Add(d2);
            p3.Controls.Add(d3);
      }
}

Write the code onButton_Click to get the selected value, before that i am calling the bindArraylist method again to re-Create dropdownlist control.


protected void Button1_Click(object sender, EventArgs e)
    {
        BindArraylist();
        foreach (GridViewRow row in GridView1.Rows)
        {
        
            DropDownList d1 = new DropDownList();
            DropDownList d2 = new DropDownList();
            DropDownList d3 = new DropDownList();

            d1 = (DropDownList)row.FindControl("d1");
            d2 = (DropDownList)row.FindControl("d2");
            d3 = (DropDownList)row.FindControl("d3");
            //txtValues.Add(d1.SelectedIndex.ToString());
  
            Response.Write(d1.SelectedValue.ToString() + "-" + d2.SelectedValue.ToString() + "-" + d3.SelectedValue.ToString());
        }
    }

PayOffers.in