This post originated from an RSS feed registered with .NET Buzz
by Peter van Ooijen.
Original Post: ASP.NET Control ID and postback
Feed Title: Peter's Gekko
Feed URL: /error.htm?aspxerrorpath=/blogs/peter.van.ooijen/rss.aspx
Feed Description: My weblog cotains tips tricks and opinions on ASP.NET, tablet PC's and tech in general.
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