Selecting item in DataList

Discussion in 'ASP .Net' started by Jeff, Nov 3, 2008.

  1. Jeff

    Jeff Guest

    Hi

    asp.net 2.0

    My webpage has a DataList containing a TemplateField (ImageButton). When the
    ImageButton is clicked it becomes the selected ImageButton. This works okay
    as it is. But this isn't an ideal solution, because CommandArgument in this
    solution will contain a counter (first element have value 0, then 1 etc).
    Instead I think it would be better to have some kind of Id from Car object
    stored in the CommandArgument so I easier could access to the id of the
    selected object. But then I don't know how to set the SelectedIndex when a
    ImageButton is clicked..... below is some of my code...

    protected void imgThumbnail_Command(object sender, CommandEventArgs e)
    {
    dlThumbnails.SelectedIndex = Convert.ToInt32(e.CommandArgument);
    }

    protected void dlThumbnails_ItemDataBound(object sender,
    DataListItemEventArgs e)
    {

    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem ||
    e.Item.ItemType == ListItemType.SelectedItem )
    {
    Car car = (Car)e.Item.DataItem;

    ImageButton img =
    (ImageButton)e.Item.FindControl("imgThumbnail");
    img.CommandArgument = _count.ToString();
    _count++; //_count is a class variable, int
    //some more logic here
    }

    }

    any suggestions?
     
    Jeff, Nov 3, 2008
    #1
    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. John Criswell
    Replies:
    1
    Views:
    1,417
    John Criswell
    Jul 24, 2003
  2. Nevyn Twyll
    Replies:
    8
    Views:
    1,964
  3. Gert
    Replies:
    0
    Views:
    490
  4. SStory
    Replies:
    1
    Views:
    159
    SStory
    Sep 22, 2003
  5. Guest

    Selecting the first item of a datalist

    Guest, Nov 17, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    200
Loading...

Share This Page