View Single Post
  #4 (permalink)  
Old May 5th, 2005, 03:24 PM
planoie's Avatar
planoie planoie is offline
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

I guess I got confused by the line in your first post: "In my VB.NET app, the button is not in the form."

1) I have a login page with 2 textboxes (user, password), and a button. I debugged the page and looked at the resulting Request.Form collection. Here's what I got.
Request.Form.AllKeys(0): "__EVENTTARGET"
Request.Form.AllKeys(1): "__EVENTARGUMENT"
Request.Form.AllKeys(2): "__VIEWSTATE"
Request.Form.AllKeys(3): "txtUsername"
Request.Form.AllKeys(4): "txtPassword"
Request.Form.AllKeys(5): "cmdLogin"

Item 5 is the button. For the sake of argument, I added a second button "cmdTest" and repeated the process. The only difference was item 5:

Request.Form.AllKeys(5): "cmdTest"

So this behaves just like ASP, which makes sense because it's an HTTP behavior (vs. just .NET) to send the clicked submit button.

2) __doPostBack:
Look at the source code for a page that has some control (besides a button) that does a postback, such as a checkbox, radiobutton, DDL, etc. Even a link button has this, just look in the browser statusbar when you mouse over it.

Whenever you have a control that requires postback (ex. checkbox with autopostback=true) ASP.NET will write out script that contains the __doPostBack function.

function __doPostBack(eventTarget, eventArgument) {
    var theform;
    theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
    theform.__EVENTARGUMENT.value = eventArgument;

That function references a couple other hidden fields in the form: __EVENTTARGET and __EVENTARGUMENT. This is what holds the target and argument for the postback call. For a postback call the client-side unique ID of the control initiating the postback is the target. Argument is optional.

3) GetPostBackEventReference:
From the msdn article: "Obtains a reference to a client-side script function that causes, when invoked, the server to post back to the page."

To get intellisense for this in Visual Studio: Tools-> Options-> Text Editor-> All Languages-> Uncheck "Hide Advanced Members". (There are a host of goodies that are hidden in VS by default.)

The reason you want to use this method is that (presumably) the __doPostBack method is generated by .NET so if you hard coded it you could get into trouble. Plus, you don't need to do anything special for it, just pass in the control for which you'd like the postback reference and let .NET create the piece of JS for you.