Answered by:
call function on grid view row

Question
-
User639567535 posted
i try to call function when i click on grid view row.. there is multiple rows in grid view when user click on row then i call function
i try this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GetVoiliations(); } }
when i click on row nothing happens
how i do this
Wednesday, June 15, 2016 5:58 AM
Answers
-
User1724605321 posted
Hi ,
If you want to click row in gridview and then call sever side function , for example, change the color of selected row , below code is for your reference :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication3.test" EnableEventValidation = "false"%> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.10.2.js"></script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView> <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton> </form> </body> </html>
Code behind :
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") }); dt.Rows.Add(1, "John Hammond", "United States"); dt.Rows.Add(2, "Mudassar Khan", "India"); dt.Rows.Add(3, "Suzanne Mathews", "France"); dt.Rows.Add(4, "Robert Schidner", "Russia"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex); e.Row.ToolTip = "Click to select this row."; } } protected void OnSelectedIndexChanged(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { if (row.RowIndex == GridView1.SelectedIndex) { row.BackColor = ColorTranslator.FromHtml("#A1DCF2"); row.ToolTip = string.Empty; } else { row.BackColor = ColorTranslator.FromHtml("#FFFFFF"); row.ToolTip = "Click to select this row."; } } }
You could also use jquery to find selected row :
http://www.aspsnippets.com/Articles/Change-Gridview-Row-Color-OnClick-without-PostBack-in-ASPNet.aspx
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 15, 2016 7:19 AM
All replies
-
User541108374 posted
Hi,
it's been quite some time I did something with this particular control but the RowDataBound event is handled whenever a row gets databound to the grid. For catching the selected row you'll need another event to handle that. Take a look at the example at https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow(v=vs.110).aspx on how to grab the selected row and act accordingly.
Grz, Kris.
Wednesday, June 15, 2016 6:14 AM -
User639567535 posted
ok now i try this
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow row = GridView1.SelectedRow; GetVoiliations(); }
but when i click on row nothing happens
Wednesday, June 15, 2016 6:20 AM -
User541108374 posted
Hi,
as I didn't see what you have for markup, check that you set the onselectedindexchanged="GridView1_SelectedIndexChanged" as well.
Also place a breakpoint in this event and see if it gets hit.
Grz, Kris.
Wednesday, June 15, 2016 6:55 AM -
User1724605321 posted
Hi ,
If you want to click row in gridview and then call sever side function , for example, change the color of selected row , below code is for your reference :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication3.test" EnableEventValidation = "false"%> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.10.2.js"></script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView> <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton> </form> </body> </html>
Code behind :
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") }); dt.Rows.Add(1, "John Hammond", "United States"); dt.Rows.Add(2, "Mudassar Khan", "India"); dt.Rows.Add(3, "Suzanne Mathews", "France"); dt.Rows.Add(4, "Robert Schidner", "Russia"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex); e.Row.ToolTip = "Click to select this row."; } } protected void OnSelectedIndexChanged(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { if (row.RowIndex == GridView1.SelectedIndex) { row.BackColor = ColorTranslator.FromHtml("#A1DCF2"); row.ToolTip = string.Empty; } else { row.BackColor = ColorTranslator.FromHtml("#FFFFFF"); row.ToolTip = "Click to select this row."; } } }
You could also use jquery to find selected row :
http://www.aspsnippets.com/Articles/Change-Gridview-Row-Color-OnClick-without-PostBack-in-ASPNet.aspx
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 15, 2016 7:19 AM -
User-1034726716 posted
but when i click on row nothing happensThe SelectedIndexChanged event of GridView will only fire once you set the ShowSelectButton command field or by setting the AutoGenerateSelect button to True in your GridView. Here's a quick example:
<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" > <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField /> <asp:BoundField /> </Columns> </asp:GridView>
then at SelectedIndexChanged event you can do:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text = GridView1.SelectedRow.Cells[1].Text; //If accessing TemplateField column then you can use FindControl method //Can your function here }
Thursday, June 16, 2016 5:44 PM