Jimmysnn
Jimmysnn

Reputation: 593

How to get value from a TextBox

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!!!

Upvotes: 2

Views: 35306

Answers (5)

Jimmysnn
Jimmysnn

Reputation: 593

I created objects using a different method from which I called txtName[i], that was my issue.

public void dataofOtherAuthor() 
    { 
...
txtName[i] = new TextBox(); 
...}

private void UploadForm() 
    { 
...
Label1.Text = txtName[i].Text; 
...
}

Upvotes: 0

Nick Rolando
Nick Rolando

Reputation: 26167

Not sure if you are aware or not, but you are placing PlaceHolder1 in Panel1 in every iteration of your for loop. Maybe you meant to do Panel1.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 setting runat="server"):

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

or you could try

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

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

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
}

Upvotes: 1

user1241335
user1241335

Reputation:

quoting:

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

You declare authors AFTER creating the array, thus your error showing that authors doesn't exist yet and later that txtName doesn't exist (as it wasn't instantiated properly). Change it to:

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

.

Also, for correctness, change String to string,

That should do it

Upvotes: 1

angus
angus

Reputation: 690

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.

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

You need to add controls at correct time early enough (normally OnInit) for controls to correctly get values from the post back.

OnInit:

Form1.Controls.Add(myTextbox);

Page_Load:

var text = myTextbox.Text;

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.

Upvotes: 0

Related Questions