GridView problem with hidden field object using gridview

Posted on 2006-05-11
Last Modified: 2007-11-27
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!

Question by:anthony6482
    LVL 7

    Expert Comment

    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.

    LVL 12

    Accepted Solution

    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.
    LVL 6

    Expert Comment

    Or you can use invisble boundcolumn instead of hidden field - BoundColumn.Visible =  False. Then you can get the value like this:
    LVL 1

    Expert Comment

    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
    LVL 12

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now