OnClick vs OnClientClick for an asp:CheckBox?(OnClick 与 OnClientClick 的 asp:CheckBox?)
问题描述
有谁知道为什么 asp:CheckBox 的客户端 javascript 处理程序需要是 OnClick="" 属性而不是 OnClientClick="" 属性,就像 asp:Button 一样?
Does anyone know why a client-side javascript handler for asp:CheckBox needs to be an OnClick="" attribute rather than an OnClientClick="" attribute, as for asp:Button?
例如,这是有效的:
<asp:CheckBox runat="server" OnClick="alert(this.checked);" />
这没有(没有错误):
<asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />
但这有效:
<asp:Button runat="server" OnClientClick="alert('Hi');" />
这不是(编译时错误):
and this doesn't (compile time error):
<asp:Button runat="server" OnClick="alert('hi');" />
(我知道 Button.OnClick 的用途;我想知道为什么 CheckBox 不能以同样的方式工作......)
(I know what Button.OnClick is for; I'm wondering why CheckBox doesn't work the same way...)
推荐答案
这很奇怪.我检查了 CheckBox 文档页面,上面写着p>
That is very weird. I checked the CheckBox documentation page which reads
<asp:CheckBox id="CheckBox1"
AutoPostBack="True|False"
Text="Label"
TextAlign="Right|Left"
Checked="True|False"
OnCheckedChanged="OnCheckedChangedMethod"
runat="server"/>
如您所见,没有定义 OnClick 或 OnClientClick 属性.
As you can see, there is no OnClick or OnClientClick attributes defined.
记住这一点,我认为这就是正在发生的事情.
Keeping this in mind, I think this is what is happening.
当你这样做时,
<asp:CheckBox runat="server" OnClick="alert(this.checked);" />
ASP.NET 不会修改 OnClick 属性并将其呈现在浏览器上.它将被呈现为:
ASP.NET doesn't modify the OnClick attribute and renders it as is on the browser. It would be rendered as:
<input type="checkbox" OnClick="alert(this.checked);" />
显然,浏览器可以理解OnClick"并发出警报.
Obviously, a browser can understand 'OnClick' and puts an alert.
在这种情况下
<asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />
同样,ASP.NET 不会更改 OnClientClick 属性并将其呈现为
Again, ASP.NET won't change the OnClientClick attribute and will render it as
<input type="checkbox" OnClientClick="alert(this.checked);" />
由于浏览器无法理解 OnClientClick,因此不会发生任何事情.它也不会引发任何错误,因为它只是另一个属性.
As browser won't understand OnClientClick nothing will happen. It also won't raise any error as it is just another attribute.
您可以通过查看呈现的 HTML 来确认上述内容.
You can confirm above by looking at the rendered HTML.
是的,这根本不直观.
这篇关于OnClick 与 OnClientClick 的 asp:CheckBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OnClick 与 OnClientClick 的 asp:CheckBox?


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01