Link to home
Start Free TrialLog in
Avatar of Samm1502
Samm1502

asked on

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;
}

Cheers
Sam
Avatar of tetorvik
tetorvik
Flag of Finland image

You can use something like:

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of tetorvik
tetorvik
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Samm1502
Samm1502

ASKER

Cheers!