开发者

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

开发者 https://www.devze.com 2023-01-10 17:39 出处:网络
Okay, this may seem silly, but on an ASP.NET .ascx control, I\'m trying to use: <input type=\"button\" runat=\"server\" />

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:

<i开发者_如何学编程nput 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?


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.


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" />


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>


You can use:

btnBuyCat.ServerClick += new EventHandler(btnBuyCat_ServerClick);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号