Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any one plz help me . i want to add a panle dynamicaly in asp.net page and i want one radio button and one lable inside it..
Posted

Place panel in aspx page and add controls in code behind.

see this sample code. It will help you i think

C#
 protected void btnAddField_click( Object sender, EventArgs e ) {
     int FieldCount = 0;
     if (ViewState["FieldCount"] != null)
     {
         FieldCount = (int)ViewState["FieldCount"];
     }

     Table tbl = new Table();
     if (Session["DynamicTable"] != null)
     {
         tbl = (Table)Session["DynamicTable"];
     }

     CheckBox chkNewField = new CheckBox();
     chkNewField.ID = "chkNewField" + FieldCount.ToString();
     chkNewField.Checked = true;

     Label LblNewLabel = new Label();
     LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
     LblNewLabel.Text = "New Lable";

     TextBox TxtNewLabel = new TextBox();
     TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

     Label LblNewValue = new Label();
     LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
     LblNewValue.Text = "New Value";

     TextBox TxtNewValue = new TextBox();
     TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

     TableRow tRow = new TableRow();

     TableCell tCell1 = new TableCell();
     TableCell tCell2 = new TableCell();
     tCell2.Attributes.Add("class", "medium");
     TableCell tCell3 = new TableCell();
     tCell3.Attributes.Add("class", "medium");
     TableCell tCell4 = new TableCell();
     TableCell tCell5 = new TableCell();
     tCell5.Attributes.Add("class", "medium");
     TableCell tCell6 = new TableCell();
     tCell6.Attributes.Add("class", "medium");

     tCell1.Controls.Add(chkNewField);
     tCell2.Controls.Add(LblNewLabel);
     tCell3.Controls.Add(TxtNewLabel);
     tCell4.Controls.Add(new LiteralControl(""));
     tCell5.Controls.Add(LblNewValue);
     tCell6.Controls.Add(TxtNewValue);

     tRow.Cells.Add(tCell1);
     tRow.Cells.Add(tCell2);
     tRow.Cells.Add(tCell3);
     tRow.Cells.Add(tCell4);
     tRow.Cells.Add(tCell5);
     tRow.Cells.Add(tCell6);

     tbl.Rows.Add(tRow);
     placeHolderTable.Controls.Remove(tbl);
     placeHolderTable.Controls.Add(tbl);
     Session["DynamicTable"] = tbl;
     FieldCount++;
     ViewState["FieldCount"] = FieldCount;
}
 
Share this answer
 
v2
Comments
Alok.singh1166 5-Jul-13 8:52am    
What is this placeHOldertable
Manu V Nath 5-Jul-13 8:56am    
Am using contentplaceholder instead of panel. Its not the exact code. Its a sample for creating controls dynamically. Here i am adding controls on a content placeholder while clicking on a button
Alok.singh1166 5-Jul-13 9:16am    
Thanks Alot i got that :)
Manu V Nath 5-Jul-13 9:18am    
Welcome Alok. Please accept solution if its help you. Happy programming
for that you need to put a place holder hidden & then add control to that.

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


& Then add code on button click

protected void btn0_Click(object sender, EventArgs e)
{
Label myLabel = new Label();
myLabel.Text = "Sample Label";
Panel Panel1 = new Panel();
Panel1.Controls.Add(myLabel);

PlaceHolder1.Controls.Add(Panel1);

RadioButton rd = new RadioButton();
rd.Checked = true;
Panel1.Controls.Add(rd);
}
 
Share this answer
 
Comments
Alok.singh1166 7-Jul-13 11:22am    
Thanks man it was really help full
Try some thing like this
C#
Label myLabel = new Label();
myLabel.Text = "Sample Label";
Panel Panel1 = new Panel();
Panel1.Controls.Add(myLabel);
 
Share this answer
 
v2
Comments
Alok.singh1166 5-Jul-13 8:56am    
Plz clarify it i did'nt get this ( i have to click on the button then i have to create dynamicaly a panel and inside that i want one radiobutton and one lable)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900