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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1009086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:52:37  来源:igfitidea点击:

How to make an ASP.NET TextBox fire it's onTextChanged event fire in an AJAX UpdatePanel?

.netajaxtextbox

提问by smercer

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:

我试图让文本框在每次击键时触发它的 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>

回答by smercer

  • You need to call the _postback()function for your textbox control when the onkeyupis raised using javascript.
  • 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.
  • _postback()onkeyup使用 javascript 引发时,您需要为您的文本框控件调用该函数。
  • 但是,由于您的文本框位于更新面板内,因此每次用户按下键时,文本框都会重新呈现,从而导致光标失去焦点。
  • 除非您将文本框从更新面板中取出,否则这将无法使用。这可能对您有用,因为更新面板往往有点慢,您可能仍然存在可用性问题。- 我建议使用自动完成组件。

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.

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

回答by Shujaat Ali

AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"

AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"

Both events are required to trigger text change event.

这两个事件都需要触发文本更改事件。

回答by Srividhya

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>

回答by CodeRedick

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 代码来手动触发事件......我什至不想考虑让它工作。