views:

79

answers:

3

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?

+2  A: 

Have you tried:

<button type="button" id="btnBuyCat" name="btnBuyCat" value="Shopping Cart"
title="Click to buy a cat"  onclick="btnBuyCat_Click">Click Me!</button>
Garis Suero
Just tried and didn't work. Worth a try though +1
JohnB
+6  A: 

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.

jball
**`PERFECT`** thanks!
JohnB
+1  A: 

To specify an input control runat=server, you must also specify an id. Also the error you get is probably because of js error. onclick event on a standard html control is assuming a script method define, whereas it seems like you wanted to do a postback type operation. Your option is to define a javascript function according to the method name you give to the onclick event, or use __doPostBack method to explicitly trigger postback

<input type="button" runat="server" id="btnTest" />
Fadrian Sudaman
To include postback behaviour on click you will do <input type="button" runat="server" id="btnTest" onclick="doPostBack('btnTest', '');"/>
Fadrian Sudaman
+1 Fadrian, this is the answer I was expecting. I just didn't realize those finer points about ASP.NET yet. *(hangs head in shame)* However, jball's tip is even more simple for what I am trying to accomplish; thanks though!
JohnB
If I get your suggestion working, I might give you the check mark, because technically you're answer explains why my code wasn't working. But I'm still using jball's because it's easier.
JohnB
No worries. Let me know how you went.
Fadrian Sudaman