Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting a problem.i have a radio button on whose click i am generating a textbox dynamically,
i also have a button id="btn" and a label with id "status" on the same page
basically my objective is that whenever the radiobutton is checked,a textbox is generated dynamically,and whatsoever text is written in the tex box must be retrieved in the label with id="status" on the button click="btn",but the problem i am facing is that whenever
i click the button,the text box simply dissappears and i cant retrieve the text,please can someone help me???
C#
protected void radiobutton_checked(object sender, EventArgs e)
  {
 if (radiobtn1.Checked == true)
    {
          
          TextBox txtbox = new TextBox();
          pnl.Controls.Add(txtbox);  //(pnl is a panel in whch i m placing the textbox)//
}

C#
protected void Upd_Click(object sender, EventArgs e)
  {
      if (txtbox.Text != string.empty)
      {
          
          status.Text ="text in textbox was"+txtbox.Text;



      }
Posted
Updated 28-Dec-13 3:24am
v2

Retaining State for Dynamically Created Controls in ASP.NET applications[^]
Quote:
When dynamically adding controls to an ASP.NET page in runtime the object references are lost at postback because they have no handle in the codebehind. The values entered by the user is not availible when accessing these objects after postback, and they seem empty. This article describes how to use ViewState to recreate and reinsert the postback values into the objects to access their values.
 
Share this answer
 
I think Tadit Dash has found your larger-scale problem, but I also would like to point out that your code is flawed: in fact, your code should not build in the first place, and you should get an error message like:

"The name 'textbox' does not exist in the current context"

The name 'textbox exists only in the scope of the 'radioButton_Checked EventHandler.

Yes, your code does create a new TextBox, and that TextBox is added to the ControlsCollection of a Panel; so, that TextBox exists, but you have no valid reference to it to use to access it ... unless you go diving into the ControlsCollection of the Panel.

By the way, I'd like to recommend you use a Button, not a RadioButton, for this type of UI task. A RadioButton is meant to be used in conjunction with other RadioButtons to give the user a chance to select one option from a set of options.

If you want to allow the user to turn-on/off being able to create new TextBoxes, you might consider having a CheckBox: when it's checked the Button that creates TextBoxes is enabled, and you disable it when its unchecked.

Also note that if you do not set the position of the TextBoxes you create, they may simply be placed exactly on top of each other.
 
Share this answer
 
 
Share this answer
 
declare a string variable as follows at class level like
static string obj;
and then store your text to this variable then assign this variable to label's text property
 
Share this answer
 

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