Posted: 11/7/2009
hi all,
I want to add dynamically control on my page on a click of a button.
Can any one help me?
Thanks in advance.
naval198409 said: I want to add dynamically control on my page on a click of a button.
Naval,
If u want to generate textbox dynamically on button clickfollowing is the code for .aspx side:
<div> <asp:TextBox runat="server" ID="txtadd" ></asp:TextBox> <asp:Button ID="btnAdd" runat="server" Text="Create" onclick="btnAdd_Click" /> <asp:Panel ID="pnlText" runat="server"></asp:Panel> </div>
code for code behind file:
protected void btnAdd_Click(object sender, EventArgs e) { for (int i = 0; i < Convert.ToInt32(txtadd.Text); i++) { TextBox txt = new TextBox(); txt.Width = 300; txt.Height = 50; txt.ID = "txtbox" + i; txt.TextMode = TextBoxMode.MultiLine; pnlText.Controls.Add(txt); } }
For more please follow following links:http://codeasp.net/blogs/Vijjendra/microsoft-net/71/dynamically-genearte-textbox-at-runtime-in-aspnet
i want to add a new control in a new line the code you had provided me added the control in a single line
so how we add a new control
in a new line
please provide me a solutions
thanks
in advance
naval kishor pandey
Hi,
naval198409 said: i want to add a new control in a new line
i want to add a new control in a new line
Modifying few lines of code of above poster code:
In Aspx
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox runat="server" ID="txtadd"></asp:TextBox> <asp:Button ID="btnAdd" runat="server" Text="Create" OnClick="btnAdd_Click" /> <asp:Panel ID="pnlText" runat="server"> </asp:Panel> </div> </form> </body> </html>
In Code Behind
using System; using System.Web.UI; using System.Web.UI.WebControls; namespace TestProject { public partial class AddingControlsDynamically : System.Web.UI.Page { protected void btnAdd_Click(object sender, EventArgs e) { for (int i = 0; i < Convert.ToInt32(txtadd.Text); i++) { TextBox txt = new TextBox { Width = 300, Height = 50, ID = string.Format("txtbox{0}", i), TextMode = TextBoxMode.MultiLine }; LiteralControl hcBreak = new LiteralControl ("<br/>"); pnlText.Controls.Add(hcBreak); pnlText.Controls.Add(txt); } } } }
And here is the output when you put 4 in textbox and press Create button
Thanks,
Raghav
Posted: 11/9/2009
naval198409 said: i want to add a new control in a new line the code you had provided me added the control in a single line so how we add a new control in a new line please provide me a solutions
I have written a sample demo below that you might be interesed with:
FAQ: Dynamically Adding Rows in ASP Table on Button Click event
Dynamically Adding TextBox Control to ASPNET Table
Here's another example: Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
Posted: 12/30/2009
naval198409 said: Hi i want to craete web sevice which interact with the hardware deivice like webcam on client sidethanks in advancenaval kishor pandey
Hi i want to craete web sevice which interact with the hardware deivice like webcam on client side
thanks in advance
Pleae post your new query in new thread.
Posted: 6/6/2011
but when u generated the controls in run time, u can't find the controls after post back :(
so in every page load u have to generate/regenerate the controls with the same id's to capture the values enter in the dynamically generated controls.
ramakrishnamraju, this is an old thread. I think the best would be to open new thread for your questions/issues...
Thanks,Hajan