Joyin
Joyin

Reputation: 295

What does webform_DoPostBackWithOptions() do?

I have a button declared like this:

<asp:Button id=Send runat="server" EnableViewState="False" 
ToolTip="Email me this report" CssClass="Button" Text="Email me this report">
</asp:Button>

But if I do Inspect Element in browser, it shows like this:

<input type="submit" class="Button" title="Email me this report" 
id="ctl03_Toolbar_Send" onclick="javascript:WebForm_DoPostBackWithOptions(new 
WebForm_PostBackOptions("ctl03$Toolbar$Send","", true, "", "";, false, false))" 
value="Email me this report" name="ctl03$Toolbar$Send">

I wonder where the onclick event comes from? What does it do?

Thanks for your help in advance.

Upvotes: 24

Views: 81723

Answers (4)

user1016327
user1016327

Reputation: 31

Also if there is some validation control on the page this can cause it as well. Set the CausesValidation property of the button to false.

     <asp:button runat="server" ID="test" CausesValidation ="false" />

Upvotes: 1

Mike U
Mike U

Reputation: 729

Understand that there's no such thing as "ASP.NET Controls" in the rendered HTML that the web server outputs in response to a user's request. All the ASP.NET controls do is render some HTML in a way that makes everything work as expected on the server. The Button control is emitting the "onclick" attribute in order to trigger some JavaScript that will result in the form data being posted back to the server (assuming any client side validators don't prevent it).

The reason for using this method to send the data back is, as I just mentioned, to give any client-side script a chance to run first, such as data validation controls that can check and see if any required fields are not filled out.

Basically, unless you're looking to create your own ASP.NET server controls, you don't need to worry too much about exactly what is getting emitted as the ultimate response from IIS. It's good to be familiar with what's happening (and I'm certainly not saying that you shouldn't learn exactly how server controls do their thing), but you don't have to be intimately familiar with every client-side call and parameter that ASP.NET is making in order to get started.

Upvotes: 8

Bhalchandra K
Bhalchandra K

Reputation: 2681

If you set the PostBackUrl property for the Button server control, then it means it is cross page posting and then asp.net framework instead of normal __DoPostBack() adds "WebForm_DoPostBackWithOptions". Check if you have "PostBackUrl" Property set for this button.

<asp:Button id=Send runat="server" EnableViewState="False" PostBackUrl="~/Page2.aspx"
ToolTip="Email me this report" CssClass="Button" Text="Email me this report">
</asp:Button>

If in your case you have not set the "PostBackUrl", then ASP.NET framework also does not add this by default for Button Control, so this means there has to be another control setting the OnClick attribute value probably using following sever side code -

    PostBackOptions myPostBackOptions = new PostBackOptions(this);
    myPostBackOptions.ActionUrl = "Page2.aspx";
    myPostBackOptions.AutoPostBack = false;
    myPostBackOptions.RequiresJavaScriptProtocol = true;
    myPostBackOptions.PerformValidation = true;

    // Add the client-side script to the HyperLink1 control.
    Button1.OnClientClick = Page.ClientScript.GetPostBackEventReference(myPostBackOptions);

Upvotes: 8

motogeek
motogeek

Reputation: 106

Remove "EnableViewState="False" and it should use the default PostBack.

When you set button to not store state, it will use this option instead which is wired to the page_load javascript event differently on the page.

Upvotes: 1

Related Questions