datakeynames, datatable and code behind

Last post 09-28-2007 8:52 AM by pettrer. 2 replies.

Sort Posts:

  • datakeynames, datatable and code behind

    09-27-2007, 2:38 PM
    • Participant
      891 point Participant
    • pettrer
    • Member since 02-08-2006, 4:08 AM
    • Stockholm, Sweden
    • Posts 330

    Hi,

    I have a gridview that is being databound from a DataTable from code behind:

       PosterGV.DataSource = dt

       PosterGV.DataBind()

    This works fine.

    I need to get the values from the row in which a linkbutton is clicked, and this works too:

       Protected Sub Andra_post(ByVal sender As Object, ByVal e As System.EventArgs)

          Dim valdLB As LinkButton = TryCast(sender, LinkButton)

          Dim gvr As GridViewRow = TryCast(valdLB.NamingContainer, GridViewRow)

          Dim i As Integer = gvr.RowIndex - 4 'Minus four

          KarADDL.SelectedValue = CInt(gvr.Cells(0).Text)

          BeloTB.Text = gvr.Cells(1).Text

    Et cetera... The problem is that two of the values must be hidden. Thus I try to use datakeynames, but can't get it to work. I have set the dkn in the gridview tag:

       DataKeyNames="inkl, sats"

    I have also set them in Page.Load:

       PosterGV.DataKeyNames = New String() {"inkl", "sats"}

     

    ...as well as in the datatable:

       dt.Columns.Add("inkl")

       dt.Columns.Add("sats")

    I know they are in the datatable because they show up in the gridview.

     

    Now, when I try to retrieve the values in the sub shown above, it won't work (it worked when they were visible normal values). 

    This is my code for now:

       myDDL.SelectedIndex = CInt(PosterGV.SelectedDataKey(0).Value.ToString())

     

    I cant understand what's wrong, but I'm sure it has to do with the change to dkn. The error I get says the following:

    System.NullReferenceException was unhandled by user code
      Message="Objektreferensen har inte angetts till en instans av ett objekt." (Meaning: The object reference has not been assigned an instance of an object)
      Source="App_Web_l_icdvjb"
      StackTrace:
           vid eko_reg.Andra_post(Object sender, EventArgs e) i C:\Documents and Settings\ ... .aspx.vb:rad 790
           vid System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
           vid System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
           vid System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
           vid System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
           vid System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
           vid System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
     

    Please help me out! (I've tried setting the dkn of the gridview after the datasource has been defined, but to no avail.)

    Pettrer, Stockholm

    Coding is a nine-to-five job: Nine PM to Five AM.
  • Re: datakeynames, datatable and code behind

    09-27-2007, 3:42 PM
    • Participant
      945 point Participant
    • skills0
    • Member since 07-29-2002, 3:29 PM
    • Chicago IL
    • Posts 178

    I don't think you can use a comma separated list as a data key.

     Try putting one of the values in a hidden control and use FindControl to get at the information and build the key back up yourself.
     

    Curtis Swartzentruber / CLARITY CONSULTING
    http://blogs.claritycon.com/blogs/curtis_swartzentruber
    Work for us: http://www.claritycon.com/careers/
    Personal Blog: http://feeds.feedburner.com/swartzentruber/QFjK
  • Re: datakeynames, datatable and code behind

    09-28-2007, 8:52 AM
    Answer
    • Participant
      891 point Participant
    • pettrer
    • Member since 02-08-2006, 4:08 AM
    • Stockholm, Sweden
    • Posts 330

    Hi,

    Thanks for your reply. It should indeed be possible to have several datakeynames (according to msdn), but I still can't get it to work. I started out by making it work with hiddenfields in an itemtemplate of a gridview's column and got it to work, but the colums still show. I can work around that, but it seems like a rather messy way to accomplish this.

    Best,
    Pettrer

    Coding is a nine-to-five job: Nine PM to Five AM.
Page 1 of 1 (3 items)