please try this: dont forget to insert command filed on gridview, generate the update,insert and delete commands and then insert there your commands to finalize convert it to templatefields when you click on gridview in design time when you convert to templatefilds the insert update and delete check boxes were unchecj but command still there and then this on code behind:
{
int idartigo = (int)GridView2.DataKeys[e.RowIndex].Value;
//this will get the id when you click on it... this example is for rowdeleting event.
for row updating event use this:
if (GridView2.EditIndex == -1) return;
int idartigo = Convert.ToInt32(GridView2.DataKeys[GridView2.EditIndex][0]);
The code block $("table[id*=GridView1] tr+tr").click(function(){, is finding all the gridview rows starting from the second row, that is except header. The
tr+tr is the selector. And adding a click event.
The code block __doPostBack('<%= Button1.ClientID %>',"OnClick") is firing the OnClick event of the hidden button.
Thanks & Regards
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
-_-
Member
376 Points
1046 Posts
click row on gridview and trigger postback c#
Mar 24, 2011 02:34 PM|LINK
i have a gridview, i don't know how to trigger postback when the user click any row on the gridview .
i use GetPostBackEventReference, but it causes error
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: click row on gridview and trigger postback c#
Mar 24, 2011 02:40 PM|LINK
Here's an example:
ASPX
CODE-BEHIND
using System; using System.Web.UI; using System.Web.UI.WebControls; public partial class GridView_SelectRowWithoutSelectCommand : Page { protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = String.Format("You selected product '{0}'.", GridView1.SelectedDataKey["ProductName"]); } protected override void Render(HtmlTextWriter writer) { const string onMouseOverStyle = "this.className='GridViewMouseOver';"; const string onMouseOutStyle = "this.className='{0}';"; foreach (GridViewRow gvr in GridView1.Rows) { gvr.Attributes["onmouseover"] = onMouseOverStyle; gvr.Attributes["onmouseout"] = String.Format( onMouseOutStyle, this.GetRowStyleCssClass(gvr.RowState)); gvr.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink( GridView1, String.Concat("Select$", gvr.RowIndex), true); } base.Render(writer); } private string GetRowStyleCssClass(DataControlRowState state) { if ((state & DataControlRowState.Edit) > 0) { return GridView1.EditRowStyle.CssClass; } else if ((state & DataControlRowState.Selected) > 0) { return GridView1.SelectedRowStyle.CssClass; } else if ((state & DataControlRowState.Alternate) > 0) { return GridView1.AlternatingRowStyle.CssClass; } else { return GridView1.RowStyle.CssClass; } } }STYLESHEET
/* Element Styles */ body { background-color: #ffffff; color: #000000; font: normal normal normal 12px/20px Verdana, Geneva, Arial, Helvetica, sans-serif; margin: 10px; } pre { background: #FFFFB3; border: solid 1px blue; color: blue; padding:10px; margin: 10px; } /* DetailsView Styles */ .DetailsView { border: solid 1px #6699cc; margin-bottom: 12px; } .DetailsViewHeader { background-color: #6699cc; color: #ffffff; font-size: 125%; font-weight: bold; text-align: center; } .DetailsViewFieldHeader { font-weight: bold; text-align: right; } .DetailsViewCommandRow { text-transform: lowercase; } /* FormView Styles */ .FormView { border: solid 1px #6699cc; margin-bottom: 12px; } /* GridView Styles */ .GridView { border: solid 1px #6699cc; margin: 0px 0px 20px 0px; } .GridViewHeader { background: #6699cc; color: #ffffff; font-size: 14px; font-weight: bold; text-align: left; vertical-align: bottom; } .GridViewRow { background-color: #ffffff; } .GridViewAlternatingRow { background-color: #ccffff; } .GridViewEditRow { background-color: #f0e68c; } .GridViewMouseOver { background-color: #f0e68c; cursor: hand; } .GridViewSelectedRow { background-color: #f0e68c; } .GridViewFooter { background-color: #ffffff; } .GridViewPager { background-color: #6699cc; color: #ffffff; font-size: 14px; font-weight: bold; }Microsoft MVP - ASP.NET
Binary_Fiddl...
Contributor
2137 Points
374 Posts
Re: click row on gridview and trigger postback c#
Mar 24, 2011 02:46 PM|LINK
you will have to use the SelectedIndexChanged event of the grid.For more information visit
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanged.aspx
This can be beneficial to other community members reading the thread.
Regards,
Amit
Microsoft Certified Technology Specialist (MCTS)
-_-
Member
376 Points
1046 Posts
Re: click row on gridview and trigger postback c#
Mar 24, 2011 03:19 PM|LINK
anytime user click on the row , will trigger which event?
SelectedIndexChanged will not be triggerred when the user click at the first time
asteranup
All-Star
30184 Points
4906 Posts
Re: click row on gridview and trigger postback c#
Mar 27, 2011 03:06 PM|LINK
Hi,
You can do something like below-
HTML-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="calculating total inside a gridview.aspx.cs" EnableEventValidation="false" Inherits="calculating_total_inside_a_gridview" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("table[id*=GridView1] tr+tr").click(function () { __doPostBack('<%= Button1.ClientID %>', "OnClick") }); }); </script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" > <Columns> <asp:TemplateField HeaderText="Quantity" SortExpression="Quantity"> <ItemTemplate> <asp:TextBox ID="Quantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Price" DataField="Price" ItemStyle-CssClass="selectorClass" /> <asp:TemplateField HeaderText="Total" SortExpression="Total"> <ItemTemplate> <asp:Label ID="TotalAmount" runat="server" ></asp:Label> </ItemTemplate> <FooterTemplate> <asp:Label ID="GrandTotal" runat="server" ></asp:Label> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" style="display:none" /> </form> </body> </html>Code-
protected void Page_Load(object sender, EventArgs e) { ClientScript.GetPostBackEventReference(Button1, ""); if (!IsPostBack) { var list = (new[]{new{Quantity="", Price=300}, new{Quantity="", Price=400}, new{Quantity="", Price=500}, new{Quantity="", Price=600}, new{Quantity="", Price=700}, new{Quantity="", Price=800}}).ToList(); GridView1.DataSource = list; GridView1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { // on row click the button click will get fired }Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
-_-
Member
376 Points
1046 Posts
Re: click row on gridview and trigger postback c#
Mar 27, 2011 03:25 PM|LINK
sorry, i can't understand the code
hsl89
Member
476 Points
441 Posts
Re: click row on gridview and trigger postback c#
Mar 27, 2011 04:22 PM|LINK
asteranup
All-Star
30184 Points
4906 Posts
Re: click row on gridview and trigger postback c#
Mar 28, 2011 05:51 AM|LINK
OK.
Let me explain a little-
You want to fire some codebehind on row click.
The code block $("table[id*=GridView1] tr+tr").click(function () {, is finding all the gridview rows starting from the second row, that is except header. The tr+tr is the selector. And adding a click event.
The code block __doPostBack('<%= Button1.ClientID %>', "OnClick") is firing the OnClick event of the hidden button.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog