Wednesday, June 24, 2015

Add dynamically created controls to webpage and read their values in asp.net

Introduction: In this I am try to explain how to add dynamically created controls to webpage and read their values in asp.net

Description:

Add a webform to page. After that I drag and drop the panel control to webpage from toolbox.
HTML markup of page:
<asp:Panel ID="pnl" runat="server">
    </asp:Panel>

Now I am going to generate the asp.net controls at webpage runtime and add them to webpage. Firstly I create a table and rows, columns.  After that controls are added to the columns. In this example I will create a textbox, Dropdownlist, radioButtonlist and Button programmatically.

Write the below given code on Page load of webpage.
C#:-
Button button = new Button();
    Label lbl = new Label();
    protected void Page_Load(object sender, EventArgs e)
    {
        Table tbl = new Table();
        TableCell tcell = new TableCell();
        TableCell tcell1 = new TableCell();
        TableRow trow = new TableRow();

        lbl = new Label();
        lbl.ID = "lblname";
        lbl.Text = "Useranme :";
        this.form1.Controls.Add(lbl);
        tcell.Controls.Add(lbl);

        TextBox txtname = new TextBox();
        txtname.ID = "txtname";
        this.form1.Controls.Add(txtname);
        tcell1.Controls.Add(txtname);
        trow.Cells.Add(tcell);
        trow.Cells.Add(tcell1);
        tbl.Rows.Add(trow);

        tcell = new TableCell();
        tcell1 = new TableCell();
        trow = new TableRow();  
        lbl = new Label();
        lbl.ID = "lblcity";
        lbl.Text = "City :";
        this.form1.Controls.Add(lbl);
        tcell.Controls.Add(lbl);
        DropDownList ddl = new DropDownList();
        ddl.ID = "ddlcity";
        ddl.AutoPostBack = true;
        ddl.Items.Add(new ListItem("--Select--"));
        ddl.Items.Add(new ListItem("Chandigarh"));
        ddl.Items.Add(new ListItem("Delhi"));
        this.form1.Controls.Add(ddl);
        tcell1.Controls.Add(ddl);
        trow.Cells.Add(tcell);
        trow.Cells.Add(tcell1);
        tbl.Rows.Add(trow);

        tcell = new TableCell();
        tcell1 = new TableCell();
        trow = new TableRow();
        lbl = new Label();
        lbl.ID = "lblgender";
        lbl.Text = "Gender :";
        this.form1.Controls.Add(lbl);
        tcell.Controls.Add(lbl);
        RadioButtonList rbl = new RadioButtonList();
        rbl.ID = "rblgender";
        rbl.RepeatDirection = RepeatDirection.Horizontal;
        rbl.Items.Add(new ListItem("Male"));
        rbl.Items.Add(new ListItem("Female"));
        this.form1.Controls.Add(rbl);
        tcell1.Controls.Add(rbl);
        trow.Cells.Add(tcell);
        trow.Cells.Add(tcell1);
        tbl.Rows.Add(trow);
       
        tcell = new TableCell();
        tcell1 = new TableCell();
        trow = new TableRow();

        button = new Button();
        button.ID = "btnsubmit";
        button.Text = "Submit";
        button.Click += new System.EventHandler(btnsubmit_Click);
        this.form1.Controls.Add(button);
        tcell1.Controls.Add(button);
        trow.Cells.Add(tcell);
        trow.Cells.Add(tcell1);
        tbl.Rows.Add(trow);
        pnl.Controls.Add(tbl);
    }

VB:-
Dim Button As New Button()
    Private lbl As New Label()
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim tbl As New Table()
        Dim tcell As New TableCell()
        Dim tcell1 As New TableCell()
        Dim trow As New TableRow()

        lbl = New Label()
        lbl.ID = "lblname"
        lbl.Text = "Useranme :"
        Me.form1.Controls.Add(lbl)
        tcell.Controls.Add(lbl)

        Dim txtname As New TextBox()
        txtname.ID = "txtname"
        Me.form1.Controls.Add(txtname)
        tcell1.Controls.Add(txtname)
        trow.Cells.Add(tcell)
        trow.Cells.Add(tcell1)
        tbl.Rows.Add(trow)

        tcell = New TableCell()
        tcell1 = New TableCell()
        trow = New TableRow()
        lbl = New Label()
        lbl.ID = "lblcity"
        lbl.Text = "City :"
        Me.form1.Controls.Add(lbl)
        tcell.Controls.Add(lbl)
        Dim ddl As New DropDownList()
        ddl.ID = "ddlcity"
        ddl.AutoPostBack = True
        ddl.Items.Add(New ListItem("--Select--"))
        ddl.Items.Add(New ListItem("Chandigarh"))
        ddl.Items.Add(New ListItem("Delhi"))
        Me.form1.Controls.Add(ddl)
        tcell1.Controls.Add(ddl)
        trow.Cells.Add(tcell)
        trow.Cells.Add(tcell1)
        tbl.Rows.Add(trow)

        tcell = New TableCell()
        tcell1 = New TableCell()
        trow = New TableRow()
        lbl = New Label()
        lbl.ID = "lblgender"
        lbl.Text = "Gender :"
        Me.form1.Controls.Add(lbl)
        tcell.Controls.Add(lbl)
        Dim rbl As New RadioButtonList()
        rbl.ID = "rblgender"
        rbl.RepeatDirection = RepeatDirection.Horizontal
        rbl.Items.Add(New ListItem("Male"))
        rbl.Items.Add(New ListItem("Female"))
        Me.form1.Controls.Add(rbl)
        tcell1.Controls.Add(rbl)
        trow.Cells.Add(tcell)
        trow.Cells.Add(tcell1)
        tbl.Rows.Add(trow)

         tcell = New TableCell()
        tcell1 = New TableCell()
        trow = New TableRow()

        Button = New Button()
        Button.ID = "btnsubmit"
        Button.Text = "Submit"
        AddHandler Button.Click, AddressOf btnsubmit_Click
        Me.form1.Controls.Add(Button)
        tcell1.Controls.Add(Button)
        trow.Cells.Add(tcell)
        trow.Cells.Add(tcell1)
        tbl.Rows.Add(trow)
         pnl.Controls.Add(tbl)
    End Sub

Now I am going to read the control values. I have created an event for button to submit.
C#:-
protected void btnsubmit_Click(object sender, EventArgs a)
    {
        TextBox txtname = (TextBox)pnl.FindControl("txtname");
        DropDownList ddlcity = (DropDownList)pnl.FindControl("ddlcity");
        RadioButtonList rblgender = (RadioButtonList)pnl.FindControl("rblgender");
        Response.Write("Name :" + txtname.Text + "<br/>City :" + ddlcity.SelectedItem + "<br/>Gender :" + rblgender.SelectedItem);
        txtname.Text = string.Empty;
        ddlcity.SelectedIndex = -1;
        rblgender.SelectedIndex = -1;
    }

VB:-
Protected Sub btnsubmit_Click(sender As Object, a As EventArgs)
        Dim txtname As TextBox = DirectCast(pnl.FindControl("txtname"), TextBox)
        Dim ddlcity As DropDownList = DirectCast(pnl.FindControl("ddlcity"), DropDownList)
        Dim rblgender As RadioButtonList = DirectCast(pnl.FindControl("rblgender"), RadioButtonList)
        Dim lblresult As Label = DirectCast(pnl.FindControl("lblresult"), Label)
        Response.Write("Name :" + txtname.Text & "<br/>City :" + ddlcity.SelectedItem.ToString() & "<br/>Gender :" + rblgender.SelectedItem.ToString())
        txtname.Text = String.Empty
        ddlcity.SelectedIndex = -1
        rblgender.SelectedIndex = -1
    End Sub

Now build, run the project and check out the result.

 Result:
Add dynamically created controls to webpage and read their values in asp.net
In this article we have learn how to add the dynamically created controls to webpage and read their values in asp.net (C#,VB). I hope you enjoyed this article. 

No comments:

Post a Comment