简体   繁体   English

ASP.Net文本框onblur事件

[英]ASP.Net textbox onblur event

I have a textbox, whose values I need to validate (if value of textbox is 50, then display message in lblShowMsg) when the user tabs out of the textbox (onBlur event). 我有一个文本框,当用户选中文本框(onBlur事件)时,我需要验证其值(如果textbox的值为50,则在lblShowMsg中显示消息)。 I can't seem to get the syntax right. 我似乎无法正确使用语法。

I have this code on my pageload event: 我在pageload事件中有这个代码:

protected void Page_Load(object sender, EventArgs e)
{
    txtCategory.Attributes.Add("onblur", "validate()"); 

}

But I can't seem to get the javascript code correct. 但我似乎无法得到正确的JavaScript代码。 Any suggestions? 有什么建议?

In the Code behind: (VB.NET) 在代码背后:(VB.NET)

On the page load event 在页面加载事件

txtAccountNumber.Attributes["onBlur"] = "IsAccNumberValid(" & txtAccountNumber.ClientID & ")";

Where txtAccountNumber is the ID of the TextBox in the markup page and you pass the ClientID of the textbox because JavaScript is client side not server side. 其中txtAccountNumber是标记页面中TextBox的ID,并且您传递文本框的ClientID,因为JavaScript是客户端而不是服务器端。 And now in the markup page(.aspx) have this javascript in the head section of the page: 现在在标记页面(.aspx)中,在页面的head部分中有这个javascript:

<script type="text/javascript">                     
function IsAccNumberValid(txtAccountNumber) {                                             
    if (txtAccountNumber.value.length < 6) {    
                      alert(txtAccountNumber.value);
            }    
        }    
</script>

Is that the actual code in your Page_Load? 这是你的Page_Load中的实际代码吗? You need to use the name of the control, and not the type name for TextBox. 您需要使用控件的名称,而不是TextBox的类型名称。 For example, you may want to try: 例如,您可能想尝试:

 textBox1.Attributes.Add("onblur", "validate();");

where "textBox1" is the ID you assigned to the textBox in your markup instead. 其中“textBox1”是您在标记中分配给textBox的ID。

Also, from Javascript, it's very possible that the ID of the textBox has changed once it gets rendered to the page. 此外,从Javascript开始,一旦将textBox的ID呈现给页面,它的ID很可能已经改变。 It would be better if you would pass the control to the validate function: 如果您将控件传递给validate函数会更好:

function validate(_this)
{
    if (_this.value == "50")
        // then set the ID of the label.  
}

Then you would set the attribute like this: 然后你可以像这样设置属性:

textBox1.Attributes.Add("onblur", "validate(this);");

Lastly, I would strongly recommend using the JQuery library if you're doing anything in Javascript. 最后,如果您在Javascript中执行任何操作,我强烈建议您使用JQuery库。 It will make your life 10x easier. 它将使您的生活更轻松10倍。

This works. 这有效。

Textbox1.Attributes.Add("onblur","javascript:alert('aaa');"); Textbox1.Attributes.Add( “的onblur”, “JavaScript的:警报( 'AAA');”);

Make sure that functiion lies in the script part of the page. 确保功能位于页面的脚本部分。


My page 我的页面

<script type="text/javascript">
    function Validate() {

        alert('validate');

    }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

code behind 代码背后

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Textbox1.Attributes.Add("onblur","Validate();");
        }
    }

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

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