Set DataList SelectedIndex - 2nd Post

Discussion in 'ASP .Net' started by David D. McCrory, Jan 20, 2004.

  1. This is the second post for this question....please help!!

    This is a fairly simple request....I am looking for some help....

    I want to set the SelectedIndex of a DataList control to equal a specific
    record in the database. What is the easiest way to accomplish this??


    Thanks,
    David D. McCrory
    David D. McCrory, Jan 20, 2004
    #1
    1. Advertising

  2. David D. McCrory wrote:
    > This is the second post for this question....please help!!
    >
    > This is a fairly simple request....I am looking for some help....
    >
    > I want to set the SelectedIndex of a DataList control to equal a specific
    > record in the database. What is the easiest way to accomplish this??
    >
    >
    > Thanks,
    > David D. McCrory
    >
    >


    I would add a handler for the ItemDataBound event of the DataList and
    then the event args give you access to the current item and its data.

    e.Item.DataItem gives the current item in the datasource; use this to
    check for a certain value
    e.Item.ItemIndex gives the current item's index

    I believe you can set the selectedindex in this event; if not, set a
    local/member var to the value and then after the DataBind, set it.

    The other solution is to loop the items after the DataBind instead of
    using the event, but doing the first way does one less loop.

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
    Craig Deelsnyder, Jan 20, 2004
    #2
    1. Advertising

  3. Thanks Craig...I will give it a try......

    "Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
    news:...
    > David D. McCrory wrote:
    > > This is the second post for this question....please help!!
    > >
    > > This is a fairly simple request....I am looking for some help....
    > >
    > > I want to set the SelectedIndex of a DataList control to equal a

    specific
    > > record in the database. What is the easiest way to accomplish this??
    > >
    > >
    > > Thanks,
    > > David D. McCrory
    > >
    > >

    >
    > I would add a handler for the ItemDataBound event of the DataList and
    > then the event args give you access to the current item and its data.
    >
    > e.Item.DataItem gives the current item in the datasource; use this to
    > check for a certain value
    > e.Item.ItemIndex gives the current item's index
    >
    > I believe you can set the selectedindex in this event; if not, set a
    > local/member var to the value and then after the DataBind, set it.
    >
    > The other solution is to loop the items after the DataBind instead of
    > using the event, but doing the first way does one less loop.
    >
    > --
    > Craig Deelsnyder
    > Microsoft MVP - ASP/ASP.NET
    David D. McCrory, Jan 20, 2004
    #3
    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. David D. McCrory

    Set DataList SelectedIndex to data record

    David D. McCrory, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    391
    Alvin Bruney
    Jan 24, 2004
  2. Steve Lloyd
    Replies:
    1
    Views:
    699
    Rick Spiewak
    May 26, 2004
  3. Nevyn Twyll
    Replies:
    8
    Views:
    1,867
  4. Les Caudle
    Replies:
    0
    Views:
    1,987
    Les Caudle
    May 2, 2006
  5. Jules
    Replies:
    1
    Views:
    4,752
Loading...

Share This Page