Click here to Skip to main content
15,798,592 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a gridview in an aspx page. I have made it row selection by javascript. The selectedindexchanged event is not working when i am using master page. Otherwise it works fine. Is there any solution for this?

Please see my code behind

public partial class WebForm2 : System.Web.UI.Page
   protected void Page_Load(object sender, EventArgs e)
       if (!IsPostBack)
           GridView1.DataSource = Enumerable.Range(1, 10).Select(a => new
               ID = a,
               FirstName = String.Format("First Name {0}", a),
               LastName = String.Format("Last Name {0}", a)
   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
       lblSelectedRow.Text = String.Format("You selected row {0} with {1} {2}",
                                               GridView1.SelectedIndex + 1,
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       if (e.Row.RowType == DataControlRowType.DataRow)
           //add css to GridViewrow based on rowState
           e.Row.CssClass = e.Row.RowState.ToString();
           //Add onclick attribute to select row.
           String.Format("javascript:__doPostBack('GridView1','Select${0}')",  e.Row.RowIndex));

here is the markup

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"      CodeBehind="WebForm2.aspx.cs"      Inherits="SelectGridRow.WebForm2" EnableEventValidation="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
 <style type="text/css">
    body, html
        font-family: Tahoma;
        font-size: small;
        background-color: #EFF3FB;
        cursor: hand;
    .Normal:Hover, .Alternate:Hover
        background-color: #D1DDF1;
        cursor: hand;
        background-color: White;
        cursor: hand;
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            CellPadding="4" DataKeyNames="ID" Font-Names="Tahoma" Font-Size="Small"
            ForeColor="#333333" GridLines="None"     OnRowDataBound="GridView1_RowDataBound"
                <asp:TemplateField HeaderText="Row">
                <%# Container.DataItemIndex+1 %>
                <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                <asp:CommandField ButtonType="Link" SelectText="Enroll"     ShowSelectButton="true"
                    Visible="false" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"     />
        <asp:Label ID="lblSelectedRow" runat="server" Text="" />
<br />
Updated 31-May-21 23:01pm

try this...

protected void grd_Distributor_RowCreated(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)


e.Row.Attributes.Add("style", "cursor:pointer;");



e.Row.ToolTip = "Click to select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.grd_Distributor, "Select$" + e.Row.RowIndex);

Share this answer
Rinshad Hameed 13-Jun-13 1:06am    
Wow great.. Its really working . u made my day..
_RatneshDubey 14-Jun-13 9:26am    
nice +5 from me
use GridView1_SelectedIndexChanging Event instead of GridView1_SelectedIndexChanged Event
Share this answer
If you use commandbutton for your gridview, write your button's commandname anything like select etc...
Share this answer
Richard Deeming 1-Jun-21 10:36am    
You are answering a solved question from eight years ago. But your solution doesn't relate to anything shown in the question!

Stick to answering newer questions, and make sure you read the question carefully before you answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900