从后面的代码获取gridview值

发布于 2024-12-27 09:08:46 字数 1531 浏览 2 评论 0原文

我想从代码隐藏中获取网格视图中隐藏字段的值,但不要在 _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经暖 2025-01-03 09:08:46

对于 GridView 中的每一行,都有一个用于 ProductID 的 HiddenField

您可以使用以下代码访问一行的 HiddenField(在第一行下方的示例中)(假设您的 HiddenField 位于第一个单元格中):

HiddenField hiddenFieldProductID = 
           (HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");

string productID = hiddenFieldProductID.Value

// Do something with the value

希望,这个有帮助。

For each row in your GridView there is a HiddenField for the ProductID.

You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):

HiddenField hiddenFieldProductID = 
           (HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");

string productID = hiddenFieldProductID.Value

// Do something with the value

Hope, this helps.

疏忽 2025-01-03 09:08:46

尝试将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文