Click here to Skip to main content
15,445,242 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

C#
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)
           });
           GridView1.DataBind();
       }
   }
   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
   {
       lblSelectedRow.Text = String.Format("You selected row {0} with {1} {2}",
                                               GridView1.SelectedIndex + 1,
                                               GridView1.SelectedRow.Cells[0].Text,
                                               GridView1.SelectedRow.Cells[1].Text);
   }
   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.
           e.Row.Attributes.Add("onclick",
           String.Format("javascript:__doPostBack('GridView1','Select${0}')",  e.Row.RowIndex));
       }
   }


here is the markup

XML
<%@ 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;
    }
    .Normal
    {
        background-color: #EFF3FB;
        cursor: hand;
    }
    .Normal:Hover, .Alternate:Hover
    {
        background-color: #D1DDF1;
        cursor: hand;
    }
    .Alternate
    {
        background-color: White;
        cursor: hand;
    }
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <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"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField HeaderText="Row">
                    <ItemTemplate>
                <%# Container.DataItemIndex+1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                <asp:CommandField ButtonType="Link" SelectText="Enroll"     ShowSelectButton="true"
                    Visible="false" />
            </Columns>
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"     />
        </asp:GridView>
        <asp:Label ID="lblSelectedRow" runat="server" Text="" />
    </ContentTemplate>
</asp:UpdatePanel>
<br />
</asp:Content>
Posted
Updated 31-May-21 22:01pm
v2

try this...

protected void grd_Distributor_RowCreated(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",

"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FF9955';");

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

e.Row.Attributes.Add("onmouseout",

"this.style.backgroundColor=this.originalstyle;");


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
 
Comments
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
 
Comments
Richard Deeming 1-Jun-21 10:36am     CRLF
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