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

Posted on 2009-04-20
Last Modified: 2012-06-21
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;

Question by:Samm1502
    LVL 9

    Expert Comment

    You can use something like:

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

    Open in new window

    LVL 9

    Accepted Solution

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

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now