How to Create Dynamic Controls

Posted by Jayakumars under ASP.NET AJAX on 3/12/2013 | Points: 10 | Views : 955 | Status : [Member] [MVP] | Replies : 2
Hi

I have only Test.Aspx Page only . When i load Test.aspx File i need design table with controls label,textbox,Button controls also from Database and load in my page and generate it
how will do those things.

Mark as Answer if its helpful to you


Responses

Posted by: Karthikreddy on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
private void Page_Load(object sender, System.EventArgs e)

{
Button newButton = new Button();
newButton.Text = "New Button";
newButton.ID = "newButton";
newButton.Click += new System.EventHandler(this.newButton_Click);

WebUserControl1 newUserControl =
(WebUserControl1)LoadControl("WebUserControl1.ascx");
newUserControl.ID = "newUserControl";
this.PlaceHolder.Add(newUserControl);
this.PlaceHolder.Add(newButton);
}


this is only for button like this you can create any control

k@rth!k
karthikreddy08a50@gmail.com

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

I have done this.
Try this code.
This is an example.

Design page:
<form id="form1" runat="server">

<div>

</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Table ID="Table1" runat="server">
</asp:Table>
</form>

Cs page:
protected void Page_Load(object sender, EventArgs e)

{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TextBox txt = new TextBox();
txt.Text = "hi";

tc.Controls.Add(txt);
tr.Cells.Add(tc);

TableCell tc1 = new TableCell();
Button btn = new Button();
btn.Click += btn_Click;
btn.Width = 200;
btn.Text = "click me";
tc1.Controls.Add(btn);
tr.Cells.Add(tc1);
Table1.Rows.Add(tr);

} void btn_Click(object sender, EventArgs e)
{
Label1.Text = "welcome";
}
Like this create cells and add the contols dynamically.
you can create any events.
You also retrieve the data and load the data to the controls



Thanks & Regards,
Santhi .V

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response