[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

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
0
Samm1502
Asked:
Samm1502
  • 2
1 Solution
 
tetorvikCommented:
You can use something like:

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

Open in new window

0
 
tetorvikCommented:
Tiny correction to my previous post:
value = Request["txtSearch" + i.ToString()];  //Removing the additional space from "txtSearch "
0
 
Samm1502Author Commented:
Cheers!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now