Reputation:
I have a GridView on my ASP.NET page. What I want is to execute a function when I click on the row, and I also want to change the style of this row. I don't want to use the select button of type command field. Any help please?
Upvotes: 1
Views: 2589
Reputation: 39
Check this out:
In the RowCreated event of the GridView, you're going to loop through each row as it is created:
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference([grid_name], e.Row.RowIndex))
End If
That's about as far as I've gotten. You're going to process the event in Page_Load, but I'm in the process of working on that myself without having two grids on the same page fight with eachother. Good Luck, hope this helps. It will at least get you to postback...
Upvotes: 0
Reputation: 27323
For just creating a rowclickable gridview: http://aspadvice.com/blogs/joteke/archive/2006/01/07/14576.aspx
You can make a TemplateField, add a linkbutton in there, make a CommandName="Select" and a CommandArgument like CommandArgument='<%#Eval("ID")%>'. Then you can attach an event handler to the Gridview's ItemCommand, and do all the stuff you want to do in there, by checking the commandname and commandargument in the EventArgs.
Something like this:
<asp:GridView ID="test" runat="server" onrowcommand="test_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandArgument='<%#Eval("ID") %>' CommandName="ActionName">Click here</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and in the codebehind
protected void test_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ActionName")
{
int id = int.Parse(e.CommandArgument.ToString());
//do stuff
}
}
Upvotes: 1