Selecting item in DataList

J

Jeff

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top