ASP.NET c# Telerik RadGrid在使用site master时为空
本文关键字:master site NET Telerik RadGrid ASP | 更新日期: 2023-09-27 17:50:05
我正试图将我当前的web项目转换为利用网站主,而不是在每个单独的页面上都有我的导航和标题。
我在使用site.master的内容页上定义了一个RadGrid。如果我尝试启动项目,它将在任何试图访问RadGrid的属性时出错,"对象引用未设置为对象的实例"。
但是,如果我在不使用站点的情况下启动项目。师傅,装得很好。页面后面的代码没有任何差异。唯一的区别是aspx前端页面使用了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);
出现上述对象引用错误。关键是RadGrid是空的。为什么是我的RadGrid空,当我使用的网站。master和工作很好,当我没有站点master?我真的很希望能够使用网站管理员。提前感谢任何帮助或洞察我的问题。
更新:我正在按照下面的要求添加更多的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。
如果你调用inside Page_PreInit,RadGrid1将永远是null,因为它还没有被创建。
最早可以在Page_Init内部调用。
看看ASP。. NET页面生命周期概述。
解决方案PreInit -在启动阶段结束后,在启动阶段结束之前引发初始化阶段开始。
Init -在所有控件和任何皮肤初始化后触发已应用设置。单个控件的Init事件在该页的Init事件之前发生。
由于您正在使用RadGrid,您不必显式地分配数据源。相反,在RadGrid1_NeedDataSource中分配DataSource。
如果需要Data, RadGrid会自动触发NeedDataSource事件。
供参考:你不应该在NeedDataSource事件中调用DataBind()方法。更多关于NeedDataSource的信息,见Telerik UI for ASP。. NET AJAX文档