• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1089
  • Last Modified:

GridView problem with hidden field object using gridview

I have a gridview object that contains a hidden field component to store idValue from a database.
When I make a for loop cycle to read the value of this hidden field,  I can't read the first value (index 0) but I CAN read the other values.

The code I'm using is very simple:

        Dim x As Integer = 0
        For x = 0 To gvCleanAer.Rows.Count - 1
            Select Case x
                Case 0
                    Session("CleanAerBreeze") = CType(gvCleanAer.Rows(x).Cells(0).Controls(1), HiddenField).Value() & ";" & CInt(CType(gvCleanAer.Rows(x).Cells(3).Controls(1), TextBox).Text)
                Case 1
                    Session("CleanAerNature") = CType(gvCleanAer.Rows(x).Cells(0).Controls(1), HiddenField).Value() & ";" & CInt(CType(gvCleanAer.Rows(x).Cells(3).Controls(1), TextBox).Text)
                Case 2
                    Session("CleanAerOrchand") = CType(gvCleanAer.Rows(x).Cells(0).Controls(1), HiddenField).Value() & ";" & CInt(CType(gvCleanAer.Rows(x).Cells(3).Controls(1), TextBox).Text)
                Case 3
                    Session("CleanAerPersian") = CType(gvCleanAer.Rows(x).Cells(0).Controls(1), HiddenField).Value() & ";" & CInt(CType(gvCleanAer.Rows(x).Cells(3).Controls(1), TextBox).Text)
            End Select

If i change the HiddenField object into a textbox every value can be read correctly without any problem. Please help!

1 Solution
I know this is C# but this is how i do it....

foreach (GridViewRow row in gridView1.Rows)
      HiddenField hid = (HiddenField)row.FindControl("hidden1");

Hope this helps.

They changed it so you can't read the values of a hidden field in a GridView control like you could in a DataGrid, but there's a way round it.  Unhide your hidden field, convert it to a template, and add style="display: none" to the HTML for your control.  It'll still be hidden on the browser, but you can now read its value.
Or you can use invisble boundcolumn instead of hidden field - BoundColumn.Visible =  False. Then you can get the value like this:
In Asp.Net no values can be read out from the hidden fields.  Instead, you change the hidden field to label and set the forecolor of the label equivalent to the background color of each grid row.  But better you mind do you have different formatting for Item and AlternateItem rows.

In this way, you can read values by using the same FindControl method, from the label which is invisible.  

- Suresh
style="display: none" will have the same effect as setting the background colour, but with the benefits that you don't have to mess around working out what colour the background is, and your key values won't be displayed to anyone cursor-selecting that part of your HTML.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now