OnClick和OnClientClick为asp:复选框? [英] OnClick vs OnClientClick for an asp:CheckBox?

查看:162
本文介绍了OnClick和OnClientClick为asp:复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么asp:CheckBox的客户端JavaScript处理程序需要是一个OnClick =属性,而不是一个OnClientClick =属性,如asp:Button?



例如,这工作原理:

 < asp:CheckBox runat =serverOnClick =alert (this.checked); /> 

这没有(没有错误):

 < asp:CheckBox runat =serverOnClientClick =alert(this.checked); /> 

但这适用于:

 < asp:Button runat =serverOnClientClick =alert('Hi'); /> 

这不会(编译时出错):

 < asp:Button runat =serverOnClick =alert('hi'); /> 

(我知道Button.OnClick是为什么;我想知道为什么CheckBox不工作同样的方式...)

解决方案

这很奇怪。我查看了复选框文档页,其中显示

 < asp:CheckBox id =CheckBox1
AutoPostBack =True | False
Text = Label
TextAlign =Right | Left
Checked =True | False
OnCheckedChanged =OnCheckedChangedMethod
runat =server/>

可以看到,没有定义OnClick或OnClientClick属性。



记住这一点,我想这是发生了什么。



当您这样做时,

 < asp:CheckBox runat =serverOnClick =alert(this.checked); /> 

ASP.NET不修改OnClick属性,并在浏览器上呈现。它将呈现为:

 < input type =checkboxOnClick =alert(this.checked); /> 

显然,浏览器可以理解OnClick



在这种情况下

 < asp:CheckBox runat = serverOnClientClick =alert(this.checked); />再次,ASP.NET不会更改OnClientClick属性,并将其转换为

 < input type =checkboxOnClientClick =alert(this.checked); /> 

由于浏览器不会理解OnClientClick会发生什么。它也不会引发任何错误,因为它只是另一个属性。



您可以通过查看呈现的HTML确认上面。



是的,这不直观。


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?

For example, this works:

<asp:CheckBox runat="server" OnClick="alert(this.checked);" />

and this doesn't (no error):

<asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />

but this works:

<asp:Button runat="server" OnClientClick="alert('Hi');" />

and this doesn't (compile time error):

<asp:Button runat="server" OnClick="alert('hi');" />

(I know what Button.OnClick is for; I'm wondering why CheckBox doesn't work the same way...)

解决方案

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

As you can see, there is no OnClick or OnClientClick attributes defined.

Keeping this in mind, I think this is what is happening.

When you do this,

<asp:CheckBox runat="server" OnClick="alert(this.checked);" />

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

Obviously, a browser can understand 'OnClick' and puts an alert.

And in this scenario

<asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />

Again, ASP.NET won't change the OnClientClick attribute and will render it as

<input type="checkbox" OnClientClick="alert(this.checked);" />

As browser won't understand OnClientClick nothing will happen. It also won't raise any error as it is just another attribute.

You can confirm above by looking at the rendered HTML.

And yes, this is not intuitive at all.

这篇关于OnClick和OnClientClick为asp:复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆