How to get value from dynamically created textbox on postback referencing textbox using a string

Hi there

During my initial page creation I generate a row of textboxes as the first row of my GridView that the user can enter search criteria in.  Now when the user presses the Search button a postback occurs and I then need to read the values from my textboxes to perform the search and only return the requested resuts to the GridView

Now because the textboxes don't get created until the GridView.DataBind() call as the first empty row gets inserted into the DataTable once the results have been obtained and the search boxes are added to the empty row during the OnRowCreated event.  This means that when I try and get my values from the textboxes they don't yet exist so can't do

value = ((TextBox)row.Cells[i].Controls[0].Text;

I do know is that the search boxes were previously named txtSearch0, txtSearch1 etc.

so I could say value = txtSearch0.Text;

and the value will be obtained from ViewState but is there a way of doing something like this:

for(int i = 0; i < noSearchBoxes; i++)
    value = txtSearch + ["i"].Text;

Who is Participating?
Tiny correction to my previous post:
value = Request["txtSearch" + i.ToString()];  //Removing the additional space from "txtSearch "
You can use something like:

for(int i = 0; i < noSearchBoxes; i++)
    value = Request["txtSearch " + i.ToString()];

Open in new window

Samm1502Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.