Tuesday, February 24, 2009

Control that caused the post-back

string EventTarget = Request[”__EVENTTARGET”] as string;
if (EventTarget==null) EventTarget=””;
if (EventTarget.IndexOf(“LbtnBeSecond”) > -1)
{
}

Basically, Request[”__EVENTTARGET”] tells you which control caused the post-back.

--

Request[”__EVENTTARGET”] doesn’t return a value when the form was submitted by a button
In this case, one can find the button-name from the Request object (like in the old ASP days).

--

Post-backs by buttons (‘submit buttons’) don’t set eventtarget.
Only controls that use the __doPostback() method set it.

--

ASP buttons render to HTML submit buttons.

In this case
<asp:button id=”btnSubmitSearch” runat=”server” Text=”Search” cssclass=”button” CausesValidation=”False” />

Renders to
<input type=”submit” name=”Module36:btnSubmitSearch” value=”Search” id=”Module36_btnSubmitSearch” class=”button” />

Yes, JS and individual solutions are possible but FindTargetControl would essentially fail for any submit/ASP button.

Instead, I can loop through the Request object if Request[”__EVENTTARGET”] is blank.
It’ll be a little expensive but only for buttons. I’ll go with this approach unless you feel there’ll be any problems.

No comments:

Post a Comment