Results 1 to 11 of 11

Thread: Creating Session Values from Gridview?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Creating Session Values from Gridview?

    I have a gridview that has a hidden column called clientID. The gridview itself has a DataKeyName called Prop_ID.

    When a user selects a row from the gridview, I store the selected rows key value in a session variable as follows

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    Session("PropertyID") = GridView1.SelectedValue

    Response.Redirect("~/PagesEDC/aaa.aspx")
    End Sub

    Q - How do I also store the value of the hidden column in a session variable?

    Thanks in advance

    Jim

  2. #2
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Creating Session Values from Gridview?

    You can create an object for the selected row and call the index of the hidden column like so:

    VB Code:
    1. Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    2.  
    3.     Dim row as GridViewRow.SelectedRow = GridView1.SelectedRow
    4.  
    5.     'set the index of your hidden value here, in my case its the first column, or index 0.  Then stores session value
    6.     Session("PropertyID") = row.Cells(0).Text
    7.  
    8.     Response.Redirect("~/PagesEDC/aaa.aspx")
    9.  
    10. End Sub

    Hope this helps!!!
    Last edited by drpcken; Apr 26th, 2006 at 01:31 PM. Reason: code typo

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Creating Session Values from Gridview?

    Thanks but no go. Unfortunately the IDE doesn't like .SelectedRow as an attribute of GridViewRow in fact .Selected Row doesn't even appear in the intelisense. Any other ideas?

  4. #4
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Creating Session Values from Gridview?

    sorry i'm using VS 2005
    what version you using?

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  5. #5
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Creating Session Values from Gridview?

    Also show me the code for your SelectedIndexChanged handler please

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Creating Session Values from Gridview?

    I'm using VS 2005 also. The selected index handler is as follows:


    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

    Session("PropertyID") = GridView1.SelectedValue

    Response.Redirect("~/PagesEDC/aaa.aspx")

    End Sub

  7. #7
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Creating Session Values from Gridview?

    hmm, here's my code that work without intellisense acting up:

    VB Code:
    1. Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    2.  
    3.         Dim gridrow As GridViewRow = GridView1.SelectedRow
    4.  
    5.         Session.Add("Session", gridrow.Cells(0).Text)
    6.  
    7.         Response.Redirect("~/PagesEDC/aaa.aspx")
    8.  
    9. End Sub

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Creating Session Values from Gridview?

    Thanks for the quick reply. I think you're heading in the right directin, but still no success. I tried the following

    Dim gridrow As GridViewRow = GridView1.SelectedRow

    Session.Add("test", gridrow.Cells(5).Text)

    Session.Add("Test2", gridrow.Cells(11).Text)

    Session("ClientID") = gridrow.Cells(11).Text
    Session("PropertyID") = GridView1.SelectedValue

    But in the immediate window it always shows "" as a value as if nothing is being passed into the session variable

    I'm somewhat of a rookie so an hand holding is greatly appreciated

  9. #9
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Creating Session Values from Gridview?

    Session("PropertyID") = GridView1.SelectedValue

    you're still not telling it which index to fill this session with

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  10. #10
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Creating Session Values from Gridview?

    Which column of the selectedIndex is the ProperyID in? The first one?

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Creating Session Values from Gridview?

    Here's what I have done:

    In global.asax I have defined the following:

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    Session("SessionLanguage") = "en-us"
    Session("ClientID") = ""
    Session("ClientName") = ""
    Session("PropertyID") = ""
    End Sub

    When I drug in the gridview I assigned its DataKeyNames as PROP_ID, which is a field from my DB

    When a row in the gridview is selected it passes the ID from that ruw into the session variable using Session("PropertyID") = GridView1.SelectedValue, and that works fine without any issues.

    This is how I was taught to do it, but I was never taught how to reference in a second value

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width