Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostBackUrl not available on Html Buttons

Tags:

We're currently performing a cross-page postback using the PostBackUrl of an asp:Button:

<asp:Button runat="server" PostBackUrl="processing.aspx" />

which generates this javascript onclick stuff:

<input type="submit" name="ctl03"
    onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl03', ', false, ', 'processing.aspx', false, false))" />

We would like to switch it to use a plain ol' <button runat="server"> (easier to style) however PostBackUrl is not supported on them.

So I thought: what if simply use said JavaScript in my <button> element?

<button runat="server" name="ctl03"
    onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl03', '', false, '', 'processing.aspx', false, true))">
</button>

And waddayaknow, it works.

Has anyone ever seen this done before? What harm will come to me or my children if I proceed with this?

like image 670
Crescent Fresh Avatar asked Aug 18 '09 02:08

Crescent Fresh


1 Answers

Interesting question, I just looked ClientScript's GetPostBackEventReference method and here what I get :

Button at ASP.NET page :

<button id="Button2" runat="server" name="Button2"></button>

At code-behind :

PostBackOptions postBackOptions = new PostBackOptions(Button2);
postBackOptions.ActionUrl = "processing.aspx";
Button2.Attributes.Add("onclick", 
    ClientScript.GetPostBackEventReference(postBackOptions));

Rendered result :

<button id="Button2" name="Button2" 
    onclick="WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Button2&quot;, &quot;&quot;, false, &quot;&quot;, &quot;processing.aspx&quot;, false, true))">
</button>
like image 137
Canavar Avatar answered Oct 04 '22 20:10

Canavar