我创建了一个简单的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;
}
}
任何人都请告诉我为什么它为空,我该怎么做才能解决它?
答案 0 :(得分:1)
完全从Register指令中删除命名空间和程序集属性,因为这些属性仅用于服务器控件。要注册用户控件,必须指定放置ascx文件的Src,TagPrefix和TagName。 How to: Include a User Control in an ASP.NET Web Page