get datakey from datalist

Discussion in 'ASP .Net' started by Andy Sutorius, Mar 3, 2005.

  1. Hi,

    I have a datalist with a asp:button. I have onitemcommand firing when the
    button is clicked. I am attempting to get the datakey of the button that was
    clicked. I can retrieve a specific datakey with the following: int
    intProductID = Convert.ToInt16(dlstProducts.DataKeys[0]) but how do I
    retrieve the datakey of the button clicked? I have tried this
    unsuccessfully: int intProductID = Convert.ToInt16(dlstProducts.DataKeys[e])

    Thanks,

    Andy
     
    Andy Sutorius, Mar 3, 2005
    #1
    1. Advertising

  2. Andy Sutorius

    Karl Seguin Guest

    use e.Item.ItemIndex to get the index of the item

    dlstProducts.DataKeys[e.Item.ItemIndex]

    also, any particular reason you are using ToInt16 instead of 32?

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Andy Sutorius" <> wrote in message
    news:%RIVd.31239$...
    > Hi,
    >
    > I have a datalist with a asp:button. I have onitemcommand firing when the
    > button is clicked. I am attempting to get the datakey of the button that

    was
    > clicked. I can retrieve a specific datakey with the following: int
    > intProductID = Convert.ToInt16(dlstProducts.DataKeys[0]) but how do I
    > retrieve the datakey of the button clicked? I have tried this
    > unsuccessfully: int intProductID =

    Convert.ToInt16(dlstProducts.DataKeys[e])
    >
    > Thanks,
    >
    > Andy
    >
    >
     
    Karl Seguin, Mar 3, 2005
    #2
    1. Advertising

  3. Hi Karl,

    Why should it be ToInt32?

    Andy


    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > use e.Item.ItemIndex to get the index of the item
    >
    > dlstProducts.DataKeys[e.Item.ItemIndex]
    >
    > also, any particular reason you are using ToInt16 instead of 32?
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/ - New and Improved (yes, the popup is
    > annoying)
    > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    > come!)
    > "Andy Sutorius" <> wrote in message
    > news:%RIVd.31239$...
    > > Hi,
    > >
    > > I have a datalist with a asp:button. I have onitemcommand firing when

    the
    > > button is clicked. I am attempting to get the datakey of the button that

    > was
    > > clicked. I can retrieve a specific datakey with the following: int
    > > intProductID = Convert.ToInt16(dlstProducts.DataKeys[0]) but how do I
    > > retrieve the datakey of the button clicked? I have tried this
    > > unsuccessfully: int intProductID =

    > Convert.ToInt16(dlstProducts.DataKeys[e])
    > >
    > > Thanks,
    > >
    > > Andy
    > >
    > >

    >
    >
     
    Andy Sutorius, Mar 3, 2005
    #3
  4. Andy Sutorius

    Karl Seguin Guest

    in my opinion yes...assuming ur developing on 32-bit system...

    a c# int (or vb.net integer) maps to Int32 on a 32bit system so it's what
    you'll get the most performance out of...

    Int16 is only useful when you have very large chunks of data (large int
    arrays) and you want to consume less space....

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Andy Sutorius" <> wrote in message
    news:g5KVd.31243$...
    > Hi Karl,
    >
    > Why should it be ToInt32?
    >
    > Andy
    >
    >
    > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    > wrote in message news:...
    > > use e.Item.ItemIndex to get the index of the item
    > >
    > > dlstProducts.DataKeys[e.Item.ItemIndex]
    > >
    > > also, any particular reason you are using ToInt16 instead of 32?
    > >
    > > Karl
    > >
    > > --
    > > MY ASP.Net tutorials
    > > http://www.openmymind.net/ - New and Improved (yes, the popup is
    > > annoying)
    > > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    > > come!)
    > > "Andy Sutorius" <> wrote in message
    > > news:%RIVd.31239$...
    > > > Hi,
    > > >
    > > > I have a datalist with a asp:button. I have onitemcommand firing when

    > the
    > > > button is clicked. I am attempting to get the datakey of the button

    that
    > > was
    > > > clicked. I can retrieve a specific datakey with the following: int
    > > > intProductID = Convert.ToInt16(dlstProducts.DataKeys[0]) but how do I
    > > > retrieve the datakey of the button clicked? I have tried this
    > > > unsuccessfully: int intProductID =

    > > Convert.ToInt16(dlstProducts.DataKeys[e])
    > > >
    > > > Thanks,
    > > >
    > > > Andy
    > > >
    > > >

    > >
    > >

    >
    >
     
    Karl Seguin, Mar 3, 2005
    #4
  5. Karl,

    After putting in the snippet of code I receive the following error: "Object
    reference not set to an instance of an object."

    Line 63: // Obtain current user's shopping cart ID
    Line 64: String cartId = cart.GetShoppingCartId();
    Line 65:
    Line 66: // Add Product Item to Cart
    Line 67: int intProductID =
    Convert.ToInt32(dlstProducts.DataKeys[e.Item.ItemIndex]);


    Source File: c:\inetpub\wwwroot\psyche\boutique\default.aspx.cs Line: 65

    I'm stumped why the Convert statement isn't resolving this error.

    Andy



    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > use e.Item.ItemIndex to get the index of the item
    >
    > dlstProducts.DataKeys[e.Item.ItemIndex]
    >
    > also, any particular reason you are using ToInt16 instead of 32?
    >
    > Karl
    >
     
    Andy Sutorius, Mar 3, 2005
    #5
  6. Andy Sutorius

    Karl Seguin Guest

    A little hard to troubleshoot since line 65 appears blank :)

    there are only 4 possibilities:
    cart is null
    dlstProducts is null
    e is null
    e.Item is null

    it's imossible for me to tell you which it is with what you've given
    me...try stepping through your code and put a watch on each of those
    items...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Andy Sutorius" <> wrote in message
    news:dOKVd.31247$...
    > Karl,
    >
    > After putting in the snippet of code I receive the following error:

    "Object
    > reference not set to an instance of an object."
    >
    > Line 63: // Obtain current user's shopping cart ID
    > Line 64: String cartId = cart.GetShoppingCartId();
    > Line 65:
    > Line 66: // Add Product Item to Cart
    > Line 67: int intProductID =
    > Convert.ToInt32(dlstProducts.DataKeys[e.Item.ItemIndex]);
    >
    >
    > Source File: c:\inetpub\wwwroot\psyche\boutique\default.aspx.cs Line:

    65
    >
    > I'm stumped why the Convert statement isn't resolving this error.
    >
    > Andy
    >
    >
    >
    > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    > wrote in message news:...
    > > use e.Item.ItemIndex to get the index of the item
    > >
    > > dlstProducts.DataKeys[e.Item.ItemIndex]
    > >
    > > also, any particular reason you are using ToInt16 instead of 32?
    > >
    > > Karl
    > >

    >
    >
     
    Karl Seguin, Mar 4, 2005
    #6
  7. Karl,

    After a good nights sleep I stepped through and it worked perfectly. I must
    have forgotten to rebuild or something. Sorry to lead you on a wild goose
    chase.

    Thanks for your help!

    Andy


    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:%...
    > A little hard to troubleshoot since line 65 appears blank :)
    >
    > there are only 4 possibilities:
    > cart is null
    > dlstProducts is null
    > e is null
    > e.Item is null
    >
    > it's imossible for me to tell you which it is with what you've given
    > me...try stepping through your code and put a watch on each of those
    > items...
    >
    > Karl
     
    Andy Sutorius, Mar 4, 2005
    #7
  8. Andy Sutorius

    Karl Seguin Guest

    glad to hear it!

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Andy Sutorius" <> wrote in message
    news:ab%Vd.31416$...
    > Karl,
    >
    > After a good nights sleep I stepped through and it worked perfectly. I

    must
    > have forgotten to rebuild or something. Sorry to lead you on a wild goose
    > chase.
    >
    > Thanks for your help!
    >
    > Andy
    >
    >
    > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    > wrote in message news:%...
    > > A little hard to troubleshoot since line 65 appears blank :)
    > >
    > > there are only 4 possibilities:
    > > cart is null
    > > dlstProducts is null
    > > e is null
    > > e.Item is null
    > >
    > > it's imossible for me to tell you which it is with what you've given
    > > me...try stepping through your code and put a watch on each of those
    > > items...
    > >
    > > Karl

    >
    >
     
    Karl Seguin, Mar 4, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    0
    Views:
    5,391
  2. MasterChief

    Get DataKey on RowDataBound

    MasterChief, Feb 15, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    73,582
    siddersscfc
    Aug 9, 2011
  3. Jim in Arizona
    Replies:
    1
    Views:
    3,227
    Jim in Arizona
    Sep 18, 2006
  4. jc
    Replies:
    1
    Views:
    1,178
    Teresita Núñez - SorCereSs -
    Mar 6, 2008
  5. Anil TG

    Problem With DataList Datakey

    Anil TG, Aug 19, 2009, in forum: ASP .Net
    Replies:
    2
    Views:
    718
    Anil TG
    Aug 20, 2009
Loading...

Share This Page