Session variable from gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soonerpgh
    New Member
    • Apr 2008
    • 23

    Session variable from gridview

    Hello, folks. I have been fighting with this issue for a few weeks and cannot seem to get anywhere with it. Thus, I have decided to give you guys as much possible information as I can get into a post and hopefully someone will be able to see what I am doing wrong.

    First of all, I am using ASP.NET 2.0. I have a gridveiw and a sqldatasource.



    The first field, ClientID is a template with a button, obviously. When I click the button, I want the text value, in this case, "2", sent to the session variable.

    The gridview is named grdSearch with the DataKey property set to ClientID, and the button, while I named it btnSelect, the .NET framework chooses to still create the click event as Button1_Click.

    At any rate, here is the code-behind that I have tried (along with a dozen or so variations) but I still get the "Unhandled Null Exeption".

    Protected Sub Button1_Click(B yVal sender As Object, ByVal e As System.EventArg s)
    Session("Client ID") = grdSearch.Selec tedDataKey.ToSt ring
    End Sub

    What the heck am I doing wrong?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Don't you need to use the Session.Add() function to add things to the Session varriable?
    I also am not sure that the selected item will update until AFTER the event is fired?

    As for why it's Button1_click, if you assign an event to a control before renaming, it does NOT rename the events to follow the new name.

    Comment

    • soonerpgh
      New Member
      • Apr 2008
      • 23

      #3
      "Don't you need to use the Session.Add() function to add things to the Session varriable?"

      That's possible. This is my first venture in ASP so I am not sure of much of anything at this point, other than my own inadequacies.

      "I also am not sure that the selected item will update until AFTER the event is fired?"

      I don't follow this one at all. I am not wanting to update the selected item. I just want to pull the data from that field into the session variable. Perhaps I misunderstood the statement.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well you are using the "SelectedDataKe y" property to get data.
        That property is only updated after all the event handlers and such fires.
        So for instance the first time, SelectedDataKey is probably null?

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          This problem isn't making sense to me.
          I don't know how it's possible that your Button1_Click sub is being executed.
          Aren't you creating many buttons in your GridView?

          Have you set up your GridView to use the OnRowCommand event?

          The OnRowCommand event is fired every time an event (like button click) originates from your GridView...it is here that you should be figuring out what button was clicked and what value to store in Session.

          Comment

          Working...