M
MPS-MMM
Guest
I have a small demo aspx file that demonstrates a gridview which, when clicking on a row will display the row that was clicked on. This demo works, but as soon as I attempt to add a MasterPagefile, it no longer works. The GridView_SelectedIndexChanged does not fire. any help would be appreciated!
Here is the aspx that works:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" EnableEventValidation="false" %>
<!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>
<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>
</head>
<body>
<form id="form1" runat="server
<asp:Label Text="" ID="lblSelectedRow" runat="server" />
<asp:GridView runat="server" ID="GridView1"
datasourceid="CustomersSource"
AutoGenerateColumns="False"
CellPadding="4"
Font-Names="Tahoma"
Font-Size="Small"
ForeColor="#333333"
GridLines="None"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowDataBound="GridView1_RowDataBound
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
</Columns>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<br />
</form>
<asp:sqldatasource id="CustomersSource"
selectcommand="SELECT CustomerID, FirstName, MiddleName, LastName, Phone FROM SalesLT.Customer"
connectionstring="<%$ ConnectionStrings:AdventureWorksLT2008R2ConnectionString %>"
runat="server"/>
</body>
</html>
and the associated code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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));
}
}
}
But when I create a new aspx file with a Master File page, it stops working. Here uis the new code and the Master file:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" EnableEventValidation="false"%>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" 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:Label Text="" ID="lblSelectedRow" runat="server" />
<asp:GridView runat="server" ID="GridView1"
datasourceid="CustomersSource"
AutoGenerateColumns="False"
CellPadding="4"
Font-Names="Tahoma"
Font-Size="Small"
ForeColor="#333333"
GridLines="None"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
</Columns>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:sqldatasource id="CustomersSource"
selectcommand="SELECT CustomerID, FirstName, MiddleName, LastName, Phone FROM SalesLT.Customer"
connectionstring="<%$ ConnectionStrings:AdventureWorksLT2008R2ConnectionString %>"
runat="server"/>
</asp:Content>
and the associated Master file
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en
<head id="Head1" runat="server
<title>FMSCO Scorecard</title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server </asp:ContentPlaceHolder>
</head><body>
<form id="Form1" runat="server
<div class="page
<div class="header
<div class="title
<table>
<tr>
<td><img alt="" src="logo3m.gif" width="74" height ="48"/></td>
<td>
<h1>FMSCO Scorecards</h1>
</td>
</tr>
</table>
<div class="clear hideSkiplink
<asp:Menu ID="NavigationMenu" runat="server" DataSourceID="SiteMapDataSource1"
MaximumDynamicDisplayLevels="1"
Orientation="Horizontal"
CssClass="menu"
EnableViewState="False"
IncludeStyleBlock="False" onmenuitemdatabound="NavigationMenu_MenuItemDataBound"
>
<DataBindings>
<asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title"
NavigateUrlField="Url" />
</DataBindings>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
<div class="main
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
<div class="clear
<div class="footer
</form>
</body>
</html>
Continue reading...
Here is the aspx that works:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" EnableEventValidation="false" %>
<!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>
<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>
</head>
<body>
<form id="form1" runat="server
<asp:Label Text="" ID="lblSelectedRow" runat="server" />
<asp:GridView runat="server" ID="GridView1"
datasourceid="CustomersSource"
AutoGenerateColumns="False"
CellPadding="4"
Font-Names="Tahoma"
Font-Size="Small"
ForeColor="#333333"
GridLines="None"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowDataBound="GridView1_RowDataBound
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
</Columns>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<br />
</form>
<asp:sqldatasource id="CustomersSource"
selectcommand="SELECT CustomerID, FirstName, MiddleName, LastName, Phone FROM SalesLT.Customer"
connectionstring="<%$ ConnectionStrings:AdventureWorksLT2008R2ConnectionString %>"
runat="server"/>
</body>
</html>
and the associated code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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));
}
}
}
But when I create a new aspx file with a Master File page, it stops working. Here uis the new code and the Master file:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" EnableEventValidation="false"%>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" 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:Label Text="" ID="lblSelectedRow" runat="server" />
<asp:GridView runat="server" ID="GridView1"
datasourceid="CustomersSource"
AutoGenerateColumns="False"
CellPadding="4"
Font-Names="Tahoma"
Font-Size="Small"
ForeColor="#333333"
GridLines="None"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
</Columns>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:sqldatasource id="CustomersSource"
selectcommand="SELECT CustomerID, FirstName, MiddleName, LastName, Phone FROM SalesLT.Customer"
connectionstring="<%$ ConnectionStrings:AdventureWorksLT2008R2ConnectionString %>"
runat="server"/>
</asp:Content>
and the associated Master file
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en
<head id="Head1" runat="server
<title>FMSCO Scorecard</title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server </asp:ContentPlaceHolder>
</head><body>
<form id="Form1" runat="server
<div class="page
<div class="header
<div class="title
<table>
<tr>
<td><img alt="" src="logo3m.gif" width="74" height ="48"/></td>
<td>
<h1>FMSCO Scorecards</h1>
</td>
</tr>
</table>
<div class="clear hideSkiplink
<asp:Menu ID="NavigationMenu" runat="server" DataSourceID="SiteMapDataSource1"
MaximumDynamicDisplayLevels="1"
Orientation="Horizontal"
CssClass="menu"
EnableViewState="False"
IncludeStyleBlock="False" onmenuitemdatabound="NavigationMenu_MenuItemDataBound"
>
<DataBindings>
<asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title"
NavigateUrlField="Url" />
</DataBindings>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
<div class="main
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
<div class="clear
<div class="footer
</form>
</body>
</html>
Continue reading...