ASP.NET C# Telerik RadGrid 在使用 site master 时为空
ASP.NET C# Telerik RadGrid is null when using site master
我正在尝试将我当前的 Web 项目转换为使用网站管理员,而不是在每个单独的页面上使用我的导航和 headers。
我在使用 site.master 的内容页面上定义了一个 RadGrid。如果我尝试启动该项目,它将在任何尝试使用 "Object Reference not set to an instance of the object."
访问 RadGrid 的 属性 时出错
但是,如果我在不使用 site.master 的情况下启动项目,它将正常加载。页面后面的代码差异为零。唯一的区别是 aspx front-end 页面使用了 ContentReplacer.
以下是我正在尝试做的一些小片段:
MainContentReplacer 中的 RadGrid 定义:
<telerik:RadGrid runat="server" ID="RadGrid1" AllowPaging="True" PageSize="25" AllowSorting="true"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound" AutoGenerateColumns="False"
AllowCustomPaging="true" PagerStyle-Mode="Advanced">
<ClientSettings Selecting-AllowRowSelect="True" EnablePostBackOnRowClick="true">
</ClientSettings>
<PagerStyle Mode="Slider" Position="TopAndBottom" PageSizeControlType="RadComboBox" AlwaysVisible="true"></PagerStyle>
<MasterTableView AllowFilteringByColumn="false" InsertItemPageIndexAction="ShowItemOnCurrentPage" CommandItemDisplay="Top" ShowHeader="true" Width="100%" DataKeyNames="ID, Location, Description, CurrentStatus, Priority, WorkOrderNumber, ReportDate, ReportedBy" AllowMultiColumnSorting="True">
<CommandItemSettings ShowAddNewRecordButton="false" ShowExportToCsvButton="false" ShowExportToExcelButton="false" ShowExportToPdfButton="false" ShowExportToWordButton="false" ShowRefreshButton="true" />
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" DataType="System.Guid" FilterControlWidth="0px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Location" HeaderText="Location" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Description" HeaderText="Description" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="CurrentStatus" HeaderText="Current Status" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Priority" HeaderText="Priority" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="WorkOrderNumber" HeaderText="Work Order Number" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridDateTimeColumn DataField="ReportDate" HeaderText="Report Date" DataFormatString="{0:d}" DataType="System.DateTime" ShowFilterIcon="false" AutoPostBackOnFilter="true" FilterControlWidth="100px"></telerik:GridDateTimeColumn>
<telerik:GridBoundColumn DataField="ReportedBy" HeaderText="Reported By" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
site.master:
<!DOCTYPE html>
<head runat="server">
<telerik:RadCodeBlock ID="radCodeBlockHead" runat="server">
<title><%=Page.Title%></title>
<!-- // Sitewide Favicon -->
<link rel="shortcut icon" type="image/x-icon" href="<%=Page.ResolveUrl("~")%>favicon.ico" />
<!-- // Main Bootstrap CSS Stylesheet v3.3.4 //-->
<link rel="stylesheet" type="text/css" href="<%=Page.ResolveUrl("~")%>includes/css/bootstrap.min.css" />
<!-- // Bootstrap Theme CSS Stylesheet v3.3.4 //-->
<link rel="stylesheet" type="text/css" href="<%=Page.ResolveUrl("~")%>includes/css/bootstrap-theme.min.css" />
<!-- // Main JQuery v1.11.2 //-->
<script src="<%=Page.ResolveUrl("~")%>includes/js/jquery-1.11.2.min.js"></script>
<!-- // Main Bootstrap JS v3.3.4 //-->
<script src="<%=Page.ResolveUrl("~")%>includes/js/bootstrap.min.js"></script>
<!-- // Placeholder for page specific header content // -->
<asp:ContentPlaceHolder ID="MainHeadContentReplacer" runat="server">
</asp:ContentPlaceHolder>
</telerik:RadCodeBlock>
</head>
<body>
<form id="form" runat="server">
<!-- // General Telerik controls every page needs access to. // -->
<telerik:RadScriptManager runat="server" ID="radScriptManager" />
<div class="container-fluid">
<telerik:RadCodeBlock ID="radCodeBlockNavigation" runat="server">
<!-- // Main Navigation // -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img src="<%=Page.ResolveUrl("~")%>includes/images/back.png" alt="Back" height="48" width="48" style="margin-top:-15px;" /></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#"><%=Page.Title%></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><img src="<%=Page.ResolveUrl("~")%>includes/images/home.png" alt="Back" height="48" width="48" style="margin-top:-15px;" /></a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><div id="username" runat="server">USERNAME</div></a>
<ul class="dropdown-menu" role="menu">
<li class="divider"></li>
<li class="dropdown-header">User Commands</li>
<li><a href="#">Manage Profile</a></li>
<li><a href="#">Logout</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container-fluid -->
</nav>
</telerik:RadCodeBlock>
<asp:ContentPlaceHolder ID="MainBodyContentReplacer" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
当后面的代码到达此特定语句时:
int startIndex = (RadGrid1.CurrentPageIndex * RadGrid1.PageSize);
上面提到的Object引用错误。关键是 RadGrid 为空。为什么当我使用 site.master 时我的 RadGrid 为空,而当我没有站点管理员时工作正常?我真的很想能够使用网站管理员。提前感谢您对我的问题的任何帮助或洞察。
更新:
我正在按照下面的要求添加我的 C# 代码隐藏的更多片段。
这是被触发的事件调用以重新查询 radgrid 的方法。
private void LoadDataForRadGrid1()
{
RadGrid1.DataSource = GetWorkOrderRequests();
RadGrid1.VirtualItemCount = getMaxRowCount();
}
异常发生在 GetWorkOrderRequests() 中。
private DataTable GetWorkOrderRequests()
{
SqlConnection conn = new SqlConnection();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
SqlParameter param;
DataTable dt = new DataTable();
conn = FSCommon.GetFederatedConnection();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_PM_GetWorkOrderRequests";
cmd.Parameters.Clear();
param = new SqlParameter();
param.ParameterName = "@TenantID";
param.SqlDbType = System.Data.SqlDbType.BigInt;
param.Value = FSCommon.TenantID();
cmd.Parameters.Add(param);
param = new SqlParameter();
param.ParameterName = "@Type";
param.SqlDbType = System.Data.SqlDbType.Bit;
param.Value = 0; // 0 is for Vesselside Requests screen. 1 is for Work Order Approval on Shoreside.
cmd.Parameters.Add(param);
int startIndex = (RadGrid1.CurrentPageIndex * RadGrid1.PageSize);
param = new SqlParameter();
param.ParameterName = "@StartIndex";
param.SqlDbType = System.Data.SqlDbType.Int;
param.Value = startIndex;
cmd.Parameters.Add(param);
param = new SqlParameter();
param.ParameterName = "@PageSize";
param.SqlDbType = System.Data.SqlDbType.Int;
param.Value = RadGrid1.PageSize;
cmd.Parameters.Add(param);
adapter.SelectCommand = cmd;
try
{
adapter.Fill(dt);
}
catch (Exception ex)
{
RenderAlert(ex.Message);
}
finally
{
conn.Dispose();
adapter.Dispose();
}
return dt;
}
更新:
添加更多导致异常的事件。
/// <summary>
/// Called everytime the page is loaded and rendered on screen.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Page_PreInit();
}
Page_PostInit();
}
/// <summary>
/// This is called when !IsPostBack on the first page load event.
/// </summary>
private void Page_PreInit()
{
LoadDataForRadGrid1();
RadGrid1.Rebind();
}
根据您的评论,您在 Page_PreInit 中调用了 RadGrid1。
如果在Page_PreInit内部调用,RadGrid1 将始终为空,因为它尚未创建。
最早可以在Page_Init里面打电话。
看看ASP.NET Page Life Cycle Overview。
PreInit - Raised after the start stage is complete and before the
initialization stage begins.
Init - Raised after all controls have been initialized and any skin
settings have been applied. The Init event of individual controls
occurs before the Init event of the page.
解决方案
由于您正在使用 RadGrid,因此您不必显式分配 DataSource。相反,在 RadGrid1_NeedDataSource.
内分配 DataSource
如果需要数据,RadGrid 将自动触发 NeedDataSource 事件。
仅供参考: 您不应在 NeedDataSource 事件中调用 DataBind() 方法。有关 NeedDataSource 的更多信息,请访问 Telerik UI for ASP.NET AJAX Documentation.
我正在尝试将我当前的 Web 项目转换为使用网站管理员,而不是在每个单独的页面上使用我的导航和 headers。
我在使用 site.master 的内容页面上定义了一个 RadGrid。如果我尝试启动该项目,它将在任何尝试使用 "Object Reference not set to an instance of the object."
访问 RadGrid 的 属性 时出错但是,如果我在不使用 site.master 的情况下启动项目,它将正常加载。页面后面的代码差异为零。唯一的区别是 aspx front-end 页面使用了 ContentReplacer.
以下是我正在尝试做的一些小片段:
MainContentReplacer 中的 RadGrid 定义:
<telerik:RadGrid runat="server" ID="RadGrid1" AllowPaging="True" PageSize="25" AllowSorting="true"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound" AutoGenerateColumns="False"
AllowCustomPaging="true" PagerStyle-Mode="Advanced">
<ClientSettings Selecting-AllowRowSelect="True" EnablePostBackOnRowClick="true">
</ClientSettings>
<PagerStyle Mode="Slider" Position="TopAndBottom" PageSizeControlType="RadComboBox" AlwaysVisible="true"></PagerStyle>
<MasterTableView AllowFilteringByColumn="false" InsertItemPageIndexAction="ShowItemOnCurrentPage" CommandItemDisplay="Top" ShowHeader="true" Width="100%" DataKeyNames="ID, Location, Description, CurrentStatus, Priority, WorkOrderNumber, ReportDate, ReportedBy" AllowMultiColumnSorting="True">
<CommandItemSettings ShowAddNewRecordButton="false" ShowExportToCsvButton="false" ShowExportToExcelButton="false" ShowExportToPdfButton="false" ShowExportToWordButton="false" ShowRefreshButton="true" />
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" DataType="System.Guid" FilterControlWidth="0px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Location" HeaderText="Location" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Description" HeaderText="Description" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="CurrentStatus" HeaderText="Current Status" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Priority" HeaderText="Priority" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="WorkOrderNumber" HeaderText="Work Order Number" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
<telerik:GridDateTimeColumn DataField="ReportDate" HeaderText="Report Date" DataFormatString="{0:d}" DataType="System.DateTime" ShowFilterIcon="false" AutoPostBackOnFilter="true" FilterControlWidth="100px"></telerik:GridDateTimeColumn>
<telerik:GridBoundColumn DataField="ReportedBy" HeaderText="Reported By" DataType="System.String" FilterControlWidth="100px"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
site.master:
<!DOCTYPE html>
<head runat="server">
<telerik:RadCodeBlock ID="radCodeBlockHead" runat="server">
<title><%=Page.Title%></title>
<!-- // Sitewide Favicon -->
<link rel="shortcut icon" type="image/x-icon" href="<%=Page.ResolveUrl("~")%>favicon.ico" />
<!-- // Main Bootstrap CSS Stylesheet v3.3.4 //-->
<link rel="stylesheet" type="text/css" href="<%=Page.ResolveUrl("~")%>includes/css/bootstrap.min.css" />
<!-- // Bootstrap Theme CSS Stylesheet v3.3.4 //-->
<link rel="stylesheet" type="text/css" href="<%=Page.ResolveUrl("~")%>includes/css/bootstrap-theme.min.css" />
<!-- // Main JQuery v1.11.2 //-->
<script src="<%=Page.ResolveUrl("~")%>includes/js/jquery-1.11.2.min.js"></script>
<!-- // Main Bootstrap JS v3.3.4 //-->
<script src="<%=Page.ResolveUrl("~")%>includes/js/bootstrap.min.js"></script>
<!-- // Placeholder for page specific header content // -->
<asp:ContentPlaceHolder ID="MainHeadContentReplacer" runat="server">
</asp:ContentPlaceHolder>
</telerik:RadCodeBlock>
</head>
<body>
<form id="form" runat="server">
<!-- // General Telerik controls every page needs access to. // -->
<telerik:RadScriptManager runat="server" ID="radScriptManager" />
<div class="container-fluid">
<telerik:RadCodeBlock ID="radCodeBlockNavigation" runat="server">
<!-- // Main Navigation // -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img src="<%=Page.ResolveUrl("~")%>includes/images/back.png" alt="Back" height="48" width="48" style="margin-top:-15px;" /></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#"><%=Page.Title%></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><img src="<%=Page.ResolveUrl("~")%>includes/images/home.png" alt="Back" height="48" width="48" style="margin-top:-15px;" /></a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><div id="username" runat="server">USERNAME</div></a>
<ul class="dropdown-menu" role="menu">
<li class="divider"></li>
<li class="dropdown-header">User Commands</li>
<li><a href="#">Manage Profile</a></li>
<li><a href="#">Logout</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container-fluid -->
</nav>
</telerik:RadCodeBlock>
<asp:ContentPlaceHolder ID="MainBodyContentReplacer" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
当后面的代码到达此特定语句时:
int startIndex = (RadGrid1.CurrentPageIndex * RadGrid1.PageSize);
上面提到的Object引用错误。关键是 RadGrid 为空。为什么当我使用 site.master 时我的 RadGrid 为空,而当我没有站点管理员时工作正常?我真的很想能够使用网站管理员。提前感谢您对我的问题的任何帮助或洞察。
更新: 我正在按照下面的要求添加我的 C# 代码隐藏的更多片段。
这是被触发的事件调用以重新查询 radgrid 的方法。
private void LoadDataForRadGrid1()
{
RadGrid1.DataSource = GetWorkOrderRequests();
RadGrid1.VirtualItemCount = getMaxRowCount();
}
异常发生在 GetWorkOrderRequests() 中。
private DataTable GetWorkOrderRequests()
{
SqlConnection conn = new SqlConnection();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
SqlParameter param;
DataTable dt = new DataTable();
conn = FSCommon.GetFederatedConnection();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_PM_GetWorkOrderRequests";
cmd.Parameters.Clear();
param = new SqlParameter();
param.ParameterName = "@TenantID";
param.SqlDbType = System.Data.SqlDbType.BigInt;
param.Value = FSCommon.TenantID();
cmd.Parameters.Add(param);
param = new SqlParameter();
param.ParameterName = "@Type";
param.SqlDbType = System.Data.SqlDbType.Bit;
param.Value = 0; // 0 is for Vesselside Requests screen. 1 is for Work Order Approval on Shoreside.
cmd.Parameters.Add(param);
int startIndex = (RadGrid1.CurrentPageIndex * RadGrid1.PageSize);
param = new SqlParameter();
param.ParameterName = "@StartIndex";
param.SqlDbType = System.Data.SqlDbType.Int;
param.Value = startIndex;
cmd.Parameters.Add(param);
param = new SqlParameter();
param.ParameterName = "@PageSize";
param.SqlDbType = System.Data.SqlDbType.Int;
param.Value = RadGrid1.PageSize;
cmd.Parameters.Add(param);
adapter.SelectCommand = cmd;
try
{
adapter.Fill(dt);
}
catch (Exception ex)
{
RenderAlert(ex.Message);
}
finally
{
conn.Dispose();
adapter.Dispose();
}
return dt;
}
更新:
添加更多导致异常的事件。
/// <summary>
/// Called everytime the page is loaded and rendered on screen.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Page_PreInit();
}
Page_PostInit();
}
/// <summary>
/// This is called when !IsPostBack on the first page load event.
/// </summary>
private void Page_PreInit()
{
LoadDataForRadGrid1();
RadGrid1.Rebind();
}
根据您的评论,您在 Page_PreInit 中调用了 RadGrid1。
如果在Page_PreInit内部调用,RadGrid1 将始终为空,因为它尚未创建。
最早可以在Page_Init里面打电话。
看看ASP.NET Page Life Cycle Overview。
PreInit - Raised after the start stage is complete and before the initialization stage begins.
Init - Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page.
解决方案
由于您正在使用 RadGrid,因此您不必显式分配 DataSource。相反,在 RadGrid1_NeedDataSource.
内分配 DataSource如果需要数据,RadGrid 将自动触发 NeedDataSource 事件。
仅供参考: 您不应在 NeedDataSource 事件中调用 DataBind() 方法。有关 NeedDataSource 的更多信息,请访问 Telerik UI for ASP.NET AJAX Documentation.