从后面的代码获取gridview值
我想从代码隐藏中获取网格视图中隐藏字段的值,但不要在 _RowDataBound
或任何其他类似方法中使用。这是我现在的代码(这是一个购物车场景):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
为了简洁起见,我删除了某些字段,因为它们仅用于显示。数量字段供用户输入数字以将多个产品添加到购物车。我希望在 _TextChanged
事件中访问 lblProductID
标签。在同一事件中,我尝试了
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
但它不起作用并且仅返回空值。解决办法是什么?
I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound
or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID
label in the _TextChanged
event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于
GridView
中的每一行,都有一个用于 ProductID 的HiddenField
。您可以使用以下代码访问一行的
HiddenField
(在第一行下方的示例中)(假设您的HiddenField
位于第一个单元格中):希望,这个有帮助。
For each row in your
GridView
there is aHiddenField
for the ProductID.You can access the
HiddenField
of a row (in the example below the first row) by using the following code (assuming yourHiddenField
is in the first cell):Hope, this helps.
尝试将 HiddenField 替换为标签或文本框,并将 visible 属性设置为 false。
我以前尝试过这个并且有效。
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.