首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >此web用户控件代码有什么问题?

此web用户控件代码有什么问题?
EN

Stack Overflow用户
提问于 2011-08-02 04:08:52
回答 1查看 588关注 0票数 0

我已经创建了一个简单的web用户控件,并且我已经在页面上注册了它以使用它。

我设置了一些必需的属性,然后调用控件的公共方法

其中我使用了一些按钮,我已经放在我的ascx文件。

当我使用按钮(我的用户控件只有12个按钮)时,我得到空引用异常。

下面是我的用户控件的代码:

代码语言:javascript
复制
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;
        }
    }
}

设计器文件如下所示:

代码语言:javascript
复制
<%@ 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>

以及(在页面上)调用控件方法的代码:

代码语言:javascript
复制
 protected void Button2_Click(object sender, EventArgs e)
        {           

                MangoPager.TotalItemCount = 7;
                MangoPager.ItemsPerPage = 2;
                MangoPager.BindPager();

        }

我在网页上注册用户控件,如下所示:

代码语言:javascript
复制
<%@ Register TagPrefix="CustomMango" Namespace="MangoCustomControls" Assembly= "MangoCustomControls" %>

并像这样创建MangoPager控件:

代码语言:javascript
复制
<CustomMango:MangoPagerControl  ID="MangoPager" runat="server" 
        ononpageritemclicked="MangoPagerControl1_OnPagerItemClicked" />

BindPager()方法中,我得到了btnPrev的空引用异常(所有按钮都是空的)。

deigner文件如下所示:

代码语言:javascript
复制
//------------------------------------------------------------------------------
// <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;
    }
}

请告诉我为什么它是空的,我应该怎么做来解决它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-02 05:11:50

从Register指令中完全删除命名空间和程序集属性,因为这些属性仅用于服务器控件。要注册用户控件,您必须指定放置ascx文件的Src、TagPrefix和TagName。How to: Include a User Control in an ASP.NET Web Page

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6903843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档