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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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" />
Method 3
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>
Method 4
You can use:
btnBuyCat.ServerClick += new EventHandler(btnBuyCat_ServerClick);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0