Monday 23 November 2015

Dynamically add controls to PlaceHolder control at run time in asp.net.

Introduction:

I will explain about How to dynamically add controls to PlaceHolder control at run time
in asp.net.

Explanation:

     I am going to create dynamic textbox controls to placeholder with
using PlaceHolder.Controls.Add method.

1.First create placeholder control to source code.

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

2.Create c# code for creating  controls dynamically to place holder.



 protected void Page_Load(object sender, EventArgs e)
    {
        PlaceHolder1.Controls.Add(new LiteralControl("<br />UserName:<br />"));
        TextBox UName_TextBox = new TextBox();
        UName_TextBox.ID = "UserName_TexBox";
        PlaceHolder1.Controls.Add(UName_TextBox);

        PlaceHolder1.Controls.Add(new LiteralControl("<br />Password:<br />"));
        TextBox Pass_TextBox = new TextBox();
        Pass_TextBox.ID = "Password_TextBox";
        PlaceHolder1.Controls.Add(Pass_TextBox);
        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
    }

I used to added UName_TextBox and Pass_TextBox controls to PlaceHolder1 using "PlaceHolder.control.add" method
and also i created two literal controls(UserName and Password) to placeholder1 control.

3.when i run this program it will show as below screen





  

No comments:

Post a Comment