在asp.net中自动调用文本框textchanged事件

时间:2013-09-08 07:02:40

标签: c# asp.net textbox updatepanel textchanged

我有一个表单,我正在导航到我的一个形式,我有一个数量的文本框。以前我输入了数量。现在,IO有一个工作流程,这是其他形式出现的原因。现在,我已经完成了大量的编码,我的文本框文本改变了事件。当我response.redirect形成其他形式时,我也会流量并将该数量放在该文本框中。现在我不想写一个新的代码(函数)来做同样的更新程序也将完成。以下是我试图执行的代码,并调用文本更改事件。我也在使用更新面板。那么,这就是为什么我的活动没有被解雇的原因。任何解决方案,。??

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>

提前致谢。

1 个答案:

答案 0 :(得分:9)

OnTextChanged只有在文本框中进行更改并且字段外的选项卡才会出现。或者你可以说文本框上的AutoPostBack会在文本框中丢失焦点时触发回发。

在文本框中输入时,不会触发OnTextChanged事件。 OnTextChanged是服务器端事件,仅在页面回发时触发。在页面上键入文本框不会将页面发回,因此只有通过不同的方式发布页面时才会触发此事件,例如:按钮单击等。因此,您确保在文本更改后,您正在移出文本框。

使用“更新”面板很好,一旦发生回发,TextChanged事件也会在页面生命周期的适当阶段被调用。

此外,尝试保留文本框代码相同但删除UpdatePanel。换句话说,完全取出AJAX并查看您的事件是否在常规回发期间被触发(并且您在预期时获得回发)。这肯定有助于缩小问题范围。

如果您希望在每次创建KeyStroke时触发此事件(当您输入时),这将有助于:How to make an ASP.NET TextBox fire it's onTextChanged event fire in an AJAX UpdatePanel?