How to get the value of dynamically created text box which is created dynamically on click of button control.

<p>

Dear All
I am new in asp.net.
I am doing one project there I have one doubt.

what I want is I have taken two  button controls in my page, in click of first button1 I have  dynamically created one textbox and I added this textbox to my page and also set the id for this textbox,.
after that in click of second button2 I want that the value of the textbox will be print in my page by getting the value of the textbbox.

 

some codes are given below.

 <form id="form1" runat="server">
    <div>
        &nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Submit" />
     
    </div>

 protected void Button1_Click(object sender, EventArgs e)
    {


        TextBox tb = new TextBox();
        tb.ID = "tb1";
        tb.Text = "Hello";
        Page.Form.Controls.Add(tb);
      
       
    }
 
    protected void Button2_Click(object sender, EventArgs e)
    {
       
        TextBox tb1 = (TextBox)Page.FindControl("tb1");
        Response.Write(tb1.Text);
    }

after clicking on second button I am finding exception.

How can I do That???

Thanks in advance to all !

</p>

 


Ajay Kumar Verma
0
ks
2/11/2008 9:09:09 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

8 Replies
1735 Views

Similar Articles

[PageSpeed] 21

 No chance to do this unless the code from Button1 click is moved to Page load Sad. Dynamic created controls are not saved between post backs.


Best regards,
Andrei
0
AndreiH
2/11/2008 10:02:06 AM

try using

Request["tb1"] to get the value of the textbox

 


Vikram
www.vikramlakhotia.com


Please mark the answer if it helped you
0
vik20000in
2/11/2008 10:03:58 AM

ks.ajayverma:
  protected void Button2_Click(object sender, EventArgs e)
    {
       
        TextBox tb1 = (TextBox)Page.FindControl("tb1");
        Response.Write(tb1.Text);
    }
 

 

hi,

use below code 

 protected void Button2_Click(object sender, EventArgs e)
    {
       
        Response.Write(Request["tb1"]);
    }

 


Shah Dharnendra G
Sr.Analyst Programmer,
GTL-Ahmedabad
0
dharnendra
2/11/2008 11:30:21 AM

i have a suggetsion

why dont u create a text box control in ur .aspx page not dynamically and set it invisible by

<asp:TextBox ID="TextBox1" runat="server" Enabled="False" Visible="False"></asp:TextBox>

and then on selecting the button set its visibilty bck to true like

protected void Button1_Click1(object sender, EventArgs e)

{

TextBox1.Visible =
true;

}

 

that is simple

sam


Saima Ghumman
--Mark the post as Answer if the Post helps you--
0
Sam
2/11/2008 12:02:00 PM

Hi ks.ajayverma,

Because you create TextBox control dynamicly, so you have to load the control every time.

I change you code slightly, it work fine. hope to understand the theory behind it.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tb"]!=null)
        {
            TextBox tb = Session["tb"] as TextBox;
            Page.Form.Controls.Add(tb);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();
        tb.ID = "tb1";
        tb.Text = "Hello";
        Page.Form.Controls.Add(tb);
        Session["tb"] = tb;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox tb1 = (TextBox)Page.FindControl("tb1");
        Response.Write(tb1.Text);
    }

Let me know if I have misunderstood what you mean. Thanks.
Hope it helps,
Hua Jun


Sincerely,
Hua Jun Li
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Hua
2/12/2008 10:22:38 AM

Hi,

the Hua Jun solution really works but I must say that storing the all TextBox instance in session isn't the best solution.

For these kind of situations I always store a bussiness flag, not the control. This way Session wont grow to much but you will have to create a new Textbox instance:

    protected void Page_Load(object sender, EventArgs e)
    {
        bool flag = Session["business flag"] as bool;
        if (flag)
        {
            AddTextBox();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();
        tb.ID = "tb1";
        tb.Text = "Hello";
        Page.Controls.Add(tb);
        Session["business flag"] = true;
    }

    private void AddTextBox()
    {
        [...]
    }
 
Nuno Gomes [visit my blog]
Portugal - Europe's West Coast
[Don't forget to click "Mark as Answer" on the post(s) that helped you.]
0
nmgomes
2/12/2008 11:57:12 AM

 still am not able to retrieve the values from the text box which are created dynamically..


 

 


Sk
0
sudhalaksh
1/24/2009 9:28:25 AM

Hi sudhalaksh,

Regarding dynamically created controls, they must be added to page controls hierarchy on Page.OnLoad or before( OnInit).

If this rule is not respected then by the time Page try to find the controls that posted values they wont be present in controls hierarchy and naturaly the posted value will not be used for anything.

Follow this rule and you will solve your problem.


Nuno Gomes [visit my blog]
Portugal - Europe's West Coast
[Don't forget to click "Mark as Answer" on the post(s) that helped you.]
0
nmgomes
1/25/2009 12:52:23 AM
Reply:

Similar Artilces:

Please guide
Hi, I am creating dynamic panels and each of these panels have an image and a button. I would like to show a modal popup when the button of the panel is clicked. Contents of popup depend on which button was clicked. I would sincerely appreciate if some one could guide me if this is possible with Ajax and AjaxControltoolkit. Till now I tried to create modalpopupextended dynamically in the click event of button and page fails with an error message that TargetControl could not be found. Please guide. Regards & thanks Kapil    Hi Kapil, Unfortunately, the ModalPopup must h...

Create Dynamic Dropdownlist Controls / How to Create Dynamic Dropdownlist Controls
Hi,    This might be useful for someone who strucks with create dynamic dropdownlist/ controls. In ASP.Net is page is stateless. when you are creating the dynamic dropdownlist control following issues you might be facing, 1. After loaded the control, on selected index changed event the control will be disappear. 2. The selectedIndexChanged raised event might not be fired when selection of Dropdownlist. 3. Locate the exact dropdownlist which you created dynamically. 4.Placing this dynamic controls in exact location in the page. Solution: You have to re-create the con...

create and add controls dynamically to dynamically created Table Control
How can i create web server Table control dynamically with vary number of  rows and columns, and  then adding  further  other controls(like  dropdown, Text box etc.)  randomly to the specific created Table Cells. First of all you should use PlaceHolder control to add Table control to it:<asp:PlaceHolder id="PH" runat="server"></asp:PlaceHolder>Code Behinde:Table t = new Table();TableRow tr = new TableRow();TableCell td = new TableCell();//td.Text = "Hello, I am a TD";// Here well create TextBox and add it to TDTextBox txt = new TextBox();tx...

Dynamically create text box when a button is clicked
Hi, I want to create a text box everytime a button is clicked. These text boxes are for user suggestions. Whenever user wants to add more suggestion he clicks a button to add one more text box. I have written code like thisprotected void Button2_Click(object sender, EventArgs e) {int counter = Convert.ToInt32(Session["tbxNo"]); string strId="txtSugesstion" + counter.ToString();TextBox tbx;tbx=new TextBox(); tbx.ID = strId; tbx.Width =Unit.Pixel(500);tbx.Height = Unit.Pixel(100);  Session["tbxNo"] = counter+1; PlaceHolder1.Controls.Add(tbx); } ...

Get the value or Text of dynamically created controls?
Hello, I have a page with a placeHolder, in wich I create several controls (of textBox, checkBox and dropdownList type) I found several articles on the web about dynamic controls, but I couldn't find how to access each controls properties. I would like to be able to check if the user has checked the checkBox or not, and read the text inside the textBox I created Is that not possible? Thank you An important thing to keep in mind is that you need to recreate the dynamic controls you add to your PlaceHolder control each time a postback occurs.  After that, you can use FindContr...

How to create a dynamic text box on each click of the button ?
In my project , I want to create a dynamic text box on each click of the button can any one help me ? Hi there,Please check this link Hope this helpsSunny NAGIProper Preparation Prevents Poor PerformanceDont forget to click "Mark as Answer" on the post that helped you. hi  ,          The above link gives the textbox for first click . But ,When i click on the button second time another textbox should be created at the bottom of the first text box ....

getting values from dynamically created text boxes
 I have created some text boxes dynamically and added them to a control Table1 for structure 1 TableCell td3 = new TableCell(); 2 TextBox prc = new TextBox(); 3 prc.Width = new Unit(30); 4 prc.ID = "prc_" + i.ToString(); 5 prc.Text = max.ToString(); 6 td3.Controls.Add(prc);  how I can the values from the text box inside the Table1 control. There are multiple textboxes with id like prc1, prc2 and I have to get the values of all.   Hi, You can use the .FindControl method on the Page class. Supply the ID of the textbox as a parameter.For ...

How get value from a dynamically created control in a user control
HI, I am working with a version of IBuySpy. On one page is a form that contains a user control that lists the items the user purchased, the subtotal, shipping, etc. I wanted to add a DropDownList from which the user could select the shipper. The dropdown list works fine as long as it has .AutoPostBack = false. However, I cannot figure out how to get the value that the user selected when they finally press the submit button on the form. All attempts to locate the control and it's value have failed. I tried drillingdown from the form to the child controls, and sub child controls. N...

Creating Delete button for Dynamically created controls?
Hi, I have at long last been able to create controls dynamically. They even maintain state after a request.  (See code below). On clicking the Add button, this code basically creates 4 textboxes + 4 labels on the fly.  Here is the catch and a real challenge for asp.net gurus. Now that I have successfully built an "Add" functionality, I am finding myself in need of a delete button that would delete the last set of controls created per click. Here is my code. Any help would be greatly appreciated. Please bare with me as I learn.(For those who landed on my page through t...

Bubbling Events on Dynamically created controls that are on dynamically created user-controls on a module!! HELP MAH PUHLEASE!
I may be cross posting this question, but I am really going to go crazy.. Please help if you can!I think I am going to go mad. If anyone can help me with this small issue, I will build an idol in your image made of clay, and have nubile maidens dance in front of it in worship!!!!I have a module. Let's call it Module-1Module-1 Connects to a database and gets a list of items.Module-1 Then creates on the fly a number of buttons that represent that list of items.Each button when clicked dynamically creates a web-user control. Let's pull out 1 button, and 1 usercontrol and call them Button-1 and ...

how to create button controls dynamically .. then i need that click event of buttons
how to create button controls  dynamically .. then i need that click event of buttons  urgent ???????Vino  Add dynamic controls with eventsWebsite Design Darlington - http://mdssolutions.co.ukhttp://lessthandot.com - Experts, Information, Ideas & Knowledgehttp://aspnetlibrary.com - An online resource for professional ASP.NET developersPlease remember to click "Mark as Answer" on this post if it helped you refer below snippet..public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)  ...

Reading form values in dynamically created child controls in a user control
I have a common problem for which I've searched the web for answers for hours to no avail. I have some radiobuttonlists ,dropdownlists,checkboxlists and text controls in a user control. The lists are created statically, however the items in the lists are populated at run time. The lists are displayed in default.aspx (the containing page) and populated on the Page_Load event of the user control .ascx file based on an external data source containing arrays of data. I want to be able to read the values selected by the user from deafult.aspx, the parent page. details: The .ascx...

Get user input from dynamic created control, why i cant get the value???
Hi!, i was tried to dynamically generate some textboxes according the selected number in a dropdownlist, but when i need to get the user input from those textboxes.. i cant get at all!!, Can any1 guide me on tis? Below is my code.Basically, i have 1 dropdownlist (ddltbCount), 1 placeHolder(Pholder1), 1 button(btGetValue), i dont know y i cant post the html here... Private count As Integer = 1 Sub IterateThroughChildren(ByVal parent As Control) For Each c As Control In parent.Controls If c.GetType.ToString.Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then ' CType(c, T...

I Want to create a tool box,toolbox contains image button and fileupload.These two buttons can be added at runtime(user can add any number of image button and fileupload button dynamically by click)
I Want to create a tool box,toolbox contains image button and fileupload buttons.These two buttons can be added at runtime(user can add any number of image button and fileupload button dynamically by click).for every image user will browse images using fileuploader and he will add the image to that imagebutton at runtime. how to write server side coding to upload the selected image and to set that image to imagebutton at runtime? is it possible? any other way to do it?  advance thanks ask123: I Want to create a tool box,toolbox contains image button and fileupload b...

Web resources about - How to get the value of dynamically created text box which is created dynamically on click of button control. - asp.net.web-forms

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ...

Everything.me launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...


A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97%

Everything.me Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 11/26/2015 12:11:55 PM