如何使ASP.NET TextBox在AJAX UpdatePanel中触发onTextChanged事件?

时间:2021-07-13 04:30:00

I am trying to get an textBox to fire it's onTextChanged event every time a keystroke is made rather than only firing only when it looses focus. I thought that adding the AsyncPostBackTrigger would do this but it's still not working. Is what I'm trying to do even possible? The code is below:

我试图让一个textBox在每次进行击键时触发onTextChanged事件,而不是仅在它失去焦点时才触发。我认为添加AsyncPostBackTrigger会这样做,但它仍然无法正常工作。我甚至想做什么?代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Items.aspx.cs" MasterPageFile="~/MMPAdmin.Master" Inherits="MMPAdmin.Items" %>
<asp:Content ID="content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:ScriptManager ID="sm_Main" runat="server" />
    <div style="left:10px;position:relative;width:100%;overflow:hidden">
        <asp:UpdatePanel ID="up_SearchText" runat="server">
            <Triggers>
                 <asp:AsyncPostBackTrigger ControlID="tb_Search" EventName="TextChanged" />
            </Triggers>
            <ContentTemplate>
                <div style="position:relative;float:left">
                    <b style="font-size:xx-large">Items</b>(<a href="Item.aspx">Add New</a>)
                </div>
                <div style="right:25px;position:absolute; top:30px">
                    Search: <asp:TextBox ID="tb_Search" runat="server" Width="200" OnTextChanged="UpdateGrid" AutoPostBack="true" />
                </div>
                <br />
                <div>
                    <asp:GridView runat="server" AutoGenerateColumns="true" ID="gv_Items" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" />
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</asp:Content>

3 个解决方案

#1


13  

  • You need to call the _postback() function for your textbox control when the onkeyup is raised using javascript.
  • 当使用javascript引发onkeyup时,需要为文本框控件调用_postback()函数。
  • However, since your textbox is inside your update panel, the textbox will get re-rendered everytime the user hits a key, causing the cursor to loose focus.
  • 但是,由于您的文本框位于更新面板中,因此每次用户按键时文本框都会重新呈现,从而导致光标失去焦点。
  • This will not be usable unless you get your textbox out of the the updatepanel. That may work out for you, as update panels tend to be a bit slow, you may still have usability issues. - I would suggest using an autocomplete component.
  • 除非您将文本框从更新面板中取出,否则这将无法使用。这可能对你有用,因为更新面板往往有点慢,你可能仍然有可用性问题。 - 我建议使用自动完成组件。

P.S : there is one in the asp.net control toolkit or you could use the jquery autocomplete plugin which I have found to be a bit better.

P.S:asp.net控件工具包中有一个,或者你可以使用我发现的jquery自动完成插件更好一些。

#2


1  

Dont Need use AJAX controls for checking the availability.. Its is not Compulsory to use it AJAX Controls.. We can use the Following Code..

不要使用AJAX控件来检查可用性..它不是必须使用它AJAX控件..我们可以使用以下代码..

<iframe>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" ontextchanged="TextBox1_TextChanged"></asp:TextBox>

 protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        RequiredFieldValidator1.ErrorMessage = "";
        Label1.Text = "";
        string name = TextBox1.Text.ToString();
        string constr = "data Source=MURALY-PC\\SQLEXPRESS; database=Online; Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        string query = "select UserName from User_tab where UserName='" + name + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {

            Label1.Text = "UserName Already Exists";
        }
        else
        {
            Label1.Text = "";
            Label1.Text = "UserName Available";
        }
        con.Close();


    }
</iframe>

#3


0  

All the AsyncPostBackTrigger does is make sure only that portion of the page refreshes when the event is fired, it does not change when the event is fired.

所有AsyncPostBackTrigger都会确保只有在触发事件时页面的那一部分才会刷新,而在触发事件时它不会改变。

I think it's possible to do what you want, but you'd need to write some javascript code to manually fire the event... and I don't even want to think about making that work.

我认为可以做你想做的事,但是你需要编写一些javascript代码来手动触发事件......我甚至不想考虑做那个工作。

#1


13  

  • You need to call the _postback() function for your textbox control when the onkeyup is raised using javascript.
  • 当使用javascript引发onkeyup时,需要为文本框控件调用_postback()函数。
  • However, since your textbox is inside your update panel, the textbox will get re-rendered everytime the user hits a key, causing the cursor to loose focus.
  • 但是,由于您的文本框位于更新面板中,因此每次用户按键时文本框都会重新呈现,从而导致光标失去焦点。
  • This will not be usable unless you get your textbox out of the the updatepanel. That may work out for you, as update panels tend to be a bit slow, you may still have usability issues. - I would suggest using an autocomplete component.
  • 除非您将文本框从更新面板中取出,否则这将无法使用。这可能对你有用,因为更新面板往往有点慢,你可能仍然有可用性问题。 - 我建议使用自动完成组件。

P.S : there is one in the asp.net control toolkit or you could use the jquery autocomplete plugin which I have found to be a bit better.

P.S:asp.net控件工具包中有一个,或者你可以使用我发现的jquery自动完成插件更好一些。

#2


1  

Dont Need use AJAX controls for checking the availability.. Its is not Compulsory to use it AJAX Controls.. We can use the Following Code..

不要使用AJAX控件来检查可用性..它不是必须使用它AJAX控件..我们可以使用以下代码..

<iframe>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" ontextchanged="TextBox1_TextChanged"></asp:TextBox>

 protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        RequiredFieldValidator1.ErrorMessage = "";
        Label1.Text = "";
        string name = TextBox1.Text.ToString();
        string constr = "data Source=MURALY-PC\\SQLEXPRESS; database=Online; Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        string query = "select UserName from User_tab where UserName='" + name + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {

            Label1.Text = "UserName Already Exists";
        }
        else
        {
            Label1.Text = "";
            Label1.Text = "UserName Available";
        }
        con.Close();


    }
</iframe>

#3


0  

All the AsyncPostBackTrigger does is make sure only that portion of the page refreshes when the event is fired, it does not change when the event is fired.

所有AsyncPostBackTrigger都会确保只有在触发事件时页面的那一部分才会刷新,而在触发事件时它不会改变。

I think it's possible to do what you want, but you'd need to write some javascript code to manually fire the event... and I don't even want to think about making that work.

我认为可以做你想做的事,但是你需要编写一些javascript代码来手动触发事件......我甚至不想考虑做那个工作。