call text box textchanged event automatically in a

2019-07-20 09:24发布

问题:

I have a form from where I am navigating to a form of mine where I have a text box for quantity. Previously I was entering the quantity. Now, IO have a work flow that is the reason the other form comes in picture. Now, I have extensive coding done my text box text changed event. When I do response.redirect form other form, I flow quantity also and put that quantity in that text box. Now i dont want to write a new code(function) for doing the same as updation procedure will also be done. Following is my code which I am trying to execute and call the text changed event. I am also using update panel. So, is that the reason why my event is not getting fired.? Any solution on that,.??

txt_Quantity.TextChanged += new EventHandler(txt_Quantity_TextChanged);



protected void txt_Quantity_TextChanged(object sender, EventArgs e)
    {
    }





 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

            <asp:Panel ID="pnlGrid" runat="server" BorderColor="#339933" Height="400px" BorderStyle="Solid"
                BorderWidth="2px" ScrollBars="Auto" Width="100%">



                <div style="height: 40px; width: 100%; background-color: #339933;">
                    <%-- <img alt="imgrid" src="Images/grid_icon_green.png" style="padding-left: 10px;" />--%>
                    <span style="font-size: 20px; font-family: Calibri; color: White; padding-left: 5px; vertical-align: top">Asset Distribution</span>
                </div>

                <AjaxToolKit:TabContainer ID="TabContainer1" runat="server" Height="400px">
                    <AjaxToolKit:TabPanel ID="tab1" runat="server" TabIndex="0" HeaderText="Basic Information">
                        <ContentTemplate>
                            <table width="100%">

                                <tr>

                                    <td class="r1">Last Code:

                                    </td>
                                    <td>
                                        <asp:Label ID="Lbl_AssetDistriCode" runat="server"></asp:Label>
                                    </td>




                                    <td width="10%" class="r1">Item Code:
                                    </td>
                                    <td width="23%">

                                        <asp:TextBox ID="txt_ItemCode" runat="server" Height="95%" Width="150px" CssClass="txtbxcomp" AutoPostBack="true" OnTextChanged="txt_ItemCode_TextChanged"></asp:TextBox>
                                       <%-- <span style="color: Red;">*</span>--%>
                                        <AjaxToolKit:AutoCompleteExtender ID="AutoCompleteExtender4" runat="server" ServiceMethod="getitem"
                                            ServicePath="WebService.asmx" TargetControlID="txt_ItemCode" CompletionInterval="500"
                                            MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="5">
                                        </AjaxToolKit:AutoCompleteExtender>

                                    </td>

                                    <td width="10%"></td>
                                    <td width="24%"></td>
                                </tr>


                                <tr>

                                    <td class="r1" width="10%">Item Group:
                                    </td>
                                    <td width="23%">
                                        <asp:DropDownList ID="ddl_Item_Grp" runat="server" Height="95%" Width="150px" CssClass="drpComp" AutoPostBack="True"
                                            OnSelectedIndexChanged="ddl_Item_Grp_SelectedIndexChanged">
                                        </asp:DropDownList>
                                       <%-- <span style="color: Red;">*</span>--%>
                                    </td>

                                    <td class="r1" width="10%">Item Type:
                                    </td>
                                    <td width="23%">
                                        <asp:DropDownList ID="ddl_Item_typ" runat="server" Height="95%" Width="150px" CssClass="drpComp" AutoPostBack="True"
                                            OnSelectedIndexChanged="ddl_Item_typ_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <%--<span style="color: Red;">*</span>--%>
                                    </td>

                                    <td class="r1" width="14%">Item Catagory:
                                    </td>
                                    <td width="20%">
                                        <asp:DropDownList ID="ddl_Item_cat" runat="server" Height="95%" Width="150px" CssClass="drpComp"></asp:DropDownList>
                                        <%--<span style="color: Red;">*</span>--%>
                                    </td>

                                </tr>


                                <tr>

                                    <td class="r1" width="10%">City:
                                    </td>
                                    <td width="23%">
                                        <asp:DropDownList ID="ddl_city" runat="server" Height="95%"  Width="150px" CssClass="drpComp" AutoPostBack="True"
                                            OnSelectedIndexChanged="ddl_city_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <%--<span style="color: Red;">*</span>--%>
                                    </td>

                                    <td class="r1" width="10%">Location:
                                    </td>
                                    <td width="24%">
                                        <asp:DropDownList ID="ddl_Loc" runat="server" Height="95%" Width="150px" CssClass="drp" 
                                          >
                                        </asp:DropDownList>
                                        <%--<span style="color: Red;">*</span>--%>
                                    </td>

                                    <td class="r1" width="10%">Branch:
                                    </td>
                                    <td width="23%">
                                        <asp:DropDownList ID="ddl_Branch" runat="server" Height="95%" Width="150px" CssClass="drpComp" AutoPostBack="True"
                                            OnSelectedIndexChanged="ddl_Branch_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <%--<span style="color: Red;">*</span>--%>
                                    </td>


                                </tr>




                                 <tr>

                                      <td class="r1" width="10%">Department:
                                    </td>
                                    <td width="24%">
                                        <asp:DropDownList ID="ddl_Dept" runat="server" Height="95%" Width="150px" CssClass="txtbx" AutoPostBack="True"
                                            OnSelectedIndexChanged="ddl_Dept_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <%--<span style="color: Red;">*</span>--%>
                                    </td>


                                    <td class="r1" width="10%">User:
                                    </td>
                                    <td width="23%">
                                        <asp:DropDownList ID="ddl_User" runat="server" Height="95%" width="150px" CssClass="txtbx"  AutoPostBack="True"></asp:DropDownList>
                                        <%--<span style="color: Red;">*</span>--%>
                                    </td>

                                    <%--<td class="r1" width="15%">Asset Code:
                                    </td>
                                    <td width="19%">
                                        <asp:DropDownList ID="ddl_Asset_code" runat="server" Height="95%" CssClass="txtbx" AutoPostBack="True"></asp:DropDownList>
                                        <span style="color: Red;">*</span>
                                    </td>--%>
                                    <td class="r1" width="10%">Quantity:
                                    </td>
                                    <td width="23%">
                                        <asp:TextBox ID="txt_Quantity" runat="server" Height="95%" Width="150px"  CssClass="txtbxcomp" AutoPostBack="true" OnTextChanged="txt_Quantity_TextChanged"></asp:TextBox>
                                        <cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender3" runat="server" TargetControlID="txt_Quantity"
                                            ValidChars="0123456789." Enabled="True">
                                        </cc1:FilteredTextBoxExtender>


                                       <%-- <span style="color: Red;">*</span>--%>

                                    </td>
                                    <td width="10%"></td>
                                    <td width="24%"></td>

                                </tr>



                                <%--<tr>
                                    <td width="15%" class="r1">Mac ID:
                                    </td>
                                    <td width="18%">

                                        <asp:TextBox ID="txt_MacId" runat="server" Height="95%" CssClass="txtbx"></asp:TextBox>
                                        <span style="color: Red;">*</span>
                                    </td>
                                    <td width="15%"></td>
                                    <td width="18%"></td>
                                    <td width="15%"></td>
                                    <td width="19%"></td>
                                </tr>--%>
                            </table>
                        </ContentTemplate>
                    </AjaxToolKit:TabPanel>

</ContentTemplate>

        <Triggers>

            <asp:PostBackTrigger ControlID="btnSave" />
            <asp:PostBackTrigger ControlID="btnUpdate" />
            <asp:PostBackTrigger ControlID="btndelete" />
            <asp:PostBackTrigger ControlID="btnClear" />

        </Triggers>

    </asp:UpdatePanel>

Thanks in advance.

回答1:

The OnTextChanged will only occur if you make a change in the textbox and also tab out of the field. OR you can say AutoPostBack on text box will trigger postback when the focus is lost from the text box.

the OnTextChanged event is NOT fired while you are typing in the text box. OnTextChanged is a server-side event and only fires when the page is posted back. Typing into a text box on a page does not post the page back and so this event will only fire once page is posted by different way like: button click etc.. So you make sure that after the text is changed, you are moving out of text box.

Using Update panel is fine and once postback happens, TextChanged event will be called too at appropriaate stage of page life cycle.

Also, try to leave the textbox code the same but drop the UpdatePanel. In other words, take AJAX completely out and see if your event is firing during a regular postback (and that you're getting the postback when you expect it). This will surely help narrow down the problem.

In case, you want this event to fire, everytime a KeyStroke is made ( as you type in), This will help: How to make an ASP.NET TextBox fire it's onTextChanged event fire in an AJAX UpdatePanel?