I change my question because it probably was not understood. Also sorry for my English...
Dynamically create TextBoxes which put them in array.
A piece 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 textboxes?? Specifically I want to pass the data in a database. Label is a test if I get the value.
Thank you!!!
Not sure if you are aware or not, but you are placing
PlaceHolder1
inPanel1
in every iteration of your for loop. Maybe you meant to doPanel1.Controls.Add(PlaceHolder1);
after the for loop?Anyway, you have placed these TextBox controls into the form. If you are trying to access them via postback, you need to either access them by their set
ID
, or access them through their asp containers (while settingrunat="server"
):or you could try
again make sure they are all
runat="server"
txtName
will not retain its value after a postback.Have your second loop do something more useful than overwrite the set string as well; maybe add each value to a List
quoting:
You declare
authors
AFTER creating the array, thus your error showing thatauthors
doesn't exist yet and later that txtName doesn't exist (as it wasn't instantiated properly). Change it to:.
Also, for correctness, change
String
tostring
,That should do it
To retrieve the controls in the placeholder, use
Placeholder1.FindControl("
[Name of Control]")
.You might want to name the controls to something unique and referenceable when you are creating them.
Alternatively you can reference them with
Placeholder1.Controls(
[Index])
if you know the index of the control.I created objects using a different method from which I called
txtName[i]
, that was my issue.You need to add controls at correct time early enough (normally OnInit) for controls to correctly get values from the post back.
OnInit:
Page_Load:
Check out HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET or many other explanations on "ASP.Net how to add dynamic controls" search criteria.