Reputation: 1083
I have a GridView
which has ID
, Name
and Type
.
Code behind:
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gridView, "Select$" + e.Row.RowIndex);
}
}
public void gridView_Command(object sender, GridViewCommandEventArgs e)
{
// some code here //
}
The GridView
:
<asp:GridView ID="gridView" runat="server" OnRowCommand="gridView_Command" />
<columns>
<asp:BoundField DataField="ID" />
<asp:HyperLinkField DataTextField="Name" />
<asp:BoundField DataField="Type" />
</columns>
The Name
is click enabled because of the HyperlinkField. The problem here is I don't want the gridview_Command
to be triggered if the row is clicked. I just want the Name
field. For example, if the column of the Type
is clicked, don't fire the gridView_Command
.
Upvotes: 3
Views: 3786
Reputation: 56849
It is not that clear what you are trying to achieve, but if I understand correctly there are a few things you need to change.
<asp:Gridview>
tag.
<asp:GridView ID="gridView" runat="server" OnRowDataBound="gridView_RowDataBound" OnRowCommand="gridView_Command" AutoGenerateColumns="false" >
<columns>
<asp:BoundField DataField="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" runat="server" Text='<%# Eval("Name") %>' CommandName="NameButton" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Type" />
</columns>
</asp:GridView>
public void gridView_Command(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "NameButton")
{
var id = e.CommandArgument;
// some code here //
}
}
Using this example, you can respond to the event of a click on the "NameButton" only.
Upvotes: 1