1

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

7
  • 1
    Do you have a Page_Load code to show us? What happens there? Commented Mar 24, 2019 at 11:49
  • I could added it but there is nothing related to txtChangeQuantity.Text Commented Mar 24, 2019 at 11:51
  • 1
    txtChangeQuantity why this is declared as static? Can you show how it is declared in the aspx page? Because is to declared in the code behind only and not in the aspx page, it will not maitain its state. That's why you will not get its value when you submit the form. Commented Mar 24, 2019 at 12:38
  • 1
    y not u take the int variable in the replace of private static TextBox txtChangeQuantity; static declared. Commented Mar 24, 2019 at 12:41
  • 1
    Can you explain why are you using txtChangeQuantity and why are you assigning value to it in RowDataBound event? Commented Mar 24, 2019 at 12:41

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

When I add the <asp:Repeater id="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server"> <ItemTemplate> the code inside the brackets are no longer visible why is that?
I am not sure what part of code you are changing... which bracket you are mentioning? Can you update the code in original question with comments in it about the issue you are facing?
What is gvInventatario? is it GridView or Repeater? @JuniorCortenbach?
I thought you are using repeater not GridView, that's why my answer is all about repeater. If you are not using repeater at all then you don't need to use <asp:Repeater>. Can you confirm if you are using only GridView and not the Repeater? And also change the code of question to the original one? I will update my answer for the GridView... Sorry for the confusion..
I updated my answer. You don't need <asp:Repeater> or anything related to it now. My updated answer should be able to resolve your issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.