views:

446

answers:

2

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?

A: 

"Probably not", but you're relying on implementation-details of the ASP.NET JavaScript that may change over time.

I'll be honest, I didn't actually realise there was a '<button>' HTML element.

Noon Silk
+3  A: 

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>
Canavar
That's probably the better way of doing it.
Zhaph - Ben Duguid
Beautiful, thank you. I've never actually used `ClientScript.GetPostBackEventReference` nor realized it took options.
Crescent Fresh