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:
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
Update: I think the post Problems when using ClientID or UniqueID before the PreRender event is closer to the problem and its causes.
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.
Comments
I don't know what is the connection between this entry and AzamSharp or GridViewGuy. Certainly I didn't have any of them in mind when I wrote it :) As for the web service called asynchronously... My guess is that either you or a component you are using try to do a RegisterForEventValidation in some place other than Render. I suggest you check the code and use Reflector on any code you don't own (even on the Microsoft DLLs which are not obfuscated in any way) and see what is causing the problem. Other than that, you would have to detail more the bug, maybe contact me to give me the code and let me find a solution.
SideriteNot trying to post off comment, but it seems like must of these errors for "RegisterForEventValidation can only be called during Render();" deal with AzamSharp/GridViewGuy. I get this error after calling an web service asynchronously. Haven't found a solution yet.
Anonymous