简体   繁体   English

将javascript添加到ASP.NET文本框控件的OnBlur属性

[英]Adding javascript to the OnBlur property of an ASP.NET text box control

Is there a way to specify some JavaScript to execute on the OnBlur event of an ASP.NET text box? 有没有办法指定一些JavaScript在ASP.NET文本框的OnBlur事件上执行? It seems to me like if I add any event handlers to the TextBox object they will just cause postbacks to the server isntead of doing what I want. 在我看来,如果我向TextBox对象添加任何事件处理程序,它们只会导致回发到服务器而不是做我想要的。 Basically, I just want to be able to have the textbox be rendered in this HTML: 基本上,我只是希望能够在此HTML中呈现文本框:

<INPUT type="text" onblur="alert('1234')" />

Thanks! 谢谢!

Could also go for: 也可以去:

<asp:TextBox runat="server" onblur="Javascript:alert('1234');" />

if you dont feel like setting it up in the codebehind. 如果你不想在代码隐藏中设置它。

Im guessing the reason why you end up with postbacks, must be because you have set AutoPostBack on the textbox to true. 我猜你最终回发的原因,必定是因为你已经将文本框上的AutoPostBack设置为true。 That makes the textbox postback when the client-side onchange event is triggered. 这会在触发客户端onchange事件时使文本框回发。 Switch it to false, and it will act as a normal input-element. 将其切换为false,它将作为普通的输入元素。

在您的代码隐藏中,添加以下内容:

myTextBox.Attributes.Add("onblur","alert('1234');");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM