Link to home
Start Free TrialLog in
Avatar of anthony6482
anthony6482

asked on

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
        Next

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

Avatar of osiris247
osiris247
Flag of United States of America image

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.

Steve
ASKER CERTIFIED SOLUTION
Avatar of Proactivation
Proactivation
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of the_paab
the_paab

Or you can use invisble boundcolumn instead of hidden field - BoundColumn.Visible =  False. Then you can get the value like this:
Response.Write(gvCleanAer.Rows(x).Cells(3).Text)
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.