aspx Tutorial

NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services,
Advertise Here

Toolbar

Get our toolbar!

Advertize



Posted by Aamir Hasan   on Saturday, August 25, 2012 Total Views:  

In this example, you will see how to add textboxes and button control to asp.net page at runtime and fetch all values of dynamic created textboxes and display it on a label by attaching the Click event handler with button.

Desgin script

<html>

<head runat="server">

    <title>How to add textboxes and button control to asp.net page at runtime</title>

    <style type="text/css">

        body, html

        {

            font-family: Verdana. Arial;

            font-size: 12px;

            color: #444;

            background-color: #fff;

        }

        #pan span

        {

            padding: 5px 10px;

        }

        #pan input

        {

            padding: 3px;

            margin: 5px;

            color: #ccc;

        }

        #pan input:hover

        {

            color: #ccc;

            background-color: #fafafa;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div id="pan">

        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

        <asp:Label ID="lbMsg" runat="server" Text=""></asp:Label>

    </div>

    </form>

</body>

</html>

 

Code behind script

 

     protected void Page_Load(object sender, EventArgs e)

    {

        this.CreateControl();

    }

    protected void CreateControl()

    {

        for (int i = 0; i < 5; i++)

        {

            var lab = new Label {

                Text = String.Format("TextBox value {0}", i),

                ID = String.Format("lb_{0}", i)

            };

            var tbox = new TextBox

               {

                   Text = String.Format("TextBox value {0}", i),

                   ID = String.Format("TextBox_{0}", i)

               };

            this.PlaceHolder1.Controls.Add(lab);

            this.PlaceHolder1.Controls.Add(tbox);

            this.PlaceHolder1.Controls.Add(new LiteralControl("<br>"));

        }

        Button btn = new Button();

        btn.Text = "Submit";

        btn.Click += new EventHandler(btn_Click);

        this.form1.Controls.Add(btn);

    }

    void btn_Click(object sender, EventArgs e)

    {

        lbMsg.Text = "";

        foreach (Control control in PlaceHolder1.Controls)

            if (control is TextBox)

                lbMsg.Text += (control as TextBox).Text+"<br>";

    }

 

 


Note:
If you have alternative solution, please comments here to share with us.

 

Output

 

Demo

See live demo

Download

Create-runtime-textboxes-button.rar (1.14 kb)

Tags:
Category: All | asp.net
Protected by Copyscape Online Plagiarism Tool

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5