简体   繁体   中英

Hidden field value in GridView

如何在asp.net的GridView中获取隐藏列的值?

Firstly in a grid view you select the column and make it a template column like this:

<asp:Template Field Header Text="SavingId" Visible="false">
    <ItemTemplate>
        <asp:Label ID = "lblSavingId" runat="server" Text ='<%#Bind(SavingId")%>' />
    </ItemTemplate>
</asp:TemplateField>

after that on the coding side you can fetch the value easily like as:

string id= (gridview1.Rows[i].FindControl("lblSavingId") as label).text;

I've actually found that a column with Visible = False won't appear in the results.

What I had to do to get the desired behavior was set the column to visible, then hide it with a style set to "visibility: hidden; display:none;". When I do this, I can access the column's value by selecting the column/row as indicated by some of the other posts.

You can also do the same thing using a hidden field if you're using a DataList, but if you're in a GridView (from your post it sounds like this is what you're doing), you may need to take a similar approach.

如果“ view”是您的GridView:

HiddenField test = view.Rows[0].Cells[0].FindControl("myHiddenField") as HiddenField

if your hidden filed in grid view itemtemplace then you can find in this way:

in item data bound event:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
    // Find the hidden field   
    HiddenField _hdn =
    (HiddenField)e.Item.FindControl("HiddenField Id Put Here"); 
}

hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM