Textbox value empty inside gridview on button click

Обновить

April 2019

Просмотры

57 раз

1

У меня есть вид сетки, что пользователь может расширить, чтобы заполнить форму. Когда я хочу, чтобы получить значение текстового поля он пуст. Вот то, что это выглядит, как я добавил красную стрелку, чтобы показать вам информацию, которую я хотел бы получить на кнопку presss введите описание изображения здесь

Вот мой передний конец

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

Вот образ проблемы введите описание изображения здесь

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://docs.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.