【发布时间】: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 事件中为其赋值吗?