Loading ...

Generate controls at run time.

Who is online?  0 guests and 0 members
home  »  forums   »  basic asp.net   »  web forms   » Generate controls at run time.

Generate controls at run time.

Posts under the topic: Generate controls at run time.

Posted: 11/7/2009

Lurker 192  points  Lurker
  • Joined on: 10/23/2009
  • Posts: 20

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

Contributor 2841  points  Contributor
  • Joined on: 11/29/2008
  • Posts: 62
  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


tags textbox

Posted: 11/7/2009

Lurker 192  points  Lurker
  • Joined on: 10/23/2009
  • Posts: 20

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

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  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

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  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

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490

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.


Posted: 6/6/2011

Lurker 125  points  Lurker
  • Joined on: 5/24/2011
  • Posts: 9

 

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.


Posted: 6/6/2011

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

ramakrishnamraju, this is an old thread. I think the best would be to open new thread for your questions/issues...

Thanks,
Hajan


Page 1 of 1 (8 items)