0

I want to retrieve a textbox value in a button click event, but as soon as the button is clicked, the postback fires and the value is empty. I have tried to create the textbox in a (!isPostBack) but that doesn't seem to work.

protected void Page_Load(object sender, EventArgs e)
{
     Form.Controls.Add(t);
}

protected void Page_PreInit(object sender, EventArgs e)
{
     predictionList = dc.getPredictions(Convert.ToInt32(Session["accountId"]));
     fixtureList = dc.getFixtures();
     t.CssClass = "panel panel-success table table-striped";
     sortLists();
     foreach (Fixture f in newList)
     {
          TableRow tr = new TableRow();
          TableCell tc1= new TableCell();
          TextBox tb1= new TextBox();
          tb1.ID = "tb1";
          tc1.Controls.Add(tb1);
          tr.Cells.Add(tc1);
          t.Rows.Add(tr);
     }
}

Here I add the control, and here I want to process whatever is in the textbox:

protected void btSubmit_Click(object sender, EventArgs e)
{
     foreach (TableRow r in t.Rows)
     {
         string textboxRead= ((TextBox)r.FindControl("tb1")).text;
         int textboxInt = Convert.ToInt32(textboxRead);
     }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Proliges
  • 371
  • 4
  • 26
  • are you getting an error? – user990423 Oct 06 '15 at 12:36
  • No, there is no error. The value just is empty. – Proliges Oct 06 '15 at 12:37
  • In the for loop, you are assigning textbox1 as an ID of all the created textboxes. I do not know if this is related to your problem here, but it might cause another problems. – Eray Balkanli Oct 06 '15 at 12:37
  • @Eray I am aware of the fact that i need to dynamically create textboxes. and also that i need to create dynamic id's. thats not related to the problem. i cannot read the value from a single textbox either. – Proliges Oct 06 '15 at 12:39
  • the object t is not clearly defined in your code, but it is used to reference the text boxes. What sort of type is it? I'm not getting an error using the example shown. – martijn Oct 06 '15 at 13:27
  • Be sure to mark this as answered, if somebody has solved the problem. – James McCormack Oct 07 '15 at 13:04

4 Answers4

0

Could it be that FindControl() is not finding the textbox because it doesn't work recursively?

i.e. You have added the TextBox to a TableCell, but you are performing your FindControl() call on the TableRow, not the TableCell. So either call FindControl() from the Cell or use a recursive version.

For a recursive version of FindControl(), see: Better way to find control in ASP.NET

Community
  • 1
  • 1
James McCormack
  • 9,217
  • 3
  • 47
  • 57
0

Try this:

TextBox tb1;

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        tb1 = ((TextBox)r.FindControl("tb1"));                
    }
}

protected void btSubmit_Click(object sender, EventArgs e)
{
     string textboxRead = tb1.Text; // here you can get the tb1.Text
     int textboxInt = Convert.ToInt32(textboxRead);
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

Hope this will help you.

protected void AddTextBox(object sender, EventArgs e)
{
    int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
    this.CreateTextBox("txtDynamic" + index);
}


private void CreateTextBox(string id)
{
    TextBox txt = new TextBox();
    txt.ID = id;
    pnlTextBoxes.Controls.Add(txt);

    Literal lt = new Literal();
    lt.Text = "<br />";
    pnlTextBoxes.Controls.Add(lt);
}

see this link for details: http://www.aspsnippets.com/Articles/Get-Value-Text-of-dynamically-created-TextBox-in-ASPNet-using-C-and-VBNet.aspx

Neha Thakur
  • 351
  • 1
  • 12
  • 37
0

You need to give the ID's to all of your dynamically created controls. This is mandatory to prevent any ambiguity on post-back.

That includes tr, tc1, tb1 and maybe even t controls.

Also, to find the value, use this snippet:

protected void btSubmit_Click(object sender, EventArgs e)
{
    foreach (TableRow tr in t.Rows)
    {
        var tc1 = (TableCell)tr.FindControl("tc1");
        var tb1 = (TextBox)tc1.FindControl("tb1");
        int textboxInt = Convert.ToInt32(tb1.Text);
     }
}
Dusan
  • 5,000
  • 6
  • 41
  • 58