简体   繁体   English

asp.net中带有文本框的autpostback

[英]autpostback with textbox in asp.net

I have the following aspx code 我有以下aspx代码

 <asp:TextBox ID="uname" runat="server" AutoPostBack="True" 
            ontextchanged="uname_TextChanged"></asp:TextBox>

in code behind file 在文件后面的代码中

protected void uname_TextChanged(object sender, EventArgs e)
    {
        Response.Write("Called on postback");
    }

As per my assumption due to autopostback if I write anything in the textbox a postback will occur, but its nor occuring now, what could be the reason? 根据我对自动回发的假设,如果我在文本框中编写任何内容,都会发生回发,但是现在不发生回发,这可能是什么原因?

  • Normally there are more than 1 event handler for an event like for a control, which event will be triggered if autopostback occurs.? 通常,一个事件(如控件)有多个事件处理程序,如果发生自动回发,将触发哪个事件。

You said As per my assumption due to autopostback if I write anything in the textbox a postback will occur . 您说过, As per my assumption due to autopostback if I write anything in the textbox a postback will occur

That is wrong . 那是错的 It will fire text changed event when your focus from textbox will be out. 当您的焦点从文本框中移出时,它将触发text changed事件。 So when you type something and press Tab key then only your TextChanged event will be triggered. 因此,当您键入内容并按Tab键时,只会触发您的TextChanged事件。

If you want to trigger TextChanged event when you type something then you should call it from javascript using OnKeyDown event. 如果要在键入内容时触发TextChanged事件,则应使用OnKeyDown事件从javascript调用它。 see below code sample : 参见下面的代码示例:

<asp:TextBox ID="uname" runat="server" AutoPostBack="True"
    OnKeyDown="TextChanged(this)" OnTextChanged="uname_TextChanged"></asp:TextBox>


<script type="text/javascript">
    function TextChanged(control) {
        $(control).change();
    }
</script>

Now when you type anything in your textbox it will call TextChanged method of javascript and this method will trigger uname_TextChanged event. 现在,当您在文本框中键入任何内容时,它将调用javascript的TextChanged方法,并且该方法将触发uname_TextChanged事件。

Assumption: 假设:

If your textbox control is inside UpdatePanel then also your change event cannot be triggered. 如果您的文本框控件位于UpdatePanel中,则您的更改事件也无法触发。 In such a case you should define trigger for text box. 在这种情况下,您应该为文本框定义触发器 as mentioned below : 如下所述:

<asp:UpdatePanel runat="server" ID="up1">
    <ContentTemplate>
        <asp:TextBox ID="uname" runat="server" AutoPostBack="True"
            OnKeyDown="TextChanged(this)" OnTextChanged="uname_TextChanged"></asp:TextBox>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="uname" />
    </Triggers>
</asp:UpdatePanel>

Q : which event will be triggered if autopostback occurs ? 问:如果发生自动回发,将触发哪个事件?

Answer: Normally, for all the events for which contents are changed between the post server, AutoPostBack requires. 答:通常,对于所有在发布服务器之间更改了内容的事件,都需要使用AutoPostBack。 But it wont be triggered until you define the event for that. 但是,除非您为此定义事件,否则它不会被触发。

For example if you set the AutoPostBack property of the DropDownList to true , and if you don't specify the OnSelectedIndexChanged event then it will not trigger this event. 例如,如果将DropDownListAutoPostBack属性设置为true ,并且未指定OnSelectedIndexChanged事件,则它将不会触发此事件。 But your page will be post back when you change the value. 但是,当您更改值时,页面将被回发。

Same thing happens in case of TextBox , CheckBox , RadioButton etc... TextBoxCheckBoxRadioButton等情况下也会发生同样的事情。

Hope it is enough to understand. 希望足以理解。

MSDN states that the AutoPostBack is only raised when the control loses focus: MSDN指出仅当控件失去焦点时才会引发AutoPostBack:

Gets or sets a value that indicates whether an automatic postback to the server occurs when the TextBox control loses focus. 获取或设置一个值,该值指示当TextBox控件失去焦点时是否自动回发到服务器。

... ...

Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. 使用AutoPostBack属性指定当TextBox控件失去焦点时是否自动回发到服务器。 Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus. 在TextBox控件中按ENTER或TAB键是更改焦点的最常见方法。

However, a test showed that you need to enter a text first for the PostBack to occur. 但是,测试表明您需要首先输入一个文本以使PostBack发生。 After I entered some text and tabbed out of the TextBox, the PostBack was done and the TextChanged event was raised. 输入一些文本并在TextBox中跳出标签后,完成了PostBack并引发了TextChanged事件。 After clearing the text and tabbing out of the TextBox, the AutoPostBack was also done, so it does not depend on whether the TextBox is empty or not. 清除文本并在TextBox中跳出标签之后,也会执行AutoPostBack,因此它不取决于TextBox是否为空。

To address also the second part of your question: which event is raised during the PostBack is decided upon by the ASP.NET framework during the initialization phase of the PostBack. 同样要解决您的问题的第二部分:在PostBack期间引发哪个事件是由ASP.NET框架在PostBack的初始化阶段决定的。 Eg if the text of the TextBox that is contained in the Form values differs from that stored in the ViewState, the TextChanged event is raised. 例如,如果Form值中包含的TextBox的文本与ViewState中存储的文本不同,则引发TextChanged事件。 This explains why the TextChanged handler is called even if the AutoPostback was initiated by the losing the focus. 这就解释了为什么即使AutoPostback是由于失去焦点而启动的,也要调用TextChanged处理程序。

Try by changing the ontextchanged to OnTextChanged and try. 通过将ontextchanged更改为OnTextChanged进行尝试,然后尝试。 and after entering the text in textbox try to click the mouse on the page sure it works. 在文本框中输入文字后,请尝试在页面上单击鼠标以确保其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM