How to get value from TextBox

I am changing my question because it probably was not understood. Also sorry for my english ...

Dynamically create text fields that put them in an array.

Part of my code:

public partial class NewArticleForm : System.Web.UI.Page { private Label[] lblName; private TextBox[] txtName; private Label[] lblSurname; private TextBox[] txtSurname; private PlaceHolder PlaceHolder1; public int NumberOfOtherAuthors() { Int32 index = Convert.ToInt32(NumberList.SelectedValue); return index; } public void dataofOtherAuthor() { int authors; int i = 0; int j=1 authors = NumberOfOtherAuthors(); lblName = new Label[authors]; txtName = new TextBox[authors]; lblSurname = new Label[authors]; txtSurname = new TextBox[authors]; PlaceHolder1 = new PlaceHolder(); for (i = 0; i < authors; i++) { Label authorInformation = new Label(); authorInformation.Text = "Information for Author " + j.ToString() + " :"; lblName[i] = new Label(); lblName[i].Text = "Name:"; txtName[i] = new TextBox(); lblSurname[i] = new Label(); lblSurname[i].Text = "Surname:"; txtSurname[i] = new TextBox(); PlaceHolder1.Controls.Add(authorInformation); PlaceHolder1.Controls.Add(lblName[i]); PlaceHolder1.Controls.Add(txtName[i]); PlaceHolder1.Controls.Add(lblSurname[i]); PlaceHolder1.Controls.Add(txtSurname[i]); // Add a spacer in the form of an HTML <BR> element. Panel1.Controls.Add(PlaceHolder1); j++; } } protected void NumberList_SelectedIndexChanged(object sender, EventArgs e) { dataofOtherAuthor(); } private void UploadForm() { int numberOfOtherAuthors = NumberOfOtherAuthors(); int i=0; for (i = 0; i < numberOfOtherAuthors; i++) { Label1.Text = txtName[i].Text; } } protected void btnUpload_Click(object sender, EventArgs e) { UploadForm(); } } 

How can I get the value of text fields? In particular, I want to transfer data to a database. A shortcut is a test if I get a value.

Thanks!!!

+2
source share
5 answers

Not sure if you know or not, but you place PlaceHolder1 in Panel1 on each iteration of the for loop. Perhaps you wanted to do Panel1.Controls.Add(PlaceHolder1); after the for loop?
In any case, you have placed these TextBox controls on the form. If you are trying to access them via postback, you need to either access them by their ID set, or access them through your asp containers (when setting runat="server" ):

 x = (p1.Controls[placeHolderIndex].Controls[textBoxIndex] as TextBox).Text; 

or you can try

 x = (p1.FindControl("placeHolder_ID").FindControl("textBox_ID") as TextBox).Text; 

make sure all runat="server" again

txtName will not retain its value after the postback.

Ask your second loop to do something more useful than overwriting the installed line as well; maybe add each value to the list

 List<string> x = new List<string>(); for (i = 0; i < authors; i++) { x.Add((p1.Controls[placeHolderIndex].Controls[i] as TextBox).Text); //increment placeHolderIndex if you don't change your design } 
+1
source

to quote:

 txtName = new TextBox[authors]; int authors = 5; 

You declare authors AFTER the array was created, so your error is showing that authors do not exist yet, and later that the name txtName does not exist (since it was not created properly). Change it to:

 int authors = 5; txtName = new TextBox[authors]; 

.

Also, for the correctness, change String to String ,

It should do it

+1
source

You need to add controls at the right time early enough (usually OnInit) for controls to correctly get values ​​from the message back.

OnInit:

 Form1.Controls.Add(myTextbox); 

Page_Load:

 var text = myTextbox.Text; 

Check out HOW: Dynamically create controls in ASP.NET using Visual C # .NET or many other explanations about "ASP.Net how to add dynamic controls."

0
source

To get the controls in the placeholder, use Placeholder1.FindControl(" [Control Name] ") .

You might want to name the controls unique and referenced when you create them.

Alternatively, you can reference them using Placeholder1.Controls( [Index] ) if you know the index of the control.

0
source

I created the objects using another method from which I called txtName[i] , that was my problem.

 public void dataofOtherAuthor() { ... txtName[i] = new TextBox(); ...} private void UploadForm() { ... Label1.Text = txtName[i].Text; ... } 
0
source

All Articles