GetPostBackEventReference disables Command or Click events
I had this DataList with a simple ItemTemplate which contained a Select button. I wanted to click on the item and select it, without the use of the button. So what I s did is add this in DataList1.OnInit:Panel1.Attributes["onclick"]=Page.ClientScript.GetPostBackEventReference(btnSelect, "");
Nothing was working after that. The select button didn't fire the OnItemCommand event and its ClientID was just btnSelect, not ctl00$ContentPlaceHolder1$UcGroupEdit1$DataList1$ctl00$btnSelect like it should have been. Of course, even considering the possibility of a button firing a command event when there are ten other buttons with the same ID on the page was ridiculous. What has caused this?
Well, I removed the line above and everything worked again. The only solution was to override the user control Render method, then take each item, FindControl the button and the panel, then repeat the same line, with an additional parameter that said to register the control for postback. If I didn't do that, an event validation error would have occurred. Why Render? Because using the last parameter anywhere else causes the "RegisterForEventValidation can only be called during Render();" error.
So
Panel1.Attributes["onclick"]=Page.ClientScript.GetPostBackEventReference(btnSelect, "",true);in the Render event was the only solution. I've spent hours just understanding what is wrong. Grrr!
Update: I think the post Problems when using ClientID or UniqueID before the PreRender event is closer to the problem and its causes.


