Link to home
Start Free TrialLog in
Avatar of rkekos
rkekos

asked on

What is the _doPostBack event?

I see this in a lot of .NET web applications and was wondering what this is.  Sounds stupid but my web application is working just fine but I don't have any of those on my button controls, for example.  What is this function and how can I use it?
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

it causes the web page to post back with some magic set to let ASP.NET know which event occurred.

http://www.sitepoint.com/article/driven-asp-net-development-c/5 is a good comparison article between the two methods
In simple terms, Webform is posted to itself as a result of an event. For example: ListboxItem is selected. Interesting thing about PostBack events is that they are occured on serverside as compare to the javascript/vbscript events that occurs on client side like SelectedIndexChanged etc.

Best, Nauman
Avatar of rkekos
rkekos

ASKER

I understand that but I'm not seeing why I wouldn't just dynamically add a server side click event to a web control.  Why would I use the _doPostBack function in .NET?  I think I read somewhere is it's because only Buttons and ImageButtons can cause a post back.
its because you need to force a submit to the browser.
Avatar of rkekos

ASKER

gregoryyoung, thanks.  I can't believe I didn't get it before but now I get it.  Basically you would use this functionality if you need to do an immediate post back rather then doing the desired tasks then waiting until the commit (post back) button was clicked.  Now that I understand the concept, how do I use it.

Say for example I have a normal link that I wish to cause a post back, when clicked, to execute a desired function.  How would I do this?
should call the Page.GetPostBackEventReference(Me, "") function, which returns the string of the method name. This is in case it changes to another name in the future.

javascript:__doPostBack('controID','parameters')  to call a control postback with parameters ... javascript:__doPostBack('','')
will jsut do a postback (pageload)
Avatar of rkekos

ASKER

Ok I got it working just fine on a button.  In this case I want to execute a function via a HyperLink web control.  The problem is that the GetPostBackClientHyperlink() wants the name of the control that will execute the post back.  Well in the case of a button it's easy since a button has the .Click event you can add to but in the case of a HyperLink control there is not one.  How do I create a link that will launch the myFunction() in my codebehind?

protected System.Web.UI.WebControls.HyperLink hlTest;
hlTest.NavigateUrl = Page.GetPostBackClientHyperlink( this.hlTest, "" );

That doesn't work but this does with a button because of the click event:

protected System.Web.UI.WebControls.Button btnTest;
btnTest.Attributes.Add( "onClick", GetPostBackEventReference( this.btnTest ) );
btnTest.Click += new EventHandler( btnTest_Click );

Thanks for the continued help gregoryyoung!
you can jsut turn on autopostback ... much easier ... then use addhandler in your code behind :)
or += eventhandler (sorry flipping back and forth VB + V#)
Avatar of rkekos

ASKER

The AutoPostBack attribute only exists on the TextBox, CheckBox, RadioButton, CheckBoxList, RadioButtonList, DropDownList, and ListBox controls.  I need a hyperlink to execute a function.  You said I could also possibly use an EventHandler but where on the Label or HyperLink control.  It has no .Click property.
forgot html controls ...

its a hack check here (it does it) http://www.devasp.net/Net/SampleChapters/6934/6934_7.asp
Avatar of rkekos

ASKER

Turns out I can just use the LinkButton control it's much more quick then what I was trying to do.

I still have one last problem though, sorry! :-)

I'm trying to figure out how to use the parameters function of the __doPostBack.  I've got it executing my desired function but I need to pass a variable to that function.  My question now is twofold.  1.  What is the syntax for the parameters section (__doPostBack( 'btnControl', 'myParam???' ).  Second how do I get this information from my codebehind.  The functions second param looks like "EventArgs e".  How do I get my param set in the javascript with the "e" varaible?
honestly I have never tried (I set form variables and read off them)

BUT !!

I can take a look at how MS does it ... They simply set the field ... I beleive it can only contain 1 parameter (hence some of the oddity in editable datagrids)
Avatar of rkekos

ASKER

I think I've seen them with a dollar sign in them (example test$Me) or something like that.  I was just going to set the variable via a hidden form field as well but would rather use that built in functionality.  Looks like I have to cast the EventArgs variable to something useful like CommandEventArgs or something.  Shoot!  Stuck again .... :-)
ah thats the EVENT TARGET I believe ... the $ get translated into : ... they represent the full name of the control (including control containers etc which avoid naming conflicts)
Avatar of rkekos

ASKER

Does anyone have any type of example or link I can get more information on passing a variable to the __doPostBack javascript function?
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial