CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

Page.GetPostBackEventReference()

Interesting find at work today.  One of my developers had two asp:image controls positioned to the right and left of a button.  The developer wanted on the click of an image to call the button postback.  I had no idea that the GetPostBackEventReference(control) even existed until Brendan pointed me to it (thanks buddy).

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            imgLeft.Attributes.Add("onclick", Page.GetPostBackEventReference(myButton))
            imgRight.Attributes.Add("onclick", Page.GetPostBackEventReference(myButton))
        End If
    End Sub

Is this "Coding Better"?  Hell if I know, it seems to work though.


Published Apr 27 2005, 08:50 AM by Eric Wise
Filed under:

Comments

pvanooijen said:

Beautifull :)
# April 27, 2005 1:12 PM

guidmaster said:

Man l
# April 27, 2005 5:18 PM

Klok said:

Thanks for the tip :)
# April 27, 2005 10:56 PM

Michael Freidgeim said:

I have a custom control that shows hyperlink next to image that when clicked, will open the same URL.
See http://geekswithblogs.net/mnf/articles/59276.aspx.
But this solution is simpler if you want to do it just in a few places.
# November 6, 2005 6:38 PM

TWilson said:

The related GetPostBackClientHyperlink(...) method can be used to create the NavigateURL for hyperlinks in a DataGrid template column, for example.

Used this way, the RaisePostBackEvent method can be handled in the page or containing user control and the hyperlinks will work even with the VIEWSTATE disabled in the datagrid.

This discovery allowed me to drop a couple of custom controls that more-or-less emulated the __dopostback javascript postback, with the added bonus that now the events fire when expected, and a lot less code to maintain.
# December 7, 2005 5:21 PM

Fabian said:

Thanks
# April 24, 2006 11:28 AM

Steve said:

I am having a little trouble figuring out what to put in the argument parameter. I want to perform the month selection postback of the calendar control. I looked at the source and I can do this.

Calendar1.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Calendar1, "R255731"));

The argument works for the current month, but if I change months the argumant changes and I get an error. that function is great instead of hard coding that long name with the $'s, but without knowing how to generate the argument, it's not useful in my case.

# January 18, 2007 2:57 PM

Eric Wise said:

Ew man,

I'm not sure on that one, but can I politely suggest ditching the craptastic microsoft default calendar control for something like Peter's Date Package?

# January 23, 2007 1:55 PM

John said:

Steve,

I am not sure if this will point you in the right direction.

But I am currently using this method to invoke server side function via client side javascript. I have a textbox that has "PostBack" set to "false", and use javascript

to capture user keycode. Whenever a "Enter" key is pressed, I will manually invoke a postback. And process it on server side.

I do it this way:

1) Add a

   GetPostBackEventReference(TextBox1,"");

   in the OnInit block.

This is not for postback purpose, but adding this will make .NET to generate a "__doPostBack" client side javascript.

2) Capture user "Enter" key press keycode, and manually cause postback.

   "__doPostBack('TextBox1', 'AutoNumber')".

3)  In code behind, use

   //Get the Control Name you use in the __doPostBack, in this case it is "TextBox1"

   Request.Params.Get("__EVENTTARGET"); //This will return "TextBox1"

   //Get the argument you use in the __doPostBack, in this case it is "AutoNumber"

   Request.Params.Get("__EVENTARGUMENT"); //This will return "AutoNumber"

4) In code behind pass these two parameter "TextBox1" and "AutoNumber" to  your function for further processing. In my case, I will generate the number if the parameter passed to my code behind function is "TextBox1" and "AutoNumber".

I hope this helps.

# January 31, 2007 11:05 AM

Noha Mohsen said:

I need a confirmation message to be added on clicking on a button in specipic cases only....

and he clicked okey,i need a certain action to occur.....

If you know how this could be done...please help me....

Thanks ,

# February 24, 2007 10:42 AM

alex stephens said:

try this, it runs from a button control on my page but you can just change the controls name:

StringBuilder sb = new StringBuilder();

sb.Append("<script language=JavaScript>");

sb.Append("var con = confirm('It appears that you have just caused a problem'); ");

sb.Append("if(con){");

//get postback ref to control

sb.Append(Page.GetPostBackEventReference(ButtongotoViewDetails));

sb.Append("}else{alert('cancel clicked');}");

sb.Append("</script>");

Page.RegisterStartupScript("myscript", sb.ToString());

# March 6, 2007 8:23 AM

secured email said:

I also wasnt aware of the GetPostBackEventReference function. The code produced in the background is just to messy, so I never paid attention until I had to disable a button so the user could not click twice to start over the process on the server. The classic application would be a a payment button where when the user clicks twice she is billed twice. GetPostBackReference did the Job.

btnBack.Attributes.Add("onclick", GetPostBackEventReference(btnBack) & ";this.value='Please wait...';this.disabled = true;")

Nice hu :)

# March 24, 2007 5:43 AM

mitra said:

good one

# April 18, 2007 7:36 AM

Bob said:

I think your friend's solution works because he disables button before and he avoids the intrinsec autoPOST of the object imagebutton. (the imagebutton inherits from basebutton of html wich generate a post when clicked)

if you try with image

(not button or imagebutton and obviously adding getpostbackeventreference to attribute "onclick" in substituton of button.OnClientClick) you can get same result with 1 only postback and without disabling the control.

# June 22, 2007 6:10 AM

Janette said:

I also am using something similar to

btnBack.Attributes.Add("onclick", GetPostBackEventReference(btnBack) & ";this.value='Please wait...';this.disabled = true;")

to disable an image button so I dont allow the user to submit an order more than once on my site.  What I cant figure out is - what if there is no OnClick() method defined in the markup for this imagebutton?  What I see is that my page is posted back and I lose all of my data on the page.  Is there a way to check for this condition before I do the GetPostBackEventReference()?  I have searched everywhere and cannot find an answer.

# September 20, 2007 11:39 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!