Javascript <input type="button" runat="server" /> 在 ASP.NET 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3445515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<input type="button" runat="server" /> won't work in ASP.NET
提问by JohnB
Okay, this may seem silly, but on an ASP.NET .ascx control, I'm trying to use:
好的,这可能看起来很傻,但是在 ASP.NET .ascx 控件上,我尝试使用:
<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)
呈现以下 HTML: (忽略命名容器 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".
这主要是罚款,但我想input type="button"不会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:
并获取此 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.
不幸的是,呈现的按钮不起作用。此外,我什至试图input type="submit"只是检查,但除非我使用<asp:Button>我无法让它工作。我确定它与 JavaScript 有关系。
Is there a way to use the regular HTML button markup and a runat="server"in ASP.NET?
有没有办法runat="server"在 ASP.NET 中使用常规的 HTML 按钮标记和 a ?
回答by jball
What you're missing is the UseSubmitBehaviorattribute, e.g.,
你缺少的是UseSubmitBehavior属性,例如,
<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.
这将为您提供一个常规按钮,而不是提交按钮。
回答by Fadrian Sudaman
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
要指定输入控件 runat=server,您还必须指定一个 id。你得到的错误也可能是因为 js 错误。标准 html 控件上的 onclick 事件假设脚本方法定义,而您似乎想要执行回发类型操作。您的选择是根据您为 onclick 事件提供的方法名称定义一个 javascript 函数,或者使用 __doPostBack 方法显式触发回发
<input type="button" runat="server" id="btnTest" />
回答by Garis M Suero
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>
回答by Garis M Suero
You can use:
您可以使用:
btnBuyCat.ServerClick += new EventHandler(btnBuyCat_ServerClick);

