【问题标题】:Textbox value empty inside gridview on button click按钮单击时gridview内的文本框值为空
【发布时间】:2019-03-24 23:16:17
【问题描述】:

我有一个网格视图,用户可以展开它来填写表格。当我想获取文本框的值时,它是空的。这是我添加一个红色箭头的样子,向您显示我想在按钮按下时获得的信息

这是我的前端

 <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 是空的,即使用户在文本框中写了一些东西。

更新

当我尝试在代码中添加转发器时,文本框不再可见,这里是新的 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>

这是问题的图片

【问题讨论】:

  • 您有 Page_Load 代码可以展示给我们吗?那里会发生什么?
  • 我可以添加它,但与 txtChangeQuantity.Text 没有任何关系
  • txtChangeQuantity 为什么将其声明为静态?你能展示它是如何在 aspx 页面中声明的吗?因为是只在后面的代码中声明,而不是在aspx页面中声明,所以它不会保持它的状态。这就是为什么您在提交表单时不会得到它的值。
  • 你不能用 int 变量代替 private static TextBox txtChangeQuantity; static 声明。
  • 您能解释一下为什么使用txtChangeQuantity 以及为什么要在 RowDataBound 事件中为其赋值吗?

标签: c# asp.net


【解决方案1】:

代码中有多个问题。我认为您已经静态声明了 txtChangeQuantity,因为您想在单击按钮时获取 GridView 中的 txtChangeQuantity 的值。

您在 GridView 的 RowDataBound 事件中将 GridView 文本框的值分配给静态 txtChangeQuantity。 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。并且对于按钮 btnConfirmPurchases,您不需要 Click 事件处理程序 confirm_purchases_Click

<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 内的控件引发的事件。

【讨论】:

  • 当我添加 括号内的代码不再可见,这是为什么?跨度>
  • 我不确定您要更改代码的哪一部分...您提到的是哪个括号?您能否更新原始问题中的代码,其中包含有关您面临的问题的 cmets?
  • 什么是gvInventatario?是GridView还是Repeater? @JuniorCortenbach?
  • 我以为你使用的是中继器而不是 GridView,这就是为什么我的回答都是关于中继器的。如果您根本不使用中继器,则不需要使用&lt;asp:Repeater&gt;。您能否确认您是否仅使用 GridView 而不是 Repeater?并且还将问题代码更改为原始代码?我将更新我对 GridView 的答案...抱歉给您带来了困惑..
  • 我更新了我的答案。你现在不需要&lt;asp:Repeater&gt; 或任何与之相关的东西。我更新的答案应该能够解决您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 2018-03-01
  • 2021-01-27
  • 1970-01-01
相关资源
最近更新 更多