Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<input type="button" runat="server" /> won't work in ASP.NET

Okay, this may seem silly, but on an ASP.NET .ascx control, I'm trying to use:

<input type="button" runat="server" />

instead of:

<asp:Button runat="server" />

And it's not working for me. This code:

<asp:Button id="btnBuyCat" runat="server" Text="Buy Cat"
ToolTip="Click to buy a cat" OnClick="btnBuyCat_Click" EnableViewState="false" />

renders the following HTML: (ignoring naming containers btw)

<input type="submit" id="btnBuyCat" name="btnBuyCat" value="Shopping Cart"
title="Click to buy a cat" />

That's mostly fine, except I want input type="button" not input type="submit".

I tried this code:

<input type="button" id="btnBuyCat" name="btnBuyCat" runat="server"
value="Buy Cat" title="Click to buy a cat" onclick="btnBuyCat_Click"
enableviewstate="False" />

and get this HTML:

<input type="button" id="btnBuyCat" name="btnBuyCat"" value="Buy Cat"
title="Click to buy a cat" onclick="btnBuyCat_Click" />

Unfortunately the rendered button does not work. Also, I even tried input type="submit" just to check, but unless I use the <asp:Button> I can't get it to work. I'm sure it has something to do with the JavaScript.

Is there a way to use the regular HTML button markup and a runat="server" in ASP.NET?

like image 285
JohnB Avatar asked Aug 10 '10 01:08

JohnB


1 Answers

What you're missing is the UseSubmitBehavior attribute, e.g.,

<asp:Button id="btnBuyCat" runat="server" Text="Buy Cat" 
UseSubmitBehavior="False" ToolTip="Click to buy a cat" 
OnClick="btnBuyCat_Click" EnableViewState="false" />

This will give you a regular button, not a submit button.

like image 100
jball Avatar answered Sep 30 '22 13:09

jball