J.C
J.C

Reputation: 752

Textbox value empty inside gridview on button click

I have a grid view that the user can expand to fill a form. When I want to get the value of the textbox it is empty. Here is what it look like I added a red arrow to show you the information I would like to get on button presss enter image description here

Here Is my front end

 <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>

When the user presses the button btnConfirmPurchases and I use the debugger I found out txtChangeQuantity.Text is empty

     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)
            {
            }
  }

And here is my Page load as some of you have asked

    protected void Page_Load(object sender, EventArgs e)
    {

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

txtChangeQuantity.Text is empty even when the user writes something inside the textbox.

UPDATE

When I tried to add a repeater inside my code the textbox was no longer visible here is the new aspx code.

           <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>

Here is a image of the problem enter image description here

Upvotes: 1

Views: 1594

Answers (1)

Chetan
Chetan

Reputation: 6891

You have multiple problems in the code. I think you have static declared txtChangeQuantity because you want to get the value of txtChangeQuantity which is in GridView when you click on the button.

You are assigning value of GridView textbox to Static txtChangeQuantity in RowDataBound event of GridView. This event is triggered for every row of the GridView. So at the end you will have the value of last textbox from the GridView in static txtChangeQuantity.

And when you change the value of GridView textbox it will not be set to the static txtChangeQuantity.

Also since txtChangeQuantity is static and not declared in the .aspx page, you will not get its value in the code behind when you click on button.

Events of the controls which are part of other template controls, such as GridView and Repeater, does not work as you trying to do.

From your code, I think you want to get the value of the textbox, which is associated with the button in the GridView control, to calculate the quantity. You can do this by using following approach.

You need to use RowCommand event of GridView control.

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

RowCommand event is triggered whenever a button control is clicked GridView.

So you need to have event handler and assigned for RowCommand of GridView. And you don't need Click event handler confirm_purchases_Click for the button 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>

The event handler for RowCommand event would look as following.

protected void gvInventatario_RowCommand(Object Sender, GridViewCommandEventArgs e) 
{        

}

Now whenever you click any button or link in GridView, this event handler will be triggered. Here, you need to find the txtChangeQuantity which is at the same level at the button and get its value to calculate the quantity. This you can do as following.

GridViewCommandEventArgs e of event handler is used to determine the GridViewRow from which the button is clicked and once you have the GridViewRow identified you can use FindControl method to access other controls from the same 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)
        {
        }
    }
}

So you actually do not need static control. All you need is proper handling events raised from the controls inside the GridView.

Upvotes: 3

Related Questions