Reputation: 116980
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?
Upvotes: 5
Views: 3330
Reputation: 48088
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("Button2", "", false, "", "processing.aspx", false, true))">
</button>
Upvotes: 5
Reputation: 55082
"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.
Upvotes: 0