?
Solved

How to get form item without specifying full client ID

Posted on 2009-04-20
3
Medium Priority
?
373 Views
Last Modified: 2012-05-06
Hi

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];
   blah.....
}

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?

Thanks
Sam
 
0
Comment
Question by:Samm1502
3 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 24185736
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:

http://www.eggheadcafe.com/articles/extendtextboxviewstate.asp?qid=21a

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

Ghost
0
 
LVL 9

Accepted Solution

by:
tetorvik earned 2000 total points
ID: 24185893
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";
            myPanel.Controls.Add(a);
            ClientIdList.Add(a.ClientID);
 
            TextBox b = new TextBox();
            myPanel.Controls.Add(b);
            b.ID = "bID";
            ClientIdList.Add(b.ClientID);
 
            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

0
 

Author Closing Comment

by:Samm1502
ID: 31572286
Thanks a million.  Can't create text boxes till later in the game so this is perfect.

Cheers
Sam
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 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