Search code examples
c#asp.netdatabound

change image background on select and unselect


I want to make image red on select and blue on unselect asp.net GridView or datalist like this

enter image description here

I know onmouse and mouseout and onclick ..I tried this on Row DataBound ..

protected void DataList1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        e.Row.Attributes.Add("onclick", "this.style.backgroundColor='Red'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='Blue'");



    }
}

but i want some events for select and unselect in Asp .net row databound . is there any option available in asp.net ??


Solution

  • You could do this in the SelectedIndexChanged event, why do you need it in the RowDataBound event? Just make them all blue to begin with and go from there, changing the selected row or item's background to red.

    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataList1.SelectedItem.BackColor = Color.Red;
    }