用户控件引用父页面控件问题
环境:VS2005
用户控件代码:
public partial class UserControls_GridPagination : System.Web.UI.UserControl
{
private GridView _grid;
private string _gridid;
/// <summary>
/// 分页显示的GridView控件ID
/// </summary>
public string GridViewID
{
get { return _gridid; }
set { _gridid = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
//_grid = (GridView)this.Page.FindControl(_gridid);
_grid = (GridView)((System.Web.UI.Page)System.Web.HttpContext.Current.Handler).FindControl(_gridid);
lblPage.Text = _grid.PageIndex.ToString() + "/ " + _grid.PageCount.ToString();
lblPerPage.Text = _grid.PageSize.ToString();
lblAll.Text = _grid.Rows.Count.ToString();
}
}
想要在这个控件中引用父页面的GridView控件
_grid = (GridView)this.Page.FindControl(_gridid);和
_grid = (GridView)((System.Web.UI.Page)System.Web.HttpContext.Current.Handler).FindControl(_gridid);都能编译通过
但到了
lblPage.Text = _grid.PageIndex.ToString() + "/ " + _grid.PageCount.ToString();时报错:未将对象引用到实例
好像没有引用成功?不知各位有什么解决办法?请不吝赐教,谢谢!
------解决方案--------------------你不能这样去用!
你要用Page.MasterPage.gridview来引用MasterPage的控件,当然你必须让这个Gridview对子页面可见!
------解决方案--------------------Page.Master.GridView
------解决方案--------------------看不太懂,帮你顶了