updatepanel中的文本框问题 - 不会导致OnTextChanged事件

时间:2022-08-25 17:30:42

I have the following situation: I have a textbox inside an ajax updatepanel. Wherever the user types in the textbox I must display a message (different message that depends on the user typed data).

我有以下情况:我在ajax updatepanel中有一个文本框。无论用户在文本框中输入什么,我都必须显示一条消息(不同的消息取决于用户输入的数据)。

     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <asp:TextBox ID="txtMyTexbox" runat="server" Width="500px" OnTextChanged="txtMyTexbox_TextChanged" AutoPostBack="true"></asp:TextBox>
            <br />
            <asp:Label ID="lblMessage" runat="server" CssClass="errorMessage" Visible="false">Hello World</asp:Label>
         </ContentTemplate>
            <Triggers>
             <asp:AsyncPostBackTrigger ControlID="txtMyTexbox" />
            </Triggers>
      </asp:UpdatePanel>

In server side I have written the following at page load

在服务器端,我在页面加载时写了以下内容

ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtMyTexbox);           

and the method like this

和这样的方法

protected void txtMyTexbox_TextChanged(object sender, EventArgs e)
    {           
            if (.....)
            {
                lblMessage.Visible = false;
            }
            else
            {
                lblMessage.Visible = true;
            }            
    }

My problem now is that: when the user types in the textbox it doesn't cause OnTextChanged event.

我现在的问题是:当用户键入文本框时,它不会导致OnTextChanged事件。

Am I missing something?

我错过了什么吗?

6 个解决方案

#1


6  

I'm not sure that your problem has anything to do with the UpdatePanel.

我不确定您的问题与UpdatePanel有什么关系。

In fact, the TextChanged event doesn't fire while typing. It will only fire after the textbox loses focus. This happens directly if AutoPostBack is set to True, or when the next postback occurs. Please see the docs for the AutoPostBack property and the TextChanged event.

实际上,键入时不会触发TextChanged事件。它只会在文本框失去焦点后触发。如果AutoPostBack设置为True,或者发生下一次回发,则会直接发生这种情况。请参阅AutoPostBack属性和TextChanged事件的文档。

Afaik, your best bet is probably to handle the keyup event in javascript.

Afaik,你最好的选择可能是在javascript中处理keyup事件。

Here's a simple jQuery example:

这是一个简单的jQuery示例:

$(document).ready(function() {
    $(':text[id$=YourTextBox]').keyup(function() {
        if ($(this).val() === "your special value") {
            $('span[id$=YourLabel]').css('visibility', 'visible');
        }
        else {
            $('span[id$=YourLabel]').css('visibility', 'hidden');
        }
    });
});

#2


5  

Set the EventName property for your txtMyTexbox AsyncPostBackTrigger to TextChanged

将txtMyTexbox AsyncPostBackTrigger的EventName属性设置为TextChanged

<Triggers>             
    <asp:AsyncPostBackTrigger ControlID="txtMyTexbox" EventName="TextChanged" />            
</Triggers>

Other sugguestion:

其他建议:

Have you tried looking at the AutoComplete control that is part of the AjaxControlToolKit? Its behaves the same way you want your solution to behave.

您是否尝试过查看属于AjaxControlToolKit的AutoComplete控件?它的行为方式与您希望解决方案的行为方式相同。

#3


1  

its strnage to know that even after adding update panel / AsyncPostBackTrigger , TextBox ChangeEvent doesn't work properly. Some time its works and some times it not..Since its is Asychronous call, we need to some time refresh, or wait or unpredictable , Hopes microsoft will come up with competent one.. Below are easy way to check user name pretty good

它知道即使在添加更新面板/ AsyncPostBackTrigger之后,TextBox ChangeEvent也无法正常工作。有一段时间它的工作,有时它不是..由于它是异步调用,我们需要一些时间刷新,或等待或不可预测,希望微软将拿出称职的一个..以下是简单的方法检查用户名相当不错

------ Under Page_Load - aspx.cs -----------------------

------在Page_Load下 - aspx.cs -----------------------

this.TextBox1.Attributes.Add("onKeyUp", "fnUNameSubmit(this);");

this.TextBox1.Attributes.Add(“onKeyUp”,“fnUNameSubmit(this);”);

-------in aspx -add script ---------------------------------------

-------在aspx -add脚本中-------------------------------------- -

<script language="javascript" type="text/javascript">

function fnUNameSubmit(urInput) {   
var inpt= urInput.value;
if (inpt.length > 21) { 
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "green";
document.form1.submit();  // This is only trick we use here..
}
else {
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "red";
}
  }
</script>

-------in aspx -add script --------------------------------------- ----------------aspx.cs ------------------- if (TextBox1.Text.Length > 21) { CheckUsrName(); Label2.Text = ""; } else { Label2.Text = "Length is less than 21"; //lets do some stuff..bla..bla } ------------------------------------------------- CheckUsername()

-------在aspx -add脚本中-------------------------------------- - ---------------- aspx.cs ------------------- if(TextBox1.Text.Length> 21){ CheckUsrName(); Label2.Text =“”; } else {Label2.Text =“长度小于21”; //让我们做一些东西..bla..bla} ------------------------------------- ------------ CheckUsername()

public void CheckUsrName() {

public void CheckUsrName(){

  Call dB values

}

#4


0  

You should not be using RegisterAsyncPostBackControl for your TextBox. That method is really only for use for controls that reside outside of UpdatePanels. I would try removing that line of code and seeing what happens.

您不应该为TextBox使用RegisterAsyncPostBackControl。该方法实际上仅用于驻留在UpdatePanel之外的控件。我会尝试删除该行代码,看看会发生什么。

See this for more info: http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerasyncpostbackcontrol.aspx

有关详细信息,请参阅此内容:http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerasyncpostbackcontrol.aspx

#5


0  

a workaround check textbox - causesvalidation property and set it to true

解决方法检查文本框 - causevalidation属性并将其设置为true

#6


0  

The Control which id is used in AsyncPostBackTrigger must be outside the update Panel(that cause to fire the Async call) like this:

在AsyncPostBackTrigger中使用的控件必须在更新面板之外(导致触发异步调用),如下所示:

<tr>
    <td colspan="4"><asp:Label ID="lblEnter_Successfully"  Text="Enter Record SuccessFully" runat="server" Visible ="false" ForeColor ="Blue" Font-Size ="Larger" Font-Bold ="true"></asp:Label>
    </td>
</tr>                                           
</table>                
</ContentTemplate>

  <Triggers>
     <asp:AsyncPostBackTrigger ControlID = "Button_Save" EventName ="Click"/>
  </Triggers>                       

</asp:UpdatePanel>
            <table>
              <tr>
                <td width = "472px" align ="right">             
                    <asp:Button ID="Button_Save" runat="server" Text="Save"  OnClientClick ="return URLValidation();"/>
                    <asp:Button ID="Button_Clear" runat="server" Text="Clear"/>
                </td>
              </tr>
            </table>

#1


6  

I'm not sure that your problem has anything to do with the UpdatePanel.

我不确定您的问题与UpdatePanel有什么关系。

In fact, the TextChanged event doesn't fire while typing. It will only fire after the textbox loses focus. This happens directly if AutoPostBack is set to True, or when the next postback occurs. Please see the docs for the AutoPostBack property and the TextChanged event.

实际上,键入时不会触发TextChanged事件。它只会在文本框失去焦点后触发。如果AutoPostBack设置为True,或者发生下一次回发,则会直接发生这种情况。请参阅AutoPostBack属性和TextChanged事件的文档。

Afaik, your best bet is probably to handle the keyup event in javascript.

Afaik,你最好的选择可能是在javascript中处理keyup事件。

Here's a simple jQuery example:

这是一个简单的jQuery示例:

$(document).ready(function() {
    $(':text[id$=YourTextBox]').keyup(function() {
        if ($(this).val() === "your special value") {
            $('span[id$=YourLabel]').css('visibility', 'visible');
        }
        else {
            $('span[id$=YourLabel]').css('visibility', 'hidden');
        }
    });
});

#2


5  

Set the EventName property for your txtMyTexbox AsyncPostBackTrigger to TextChanged

将txtMyTexbox AsyncPostBackTrigger的EventName属性设置为TextChanged

<Triggers>             
    <asp:AsyncPostBackTrigger ControlID="txtMyTexbox" EventName="TextChanged" />            
</Triggers>

Other sugguestion:

其他建议:

Have you tried looking at the AutoComplete control that is part of the AjaxControlToolKit? Its behaves the same way you want your solution to behave.

您是否尝试过查看属于AjaxControlToolKit的AutoComplete控件?它的行为方式与您希望解决方案的行为方式相同。

#3


1  

its strnage to know that even after adding update panel / AsyncPostBackTrigger , TextBox ChangeEvent doesn't work properly. Some time its works and some times it not..Since its is Asychronous call, we need to some time refresh, or wait or unpredictable , Hopes microsoft will come up with competent one.. Below are easy way to check user name pretty good

它知道即使在添加更新面板/ AsyncPostBackTrigger之后,TextBox ChangeEvent也无法正常工作。有一段时间它的工作,有时它不是..由于它是异步调用,我们需要一些时间刷新,或等待或不可预测,希望微软将拿出称职的一个..以下是简单的方法检查用户名相当不错

------ Under Page_Load - aspx.cs -----------------------

------在Page_Load下 - aspx.cs -----------------------

this.TextBox1.Attributes.Add("onKeyUp", "fnUNameSubmit(this);");

this.TextBox1.Attributes.Add(“onKeyUp”,“fnUNameSubmit(this);”);

-------in aspx -add script ---------------------------------------

-------在aspx -add脚本中-------------------------------------- -

<script language="javascript" type="text/javascript">

function fnUNameSubmit(urInput) {   
var inpt= urInput.value;
if (inpt.length > 21) { 
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "green";
document.form1.submit();  // This is only trick we use here..
}
else {
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "red";
}
  }
</script>

-------in aspx -add script --------------------------------------- ----------------aspx.cs ------------------- if (TextBox1.Text.Length > 21) { CheckUsrName(); Label2.Text = ""; } else { Label2.Text = "Length is less than 21"; //lets do some stuff..bla..bla } ------------------------------------------------- CheckUsername()

-------在aspx -add脚本中-------------------------------------- - ---------------- aspx.cs ------------------- if(TextBox1.Text.Length> 21){ CheckUsrName(); Label2.Text =“”; } else {Label2.Text =“长度小于21”; //让我们做一些东西..bla..bla} ------------------------------------- ------------ CheckUsername()

public void CheckUsrName() {

public void CheckUsrName(){

  Call dB values

}

#4


0  

You should not be using RegisterAsyncPostBackControl for your TextBox. That method is really only for use for controls that reside outside of UpdatePanels. I would try removing that line of code and seeing what happens.

您不应该为TextBox使用RegisterAsyncPostBackControl。该方法实际上仅用于驻留在UpdatePanel之外的控件。我会尝试删除该行代码,看看会发生什么。

See this for more info: http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerasyncpostbackcontrol.aspx

有关详细信息,请参阅此内容:http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerasyncpostbackcontrol.aspx

#5


0  

a workaround check textbox - causesvalidation property and set it to true

解决方法检查文本框 - causevalidation属性并将其设置为true

#6


0  

The Control which id is used in AsyncPostBackTrigger must be outside the update Panel(that cause to fire the Async call) like this:

在AsyncPostBackTrigger中使用的控件必须在更新面板之外(导致触发异步调用),如下所示:

<tr>
    <td colspan="4"><asp:Label ID="lblEnter_Successfully"  Text="Enter Record SuccessFully" runat="server" Visible ="false" ForeColor ="Blue" Font-Size ="Larger" Font-Bold ="true"></asp:Label>
    </td>
</tr>                                           
</table>                
</ContentTemplate>

  <Triggers>
     <asp:AsyncPostBackTrigger ControlID = "Button_Save" EventName ="Click"/>
  </Triggers>                       

</asp:UpdatePanel>
            <table>
              <tr>
                <td width = "472px" align ="right">             
                    <asp:Button ID="Button_Save" runat="server" Text="Save"  OnClientClick ="return URLValidation();"/>
                    <asp:Button ID="Button_Clear" runat="server" Text="Clear"/>
                </td>
              </tr>
            </table>