我已经创建了一个简单的web用户控件,并且我已经在页面上注册了它以使用它。
我设置了一些必需的属性,然后调用控件的公共方法
其中我使用了一些按钮,我已经放在我的ascx文件。
当我使用按钮(我的用户控件只有12个按钮)时,我得到空引用异常。
下面是我的用户控件的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MangoCustomControls
{
public partial class MangoPagerControl : System.Web.UI.UserControl
{
// variables
private int _totalItemCount;
private int _currentPageIndex;
private int _itemsPerPage;
private int _noOfPages;
private const int MAX_PAGE_SIZE = 10;
// event
public event EventHandler<PagerControlEventArgs> OnPagerItemClicked;
// methods
public void BindPager()
{
int remainder = _totalItemCount % _itemsPerPage;
int result = _totalItemCount / _itemsPerPage;
if (remainder > 0)
{
result++;
}
_noOfPages = result;
ShowHideButtonsAndPutTextsOnThem();
}
private void ShowHideButtonsAndPutTextsOnThem()
{
if (_noOfPages < MAX_PAGE_SIZE)
{
btnPrev.Visible = false;
btnNext.Visible = false;
for (int i = 1; i <= MAX_PAGE_SIZE; i++)
{
Control ctrl = FindControl(string.Format("btn{0}", i));
if (i <= _noOfPages)
{
ctrl.Visible = true;
((Button)ctrl).Text = string.Format("{0}", i);
}
else
{
ctrl.Visible = false;
}
}
}
else
{
btnPrev.Visible = false;
btnNext.Visible = true;
for (int i = 1; i <= MAX_PAGE_SIZE; i++)
{
Control ctrl = FindControl(string.Format("btn{0}", i));
ctrl.Visible = true;
((Button)ctrl).Text = string.Format("{0}", i);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void OnButtonClick(object sender, EventArgs e)
{
Button btn = sender as Button;
int pageNumber = int.TryParse(btn.Text, out pageNumber) ? pageNumber : -1;
if (OnPagerItemClicked != null)
{
OnPagerItemClicked(this, new PagerControlEventArgs(pageNumber));
}
}
// properties
public int TotalItemCount
{
get { return _totalItemCount; }
set { _totalItemCount = value; }
}
public int ItemsPerPage
{
get { return _itemsPerPage; }
set { _itemsPerPage = value; }
}
public int CurrentPageIndex
{
get { return _currentPageIndex; }
set { _currentPageIndex = value; }
}
}
public class PagerControlEventArgs : EventArgs
{
public int SelectedPageNumber { get; private set; }
public PagerControlEventArgs(int selectedPageNumber)
{
SelectedPageNumber = selectedPageNumber;
}
}
}设计器文件如下所示:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MangoPagerControl.ascx.cs"
Inherits="MangoCustomControls.MangoPagerControl" %>
<div>
<table>
<tr>
<td>
<asp:Button ID="btnPrev" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn1" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn2" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn3" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn4" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn5" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn6" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn7" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn8" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn9" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btn10" runat="server" Text="" OnClick="OnButtonClick" />
</td>
<td>
<asp:Button ID="btnNext" runat="server" Text="" OnClick="OnButtonClick" />
</td>
</tr>
</table>
</div>以及(在页面上)调用控件方法的代码:
protected void Button2_Click(object sender, EventArgs e)
{
MangoPager.TotalItemCount = 7;
MangoPager.ItemsPerPage = 2;
MangoPager.BindPager();
}我在网页上注册用户控件,如下所示:
<%@ Register TagPrefix="CustomMango" Namespace="MangoCustomControls" Assembly= "MangoCustomControls" %>并像这样创建MangoPager控件:
<CustomMango:MangoPagerControl ID="MangoPager" runat="server"
ononpageritemclicked="MangoPagerControl1_OnPagerItemClicked" />在BindPager()方法中,我得到了btnPrev的空引用异常(所有按钮都是空的)。
deigner文件如下所示:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace MangoCustomControls {
public partial class MangoPagerControl {
/// <summary>
/// btnPrev control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnPrev;
/// <summary>
/// btn1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn1;
/// <summary>
/// btn2 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn2;
/// <summary>
/// btn3 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn3;
/// <summary>
/// btn4 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn4;
/// <summary>
/// btn5 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn5;
/// <summary>
/// btn6 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn6;
/// <summary>
/// btn7 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn7;
/// <summary>
/// btn8 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn8;
/// <summary>
/// btn9 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn9;
/// <summary>
/// btn10 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btn10;
/// <summary>
/// btnNext control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnNext;
}
}请告诉我为什么它是空的,我应该怎么做来解决它?
发布于 2011-08-02 05:11:50
从Register指令中完全删除命名空间和程序集属性,因为这些属性仅用于服务器控件。要注册用户控件,您必须指定放置ascx文件的Src、TagPrefix和TagName。How to: Include a User Control in an ASP.NET Web Page
https://stackoverflow.com/questions/6903843
复制相似问题