按钮单击时,gridview值在gridview内部为空

问题描述 投票:1回答:1

我有一个网格视图,用户可以展开以填充表单。当我想获取文本框的值时,它是空的。这就是它的样子我添加了一个红色箭头,向您显示我想要按下按钮的信息enter image description here

这是我的前端

 <asp:TemplateField>
                        <ItemTemplate>
                            <tr>
                               <td colspan="100%" style="background:#F5F5F5" >
                                 <div id="div<%# Eval("componente_id") %>"  style="overflow:auto; display:none; position: relative; left: 15px; overflow: auto">
                            <div class="ExpandTableHeader">
                             Cambiar la cantidad
                            </div>
                            <div class="body">
                                <label for="validationOfTypeID">Armario:</label>
                                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                 <ContentTemplate>
                                <asp:DropDownList ID="drCloset" AppendDataBoundItems="True" runat="server" Width="20%" Height="30px" AutoPostBack="true" OnSelectedIndexChanged = "OnClosetIndexChanged"></asp:DropDownList>
                               <br/>
                                      <label for="validationOfTypeID" visible="false" >cajon</label> <br/>
                                  <asp:DropDownList ID = "drDrawer"  AutoPostBack="true" runat="server" Width="20%" Height="30px" >
                                 </asp:DropDownList>
                                </ContentTemplate>
                               <Triggers>
                                  <asp:AsyncPostbackTrigger ControlID="drCloset" EventName="SelectedIndexChanged" />
                               </Triggers>
                                     </asp:UpdatePanel>
                               <asp:Label ID="lblQuantity" runat="server" Text=""></asp:Label>
                                 <label for="validationOfTypeID"></label>
                                   <asp:DropDownList Height="30px" ID="drOperation" runat="server" AutoPostBack="true">
                                       <asp:ListItem>+</asp:ListItem>
                                       <asp:ListItem>-</asp:ListItem>
                                </asp:DropDownList> 

                                <asp:TextBox width="50px" ID="txtChangeQuantity" runat="server" TextMode="Number" min="0" step="1" Value="0"  ></asp:TextBox>
                                  <asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>

                                  <br/>
                                </br>
                                <asp:Button ID="btnConfirmPurchases" runat="server" Text="Validar" AutoPostback="true" OnClick="confirm_purchases_Click" /> 
                                <asp:Button ID="btnHide2" runat="server" Text="Anular" AutoPostBack="True"  />

                            </div>
                                     <asp:DetailsView id="DetailsView1" DataKeyNames="componente_id" Runat="server" Width="300px" Font-Names="Calibri"/>                              
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:TemplateField>

当用户按下按钮btnConfirmPurchases并且我使用调试器时我发现txtChangeQuantity.Text为空

     private static TextBox txtChangeQuantity;


 protected void gvInventatario_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)

            {
                txtChangeQuantity = (TextBox)e.Row.FindControl("txtChangeQuantity");
            }

       }

     protected void confirm_purchases_Click(object sender, EventArgs e)
        {
            int resultingQuantity = 0;


            if (drOperation.Text == "-")
            {
                resultingQuantity = quantity - int.Parse(txtChangeQuantity.Text);
            }
            else
            {
                resultingQuantity = quantity + int.Parse(txtChangeQuantity.Text);
            }
            if (resultingQuantity > 0)
            {
            }
  }

这是你的一些人问我的页面加载

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ViewState["sortOrder"] = "";
            PopulateSorting("", "");
            PopulateGridview(queryStrPopulateBasic);
            gvInventario.DataSource = dt;
            gvInventario.DataBind();
         }
    }

即使用户在文本框中写入内容,txtChangeQuantity.Text也是空的。

UPDATE

当我尝试在我的代码中添加转发器时,文本框不再可见,这里是新的aspx代码。

           <asp:TemplateField>
            <ItemTemplate>
                <tr>
                   <td colspan="100%" style="background:#F5F5F5" >
                     <div id="div<%# Eval("componente_id") %>"  style="overflow:auto; display:none; position: relative; left: 15px; overflow: auto">
                <div class="ExpandTableHeader">
                 Cambiar la cantidad
                </div>
                <div class="body">
                    <label for="validationOfTypeID">Armario:</label>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                     <ContentTemplate>
                    <asp:DropDownList ID="drCloset" AppendDataBoundItems="True" runat="server" Width="20%" Height="30px" AutoPostBack="true" OnSelectedIndexChanged = "OnClosetIndexChanged"></asp:DropDownList>
                   <br/>
                          <label for="validationOfTypeID" visible="false" >cajon</label> <br/> <%--WARGING DO NOT CHANGE NAME cajon WILL BREAK APPLICATION  --%>
                      <asp:DropDownList ID = "drDrawer"  AutoPostBack="true" runat="server" Width="20%" Height="30px" >
                     </asp:DropDownList>
                    </ContentTemplate>
                   <Triggers>
                      <asp:AsyncPostbackTrigger ControlID="drCloset" EventName="SelectedIndexChanged" />
                   </Triggers>
                         </asp:UpdatePanel>

                    <asp:Repeater id="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server">
                 <ItemTemplate>
                   <asp:Label ID="lblQuantity" runat="server" Text=""></asp:Label>
                     <label for="validationOfTypeID"></label>
                       <asp:DropDownList Height="30px" ID="drOperation" runat="server" AutoPostBack="true">
                           <asp:ListItem>+</asp:ListItem>
                           <asp:ListItem>-</asp:ListItem>
                    </asp:DropDownList> 

                    <asp:TextBox width="50px" ID="txtChangeQuantity" runat="server" TextMode="Number" min="0" step="1" Value="0"  ></asp:TextBox>
                      <asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>

                      <br/>
                    </br>
                    <asp:Button ID="btnConfirmPurchases" runat="server" Text="Validar" AutoPostback="true" OnClick="confirm_purchases_Click"  /> 
                    <asp:Button ID="btnHide2" runat="server" Text="Anular" AutoPostBack="True"  />
                     </ItemTemplate>
                    </asp:Repeater>



                </div>
                  <asp:DetailsView id="DetailsView1" DataKeyNames="componente_id" Runat="server" Width="300px" Font-Names="Calibri"/>                              
                    </td>
                </tr>
            </ItemTemplate>
        </asp:TemplateField>

这是enter image description here问题的图像

c# asp.net
1个回答
3
投票

您在代码中遇到了多个问题。我认为你有静态声明txtChangeQuantity,因为你想获得当你点击按钮时在GridView中的txtChangeQuantity的值。

您正在GridView的txtChangeQuantity事件中将GridView文本框的值分配给静态RowDataBound。 GridView的每一行都会触发此事件。因此,最后您将获得静态txtChangeQuantity中GridView的最后一个文本框的值。

当您更改GridView文本框的值时,它将不会设置为静态txtChangeQuantity

此外,由于txtChangeQuantity是静态的并且未在.aspx页面中声明,因此当您单击按钮时,您将无法在后面的代码中获得其值。

作为其他模板控件一部分的控件事件(如GridView和Repeater)不能像您尝试的那样工作。

从您的代码中,我认为您希望获得与GridView控件中的按钮相关联的文本框的值来计算数量。您可以使用以下方法执行此操作。

您需要使用GridView控件的RowCommand事件。

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowcommand?view=netframework-4.7.2

只要单击按钮控件GridView,就会触发RowCommand事件。

因此,您需要具有事件处理程序并为GridView的RowCommand分配。并且您不需要按钮confirm_purchases_Click的Click事件处理程序btnConfirmPurchases

<asp:GridView id="gvInventatario" OnRowCommand="gvInventatario_RowCommand" runat="server">
    <ItemTemplate>
      //All other code...
      <asp:Button ID="btnConfirmPurchases" runat="server" Text="Validar" />
      //All other code...
    </ItemTemplate>
</asp:GridView>

RowCommand事件的事件处理程序如下所示。

protected void gvInventatario_RowCommand(Object Sender, GridViewCommandEventArgs e) 
{        

}

现在,只要单击GridView中的任何按钮或链接,就会触发此事件处理程序。在这里,您需要找到按钮处于同一级别的txtChangeQuantity并获取其值以计算数量。您可以执行以下操作。

事件处理程序的GridViewCommandEventArgs e用于确定单击按钮的GridViewRow,一旦确定了GridViewRow,就可以使用FindControl方法从同一GridViewRow访问其他控件。

How can we find the control in the row command of grid view?

protected void gvInventatario_RowCommand(Object Sender, GridViewCommandEventArgs e) 
{   
    GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

    TextBox quantityTextBox = row.FindControl("MyTextBoxId") as TextBox;

    if(quantityTextBox != null)
    {
        int resultingQuantity = 0;

        if (drOperation.Text == "-")
        {
            resultingQuantity = quantity - int.Parse(quantityTextBox.Text);
        }
        else
        {
            resultingQuantity = quantity + int.Parse(quantityTextBox.Text);
        }

        if (resultingQuantity > 0)
        {
        }
    }
}

所以你实际上不需要静态控制。您只需要从GridView中的控件引发正确的处理事件。

© www.soinside.com 2019 - 2024. All rights reserved.