What Control Caused the PostBack?

  • Thread starter Thread starter Jeff Voigt
  • Start date Start date
J

Jeff Voigt

Is there any way to dynamically get the name of the control that caused the
postback? Since SmartNav is not working for me I'm trying to implement a way
to scroll to the control that caused the post back so the user does not have
to scroll all the time.

I got the script working great and everything else, but I want to be able to
tell the name of the control that posted back. That way we don't have to
hard code much at all.

This is what I have implemented so far:

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

' automatically scroll to the object that posted back (if applicable)

If Page.IsPostBack > 0 Then

RegisterStartupScript("scroll", "<script language=javascript>" +
_scrollToObjectPath + _scrollToObjectName + ".scrollIntoView();</script>")

End If



End Sub


Thanks in advance,
- Jeff
 
Jeff Voigt said:
Is there any way to dynamically get the name of the control that caused the
postback? Since SmartNav is not working for me I'm trying to implement a way
to scroll to the control that caused the post back so the user does not have
to scroll all the time.

Jeff, you could look at the value of the __EVENTTARGET hidden field via
Request.Form["__EVENTTARGET"].

You should note that although this will work in many cases, it won't work
for all. ASP.NET searches the control tree looking for a control with its
ClientID equal to __EVENTTARGET and implementing IPostBackEventHandler. If
found, it will pass __EVENTTARGET and __EVENTARGUMENT to this controls
RaisePostBackEvent method.

Usually, __EVENTTARGET is the ClientID of a single control in a single place
on the page. But a composite control may have many child controls which can
post back, and __EVENTTARGET may only name a single hidden field located in
front of all of those child controls. The composite control might then use
__EVENTARGUMENT to distinguish which child control caused the postback, but
you would be unable to do so.

Another issue would be the case where a button causes the postback, but that
it is "clicked" via its accessKey attribute. This button might not be
anywhere near where the user was working when he pressed the access key.

Is there anything in the DOM which would give you a better idea of "what
part of the window is visible right now"? If so, you might capture onsubmit
and cause that information to be sent to the host, which could send it back
to the page in the response. Code in the onload handler on the page might
then be able to restore that information.
 
You may be able to get it from Request.Form("__EVENTTARGET"), but AFAIK
that's only generated by asp.net if one of your controls, such as a
combobox, has it's AutoPostBack property set to True. I'm not 100% sure on
that.
 
Back
Top