【问题标题】:How to fire Textbox textchanged event while typing in textbox如何在输入文本框时触发文本框 textchanged 事件
【发布时间】:2014-02-07 05:32:23
【问题描述】:

在我的项目中,我使用更新面板中的文本框来显示 db 表中尚不存在的收据编号。 但是文本框上的 textchanged 事件在键入时没有触发。我的逻辑是在打字时显示可用的收据。如果没有显示,则用户可以插入该收据编号。

<asp:UpdatePanel ID="updAvailableReceipt" runat="server">
     <ContentTemplate>
         <asp:TextBox ID="txtReceiptNo" runat="server" class="textBoxStyle" AutoPostBack="true" OnTextChanged="txtReceiptNo_TextChanged"></asp:TextBox>
         <asp:GridView ID="grdShowAvailableReceipt" runat="server" EnableTheming="True" ShowHeader="False">
         </asp:GridView>

     </ContentTemplate>
     <Triggers>
         <asp:AsyncPostBackTrigger ControlID="txtReceiptNo" EventName="TextChanged" />
     </Triggers>
</asp:UpdatePanel>


protected void txtReceiptNo_TextChanged(object sender, EventArgs e)
{
    param = ParameterClass.IniliazeParameter(1);
    int count=0;
    ParameterClass.AddParameter(ref param, "@ReceiptNumber", txtReceiptNo.Text, ref count);
    grdShowAvailableReceipt.DataSource = new bAuctionPayment().Fetch(RecordFetchMode.FethcAvailableReceipt, param);
    grdShowAvailableReceipt.DataBind();

}

【问题讨论】:

  • 会不会只是慢了点?就像尝试打字并等待一段时间。你在调试器中得到了什么?
  • 你可以使用 keydown 事件 [这可能对你有帮助][1] [1]: stackoverflow.com/questions/12560418/…

标签: c# javascript jquery asp.net


【解决方案1】:

使用它,它工作正常

JAVASCRIPT

function ChangeText() {
            var t1 = document.getElementById('<%= txtArticleTitle.ClientID %>');
            var t2 = document.getElementById('<%= txtPageName.ClientID %>');

            t2.value = t1.value;
        }

ASP.NET 源

Article Title:
 <asp:TextBox ID="txtArticleTitle" runat="server" onkeyup="ChangeText()" Width="600px"></asp:TextBox>
<br />
Page Name:
<asp:TextBox ID="txtPageName" runat="server" Width="600px"></asp:TextBox>

【讨论】:

  • 感谢您的回复。但我想调用 textchage 事件,这将从数据库中获取记录。所以我需要对 textchange 进行部分回发?我可以从此 javascript 调用部分回发吗
【解决方案2】:

两个音符。

  1. Textchanged 在文本框失去焦点时触发。您可以改用 keyd own 或 keypress 事件。

  2. 不建议每次用户按键时调用服务器。考虑使用超时,以便仅在用户完成输入时调用服务器(我认为标准是 350 毫秒)或从服务器预加载列表并在客户端过滤它。

附言

不要使用 ASP.Net ajax...只是说。

【讨论】:

    【解决方案3】:

    您可以在服务器端创建一个过程并使用 javascript 执行它。 下面使用的逻辑很简单,创建一个过程,添加一个按钮并将可见性设置为 false 或 display: none 与 css。 然后在单击按钮时触发该过程,并从 javascript 触发单击事件。 有什么问题欢迎追问

    vb代码

        Public Sub FetchRecord()
    
        'Your Code Here..
    
        End Sub
    
    Protected Sub btnView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnView.Click
            FetchRecord()
    End Sub
    

    Javascript

    <script type="text/javascript">
            function Filter() {
                document.getElementById('<%= btnView.ClientID %>').click()
            }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      相关资源
      最近更新 更多