[英]Textbox value empty inside gridview on button click
我有一個網格視圖,用戶可以展開以填充表單。 當我想獲取文本框的值時,它是空的。 這就是它的外觀,我添加了一個紅色箭頭,向您顯示我想要按下按鈕的信息
這是我的前端
<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>
您在代碼中遇到了多個問題。 我認為你有靜態聲明的txtChangeQuantity
因為你想獲得當你點擊按鈕時在GridView中的txtChangeQuantity
的值。
您將GridView文本框的值分配給GridView的RowDataBound
事件中的靜態txtChangeQuantity
。 GridView的每一行都會觸發此事件。 因此,最后您將獲得靜態txtChangeQuantity
GridView的最后一個文本框的值。
當您更改GridView文本框的值時,它將不會設置為靜態txtChangeQuantity
。
此外,由於txtChangeQuantity
是靜態的,並且未在.aspx
頁面中聲明,因此當您單擊按鈕時,您將無法在后面的代碼中獲取其值。
作為其他模板控件一部分的控件事件(如GridView和Repeater)不能像您嘗試的那樣工作。
從您的代碼中,我認為您希望獲得與GridView控件中的按鈕相關聯的文本框的值來計算數量。 您可以使用以下方法執行此操作。
您需要使用GridView控件的RowCommand
事件。
只要單擊按鈕控件GridView,就會觸發RowCommand事件。
因此,您需要具有事件處理程序並為GridView的RowCommand分配。 並且您不需要Click事件處理程序confirm_purchases_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訪問其他控件。
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中的控件引發正確的處理事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.