CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

ASP.NET Control ID and postback

Controls on your webforms all have an ID, like TextBox1, LinkButtonAdd, etc. This ID is not the internal Id used by ASP.NET. In case you have a datagrid with multiple rows, each having a Select link button you would have a problem. In case you have a web usercontrol on your page with a Button1 on it and a Button1 on the hosting page that would result in a conflict as well. The internal name is composed of the control id and the control containing the control.

Internal control names look like

LinkButton1 A link button on the page itself
WebUserControl11:LinkButton1 A link button on a usercontrol
WebUserControl11:DataGrid1:_ctl4:_ctl0   A linkbutton in a datgrid row on an usercontrol

At runtime you can read this Id from the control's ClientID property. It's value is in the request of a postback. In an earlier post I showed how to read it form the _EVENTTARGET form variable to find out which control had caused the postback. As Andrew noted this form variable is empty in case of a button causing the postback and wondered how he still could find out which button had caused the postback.

Well, if you inspect the AllKeys property of the Request.Form you will see that item 3 contains the id of the control posting back But this item3 is only in the collection when a button posts back, not when an asp.net specific control like a linkbutton or dropdownlist caused it. So a better version of my previous post would be

string postbackControlId;

if (Request.Form.AllKeys.GetUpperBound(0) > 2)
   postbackControlId = Request.Form.AllKeys[3];
else
   postbackControlId = Request.Form["__EVENTTARGET"];

// Fiddle the control id out of the string

 


Published May 31 2005, 01:46 AM by pvanooijen
Filed under:

Comments

Eric Wise said:

That's a magic number though. AllKeys[3]...

I'd be wary of using that in an application I wanted to port to 2.0. Have you validated it's the same in both versions?
# May 31, 2005 7:10 AM

pvanooijen said:

This is absolutely 2003 only. Havn't dared to try it under 2005 :) It's all undocumented hacking your way through the page life-cycle.
Which is fun nevertheless.
# May 31, 2005 12:06 PM

pvanooijen said:

It's not much different in 2.0. Except that the parts of the id are now separated by a $ instead of a :
# July 22, 2005 3:35 AM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
I have built a lot of web application with Visual Studio...
# August 3, 2005 3:16 PM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
I have built a lot of web application with Visual Studio...
# August 3, 2005 3:24 PM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
The DataGrid with Visual Studio 2003 is a very nice...
# August 15, 2005 10:43 AM

Peter's Gekko said:

Author: <a href="/blogs/peter.van.ooijen">Peter Van Ooijen</a><br />
The DataGrid with Visual Studio 2003 is a very nice control, however, when it comes to the viewstate and "pushing your app through the wire" it does have some serious drawbacks. Visual Studio 2005 does go a long way towards fixing these issues, but a better general approach to optimizing DataGrid performance is fighting viewstate size.
# August 22, 2005 11:16 AM

Peter's Gekko said:

Author: <a href="/blogs/peter.van.ooijen">Peter Van Ooijen</a><br />
The DataGrid with Visual Studio 2003 is a very nice control, however, when it comes to the viewstate and "pushing your app through the wire" it does have some serious drawbacks. Visual Studio 2005 does go a long way towards fixing these issues, but a better general approach to optimizing DataGrid performance is fighting viewstate size.
# August 22, 2005 11:16 AM

Yitzhak Gootvilig's Blog said:

# August 31, 2005 9:52 AM

Yitzhak Gootvilig's Blog said:

# January 23, 2006 8:20 AM

Onur TOPAL said:

in my codes it always returns "__VIEWSTATE"  i used visual studio 2003

# October 30, 2006 5:32 AM

pvanooijen said:

__viewstate is the id of the hidden control which contains the viewstate.

# October 30, 2006 6:29 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News