I want to know how i can implement the functionality in gridview. In Gridview i have a one button field when a user click on the button its fire RowCommand and event and then i redirect the user to an other page.
Now i want to implement user can click anywhere in the row and RowCommand event fire and its redirect the user to another page. So in this scenario user dont need to click on the Button in the gridview. Just click the row which he want to see the detail
on other page.
add a gridview rowdatabound event or in this event add the e.row.attribute line:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Select Case e.Row.RowType
Case DataControlRowType.DataRow
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString))
End Select
End Sub
Please Mark as Answer if You Find Useful!
But don't expect me to do your job!
i got error overload when i put your suggestion in RowDataBound. Here is my aspc and code behind file code. Next question is when user click on the row how i can redirect to other page.
shahid.majee...
Member
620 Points
544 Posts
How to fire RowCommand event when user click anywhere in the gridview row
Nov 19, 2012 03:54 PM|LINK
Hi.
I want to know how i can implement the functionality in gridview. In Gridview i have a one button field when a user click on the button its fire RowCommand and event and then i redirect the user to an other page.
Now i want to implement user can click anywhere in the row and RowCommand event fire and its redirect the user to another page. So in this scenario user dont need to click on the Button in the gridview. Just click the row which he want to see the detail on other page.
how i can do this.
Thanks in advance
Shahid Majeed
Email: shahid.majeed@gmail.com
UstesG
Contributor
2098 Points
449 Posts
Re: How to fire RowCommand event when user click anywhere in the gridview row
Nov 19, 2012 05:18 PM|LINK
add a gridview rowdatabound event or in this event add the e.row.attribute line:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Select Case e.Row.RowType Case DataControlRowType.DataRow e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString)) End Select End SubBut don't expect me to do your job!
shahid.majee...
Member
620 Points
544 Posts
Re: How to fire RowCommand event when user click anywhere in the gridview row
Nov 19, 2012 06:49 PM|LINK
i got error overload when i put your suggestion in RowDataBound. Here is my aspc and code behind file code. Next question is when user click on the row how i can redirect to other page.
aspx gridview
<asp:GridView ID="gvUtrustning" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="dsUtrustning" onrowcommand="gvUtrustning_RowCommand" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" Width="80%" onrowdatabound="gvUtrustning_RowDataBound"> <AlternatingRowStyle CssClass="alt"/> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" /> <asp:ButtonField CommandName="Utrustning" DataTextField="Benamning" HeaderText="Benamning" /> <asp:BoundField DataField="Pris" HeaderText="Pris" SortExpression="Pris" DataFormatString="{0:N}" /> <asp:BoundField DataField="Kostnad" HeaderText="Kostnad" SortExpression="Kostnad" DataFormatString="{0:N}" /> <asp:BoundField DataField="PrisPer" HeaderText="PrisPer" SortExpression="PrisPer" /> <asp:BoundField DataField="Bonus" HeaderText="Bonus" ReadOnly="True" SortExpression="Bonus" /> <asp:BoundField DataField="Grupp" HeaderText="Grupp" SortExpression="Grupp" /> </Columns> <PagerStyle CssClass="pgr" /> </asp:GridView> <asp:SqlDataSource ID="dsUtrustning" runat="server" ConnectionString="<%$ ConnectionStrings:KontorBokningConnectionString %>" SelectCommand="SP_ShowUtrustningList" SelectCommandType="StoredProcedure"> </asp:SqlDataSource>Code Behind
protected void gvUtrustning_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Utrustning") { Response.Redirect("VisaUtrustning.aspx?UID=" + gvUtrustning.DataKeys[Convert.ToInt32(e.CommandArgument.ToString())]["ID"].ToString(), true); } } protected void gvUtrustning_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) e.Row.Attributes.Add("onclick",Page.ClientScript.GetPostBackEventReference(sender,"Select$" + e.Row.RowIndex.ToString())); }Shahid Majeed
Email: shahid.majeed@gmail.com
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: How to fire RowCommand event when user click anywhere in the gridview row
Nov 22, 2012 09:23 AM|LINK
Hi,
Refer to this approach:
<script type="text/javascript"> function RedirectTo() { window.location.href = "http://google.com"; } </script>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';"; e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; e.Row.Attributes["onclick"] = "javascript:RedirectTo()"; } }Feedback to us
Develop and promote your apps in Windows Store
shahid.majee...
Member
620 Points
544 Posts
Re: How to fire RowCommand event when user click anywhere in the gridview row
Nov 24, 2012 03:55 PM|LINK
I resolve my problem here is my changed code for RowDataBound
protected void gvRum_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("style", "cursor:pointer;"); string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString(); e.Row.Attributes["onClick"] = "location.href='VisaRum.aspx?Rumid=" + abc + "'"; } }Shahid Majeed
Email: shahid.majeed@gmail.com