Corin
Corin

Reputation: 2457

Making rows containing DropDownList in a GridView clickable, breaks DropDownList

I've made the rows in a GridView clickable using the following code and a RaisePostBackEvent function.

protected void gvResults_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(this, e.Row.RowIndex.ToString()));
    }
}

The GridView also has a DropDownList control for one of the cells. The problem is that clicking on the DropDownList to change the selection also triggers the onclick of the row. This results in the option list for the DropDownList being visible but unusable as the onclick for the row has also been processed.

I tried setting a flag in the onmouseover of the DropDownList and checking that in the RaisePostBackEvent function but that was only a partial success. That prevented the functionality in the RaisePostBackEvent but row onclick still occurred meaning the DropDownList was still in an unusable state.

So how do I make the DropDownList function while still allowing the onclick to function when other parts of the row are clicked?

Note: I'm currently restricted working with .NET 2.0.

Upvotes: 0

Views: 451

Answers (1)

jmaglio
jmaglio

Reputation: 776

What you want to do is stop propagation of the Click event. The easiest way to do this would be to wrap the elements in a div/span element that stops propagation :

 <div onclick="if (event.stopPropagation){ event.stopPropagation(); } else if(window.event){ window.event.cancelBubble=true; } ">
 DropDownList element here...
 </div>

IE uses the event.cancelBubble property, while other browsers use stopPropagation.

I'm not sure how you're generating the DropDownList elements in your Gridview - you could either add this in the ItemTemplate or find a way to add this around the elements when they are created in the RowCreated event.

Upvotes: 1

Related Questions