How to get form item without specifying full client ID

Posted on 2009-04-20
Last Modified: 2012-05-06

My Asp.NET page builds various things dynamically includinh some text boxes for the user to enter search criteria.

Now when the user clicks on the search button it causes a postback and the values stored in
txtSearch0 to txtSearchn are posted back but the text boxes they belong to no longer exist because they were created dynamically and haven't yet been re-created so I know I can access the values of the text boxes like this:

for(int i = 0; i < Headers.Length; i++)
   value = Request.Form["ctl00$ContentPlaceHolder1$MyGridView$ctl02$txtSearch" + i];

but I don't want to have to put the ctl00$ContentPlaceHolder1$MyGridView$ctl02$ specifier on there first I just want to user "txtSearch" + i.  Is this possible? Or is there another way to get these values?

Question by:Samm1502
    LVL 13

    Expert Comment

    If you re-create the text boxes before the page_load then you'll be good to go.

    you'll need to re-create them on init. You can google init dynamic control re-creation for more info.

    a couple good urls:

    If you don't want to re-create the controls then it will be impossible to reference them by: "txtSearch" + i.

    LVL 9

    Accepted Solution

    You can store client ids of dynamically create controls into viewstate. When doing a postback take the clientIds of dynamically created controls from Viewstate like

    //Creating dynamic controls
                List<string> ClientIdList = new List<string>();
                TextBox a = new TextBox();
                a.ID = "aID";
                TextBox b = new TextBox();
                b.ID = "bID";
                ViewState.Add("DynamicControlClientIds", ClientIdList);
    //Getting values from dynamically created controls 
                List<string> ClientIdList = ViewState["DynamicControlClientIds"] as List<string>;
                string strvalue;
                foreach (string clientId in ClientIdList2)
                    strvalue = Request[clientId];

    Open in new window


    Author Closing Comment

    Thanks a million.  Can't create text boxes till later in the game so this is perfect.


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    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…
    This video discusses moving either the default database or any database to a new volume.

    733 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

    22 Experts available now in Live!

    Get 1:1 Help Now