home »forums »basic asp.net »web forms »Generate controls at run time.

Generate controls at run time.

Topic RSS Feed

Posts under the topic: Generate controls at run time.

Posted: 11/7/2009 3:57:35 AM

Lurker 180 points Lurker
  • Joined on: 10/23/2009 2:43:08 AM
  • Posts: 14

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.


Posted: 11/7/2009 4:04:46 AM

Contributor 2596 points Contributor
  • Joined on: 11/29/2008 6:26:22 AM
  • Posts: 53
answered  Answered

 

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 click

following 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


Posted: 11/7/2009 4:38:46 AM

Lurker 180 points Lurker
  • Joined on: 10/23/2009 2:43:08 AM
  • Posts: 14

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

 


Posted: 11/7/2009 5:32:41 AM

Professional 9529 points Professional
  • Joined on: 4/19/2009 1:46:52 AM
  • Posts: 219
answered  Answered

Hi,

 

naval198409 said:

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 9:35:19 PM

Contributor 5349 points Contributor
  • Joined on: 4/15/2009 12:12:23 PM
  • Posts: 236
answered  Answered

 

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 12:12:11 AM

Lurker 180 points Lurker
  • Joined on: 10/23/2009 2:43:08 AM
  • Posts: 14

Hi i want to craete web sevice which interact with the hardware  deivice like webcam on client side

thanks in advance

naval kishor pandey


Posted: 12/30/2009 2:10:02 AM

Professional 9529 points Professional
  • Joined on: 4/19/2009 1:46:52 AM
  • Posts: 219

naval198409 said:

Hi i want to craete web sevice which interact with the hardware  deivice like webcam on client side

thanks in advance

naval kishor pandey

 

Pleae post your new query in new thread.


Page 1 of 1 (7 items)