为什么onTextChanged无法在ASP.net中工作[重复]

时间:2022-08-25 17:35:09

This question already has an answer here:

这个问题在这里已有答案:

I have the following textbox in my ASP.net page:

我的ASP.net页面中有以下文本框:

<asp:TextBox OnTextChanged="maxchar" ID="txtName" Width="95%" runat="server"></asp:TextBox>

The following in my code-behind:

我的代码隐藏中的以下内容:

protected void maxchar(object sender, EventArgs e)
{
    if (txtName.Text.Length > 25)
    {
        lblIsValid.Text = "only 25 characters allowed";
    }
}

When I enter text in the textbox and exceed the 25 character nothing is displayed in the label. How can I fix it?

当我在文本框中输入文本并超过25个字符时,标签中不显示任何内容。我该如何解决?

4 个解决方案

#1


3  

In this case, for the max lenght, you should use the property of asp:textbox called MaxLength="25".

在这种情况下,对于最大长度,您应该使用名为MaxLength =“25”的asp:textbox属性。

But, if you want to send a message to user, i recomend to use JQuery, it's easy:

但是,如果你想向用户发送消息,我建议使用JQuery,这很容易:

$('#txtName').keyDown(function(){
     if($(this).length >= 25){
        //you alert
     }; 
});

#2


1  

OnTextChanged only occurs during a post back MSDN reference.

OnTextChanged仅在回发MSDN引用期间发生。

You would need to add javascript to do what you want to do, since javascript runs locally it doesn't require a postback.

你需要添加javascript来做你想做的事情,因为javascript在本地运行它不需要回发。

If you don't want javascript you will have to wait for the user to submit the page, it is an unavoidable restriction of javascript-free webpages.

如果你不想要javascript,你将不得不等待用户提交页面,这是一个不可避免的无javascript网页限制。

#3


1  

Pretty sure that should be TextChanged and that OnTextChanged should be a JavaScript call (I believe). Either way, you should do this in JS. Calling to the server on every text changed event should be avoided.

很确定应该是TextChanged并且OnTextChanged应该是一个JavaScript调用(我相信)。不管怎样,你应该在JS中这样做。应避免在每个文本更改事件上调用服务器。

In jQuery you can do

在jQuery中你可以做到

$('#txtTime').changed(function(){
    // do event logic here
});

Now hopefully you're using jQuery. If not I can update for vanilla JS.

现在希望你使用jQuery。如果没有,我可以更新为香草JS。

#4


0  

Have you tried setting autopostback="true" in the asp:TextBox declaration? A quick search turned up this article as well, maybe it will be some help.

你试过在asp:TextBox声明中设置autopostback =“true”吗?快速搜索也出现在这篇文章中,也许它会有所帮助。

#1


3  

In this case, for the max lenght, you should use the property of asp:textbox called MaxLength="25".

在这种情况下,对于最大长度,您应该使用名为MaxLength =“25”的asp:textbox属性。

But, if you want to send a message to user, i recomend to use JQuery, it's easy:

但是,如果你想向用户发送消息,我建议使用JQuery,这很容易:

$('#txtName').keyDown(function(){
     if($(this).length >= 25){
        //you alert
     }; 
});

#2


1  

OnTextChanged only occurs during a post back MSDN reference.

OnTextChanged仅在回发MSDN引用期间发生。

You would need to add javascript to do what you want to do, since javascript runs locally it doesn't require a postback.

你需要添加javascript来做你想做的事情,因为javascript在本地运行它不需要回发。

If you don't want javascript you will have to wait for the user to submit the page, it is an unavoidable restriction of javascript-free webpages.

如果你不想要javascript,你将不得不等待用户提交页面,这是一个不可避免的无javascript网页限制。

#3


1  

Pretty sure that should be TextChanged and that OnTextChanged should be a JavaScript call (I believe). Either way, you should do this in JS. Calling to the server on every text changed event should be avoided.

很确定应该是TextChanged并且OnTextChanged应该是一个JavaScript调用(我相信)。不管怎样,你应该在JS中这样做。应避免在每个文本更改事件上调用服务器。

In jQuery you can do

在jQuery中你可以做到

$('#txtTime').changed(function(){
    // do event logic here
});

Now hopefully you're using jQuery. If not I can update for vanilla JS.

现在希望你使用jQuery。如果没有,我可以更新为香草JS。

#4


0  

Have you tried setting autopostback="true" in the asp:TextBox declaration? A quick search turned up this article as well, maybe it will be some help.

你试过在asp:TextBox声明中设置autopostback =“true”吗?快速搜索也出现在这篇文章中,也许它会有所帮助。