文本更改事件中的问题

本文关键字:问题 事件 文本 | 更新日期: 2023-09-27 18:24:08

在下面的代码中,我有网格视图,并且有一些控件,其中我有数量文本框。如果我输入一个值并离开文本框,它应该调用服务器端方法。我试着在调用该方法时清除控件的所有值。请帮我解决这个问题。

  <asp:GridView Width="100%" runat="server" ID="gvProduct" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" ShowFooter="true"
                            PageSize-Mode="NumericPages" PageSize="10" PagerStyle-Visible="true" AllowPaging="true" AllowSorting="true"                            
                            >
                            <Columns>                                               
                                <asp:TemplateField HeaderText="Product Name" ItemStyle-Width="350px">
                                    <ItemTemplate>
                                      <asp:DropDownList ID="ddlProduct" runat="server" OnSelectedIndexChanged="ddlProduct_SelectedIndexChanged"  AutoPostBack="true" Style="width: 100%; height:23px" ></asp:DropDownList>                                                                             
                                     </ItemTemplate>                                   
                                </asp:TemplateField>
                               <asp:TemplateField HeaderText="Current Stock" ItemStyle-Width="150px"  >
                                    <ItemTemplate>
                                       <center> <asp:TextBox ID="txtCurrentStock" onkeypress="return isNumberKey(event, false);" Height="20px" runat="server" Width="160px" Enabled="false"> </asp:TextBox></center>
                                    </ItemTemplate>                                    
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Quantity" ItemStyle-Width="150px">
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtQuantity" onkeypress="return isNumberKey(event, false);"  AutoPostBack="true"  runat="server" Height="20px" Width="150px" OnTextChanged="txtQuantity_TextChanged" > </asp:TextBox>
                                    </ItemTemplate>                                   
                                </asp:TemplateField>
                                 <asp:TemplateField HeaderText="Unit Type" ItemStyle-Width="50px" >
                                    <ItemTemplate>
                                    <center>  <asp:Label ID="lblunittype" runat="server" Width="50px" ></asp:Label></center> 
                                    </ItemTemplate>                                    
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Product Required Date" ItemStyle-Width="150px">
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtProductRequiredDate"  style="text-align: left" onkeypress="return isNumberKey(event, false);"  Height="20px" runat="server" Width="150px" > </asp:TextBox>   
                                        <asp:LinkButton ID="lnkDeliveryDate" runat="server" CommandName="DeliveryDate" >select</asp:LinkButton> 
                                    </ItemTemplate>                                   
                                </asp:TemplateField> 

                                <asp:TemplateField HeaderText="Remove" HeaderStyle-VerticalAlign="Top" ItemStyle-Width="100px" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" OnClientClick="return confirmDelete()" >Delete</asp:LinkButton>
                                    </ItemTemplate>
                                    <FooterTemplate>                                     
                                            <asp:Button ID="ButtonAdd" runat="server" OnClick="btnAdd_Click" class="button" Text="Add New" />                                                            
                                   </FooterTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle Font-Bold="True" ForeColor="White" />
                            <FooterStyle HorizontalAlign="Right" />
                        </asp:GridView>
 protected void txtQuantity_TextChanged(object sender, EventArgs e)
        {
  AddNewRowToGrid()
        }

文本更改事件中的问题

问题在于页面的生命周期。您需要在生命周期中早于触发单击事件时从文本框中收集值。我记得在使用web表单时遇到过这样的问题,一旦我切换到MVC,我发现生活就轻松多了。