求教一个关于自定义控件的属性在VS2005的设计器中有时候报错的问题
时间:2010-11-25 来源:蓝海
  namespace Toiletry.UI
  {
      /// <summary>
      /// 繼承Repeater控件,添加分頁功能
      /// </summary>
      [ToolboxData("<{0}:DataRepeater runat='server' RecordCount=\"100\"><HeaderTemplate><table><tr><td>Header1</td><td>Header2</td></tr></HeaderTemplate><ItemTemplate><tr><td>Data1</td><td>Data2</td></tr></ItemTemplate><FooterTemplate></table></FooterTemplate> </{0}:DataRepeater>")]
      public class DataRepeater : System.Web.UI.WebControls.Repeater
      {
          [Category("分頁")]
          [DefaultValue(0)]
          public int RecordCount
          {
              get
              {
                  object o = ViewState["RecordCount"];
                  return o == null ? 0 : Convert.ToInt32(o);
              }
              set
              {
                  ViewState["RecordCount"] = value;
              }
          }
  
          [Category("分頁")]
          [DefaultValue(0)]
          public virtual int PageIndex
          {
              get
              {
                  object o = ViewState["PageIndex"];
                  return o == null ? 0 : Convert.ToInt32(o);
              }
              set
              {
                  ViewState["PageIndex"] = value;
              }
          }
          [Category("分頁")]
          [DefaultValue(10)]
          public virtual int PageSize
          {
              get
              {
                  object o = ViewState["PageSize"];
                  return o == null ? 10 : Convert.ToInt32(o);
              }
              set
              {
                  ViewState["PageSize"] = value;
              }
          }
  
          [Category("分頁")]
          [DefaultValue(true)]
          public bool EnablePaging
          {
              get
              {
                  object o = ViewState["EnablePaging"];
                  return o == null ? true : Convert.ToBoolean(o);
              }
              set
              {
                  ViewState["EnablePaging"] = value;
              }
          }
  
          public override void RenderControl(HtmlTextWriter writer)
          {
              Table PageTable = new Table();
              PageTable.RenderBeginTag(writer);
#region "Data Row"
              TableRow DataRow = new TableRow();
              DataRow.RenderBeginTag(writer);
              //Data Cell
              TableCell DataCell = new TableCell();
              DataCell.RenderBeginTag(writer);
              //Repeater控件的內容
              base.RenderControl(writer);
DataCell.RenderEndTag(writer);
DataRow.RenderEndTag(writer);
#endregion
              //允許分頁時,才呈現分頁控件
              if (EnablePaging == true)
              {
                  #region "Page Row"
                  TableRow PageRow = new TableRow();
                  PageRow.RenderBeginTag(writer);
                  //Page Cell
                  PageCell PCell = new PageCell(RecordCount, PageIndex, PageSize);
                  PageRow.Cells.Add(PCell);
PageRow.RenderControl(writer);
PageRow.RenderEndTag(writer);
                  #endregion
              }
              PageTable.RenderEndTag(writer);     
          }
      }
  
      public class PageCell : System.Web.UI.WebControls.TableCell
      {
          /// <summary>
          /// 總記錄數
          /// </summary>
          private int _RecordCount;
          /// <summary>
          /// 當前頁碼[索引從0開始,即0=第1頁]
          /// </summary>
          private int _PageIndex;
          /// <summary>
          /// 每頁顯示的記錄筆數
          /// </summary>
          private int _PageSize;
          /// <summary>
          /// 總共有多少頁
          /// </summary>
          private int _PageCount;
          /// <summary>
          /// 在分頁控件上,顯示多少個[頁碼鏈接]
          /// </summary>
          private int _PageLinkCount = 8;
  
          /// <summary>
          /// 構造函數
          /// </summary>
          /// <param name="RecordCount">總記錄數</param>
          /// <param name="PageIndex">當前頁碼[索引從0開始,即0=第1頁]</param>
          /// <param name="PageSize">每頁顯示的記錄筆數</param>
          public PageCell(int RecordCount, int PageIndex, int PageSize)
          {
              _RecordCount = RecordCount;
              _PageIndex = PageIndex;
              _PageSize = PageSize;
              //_PageSize=0的異常
              if (_PageSize == 0)
              {
                  _PageSize = 10;
              }
              //求一共有幾頁
              if (_RecordCount % _PageSize == 0)
              {
                  _PageCount = _RecordCount / _PageSize;
              }
              else
              {
                  _PageCount = _RecordCount / _PageSize + 1;
              }
              //產生分頁控件
              InitPageControl();
          }
  
          /// <summary>
          /// 產生分頁控件
          /// </summary>
          public virtual void InitPageControl()
          {
              //當前是第幾頁
              int CurrentPageIndex = _PageIndex + 1;
              //頁碼排列的中間值
              int MiddleValue = 0;
              //頁碼排列中間左邊有幾個
              int LeftCount = 0;
              //頁碼排列中間右邊有幾個
              int RightCount = 0;
              if (_PageLinkCount % 2 == 0)
              {
                  MiddleValue = _PageLinkCount / 2 + 1;
                  LeftCount = _PageLinkCount / 2;
                  RightCount = _PageLinkCount / 2 - 1;
              }
              else
              {
                  MiddleValue = (_PageLinkCount - 1) / 2 + 1;
                  LeftCount = (_PageLinkCount - 1) / 2;
                  RightCount = (_PageLinkCount - 1) / 2;
              }
              //當前頁碼<=中間值
              if (CurrentPageIndex <= MiddleValue)
              {
                  if (_PageCount >= _PageLinkCount)
                  {
                      for (int i = 1; i <= _PageLinkCount; i++)
                      {
                          LinkButton PageLink = new LinkButton();
                          PageLink.Text = i.ToString();
                          //分隔符
                          Literal PageBlank = new Literal();
                          PageBlank.Text = " ";
                          //add to control
                          this.Controls.Add(PageLink);
                          this.Controls.Add(PageBlank);
                      }
                  }
                  else
                  {
                      for (int i = 1; i <= _PageCount; i++)
                      {
                          LinkButton PageLink = new LinkButton();
                          PageLink.Text = i.ToString();
                          //分隔符
                          Literal PageBlank = new Literal();
                          PageBlank.Text = " ";
                          //add to control
                          this.Controls.Add(PageLink);
                          this.Controls.Add(PageBlank);
                      }
                  }
              }
              else
              {
                  //當前頁碼>中間值
//處理原則,是把當前頁作為頁碼排列的中間值處理
                  //左部分
                  for (int i = 1; i <= LeftCount;i++)
                  {
                      LinkButton PageLink = new LinkButton();
                      PageLink.Text = (CurrentPageIndex - MiddleValue + 1).ToString();
                      //分隔符
                      Literal PageBlank = new Literal();
                      PageBlank.Text = " ";
                      //add to control
                      this.Controls.Add(PageLink);
                      this.Controls.Add(PageBlank);
                  }
                  //中間值
                  LinkButton MidPageLink = new LinkButton();
                  MidPageLink.Text = CurrentPageIndex.ToString();
                  //分隔符
                  Literal MidPageBlank = new Literal();
                  MidPageBlank.Text = " ";
                  //add to control
                  this.Controls.Add(MidPageLink);
                  this.Controls.Add(MidPageBlank);
                  //右部分
                  if (_PageCount - CurrentPageIndex >= RightCount)
                  {
                      for (int i = 1; i <= RightCount; i++)
                      {
                          LinkButton PageLink = new LinkButton();
                          PageLink.Text = (CurrentPageIndex + i).ToString();
                          //分隔符
                          Literal PageBlank = new Literal();
                          PageBlank.Text = " ";
                          //add to control
                          this.Controls.Add(PageLink);
                          this.Controls.Add(PageBlank);
                      }
                  }
                  else
                  {
                      for (int i = 1; i <= _PageCount - CurrentPageIndex; i++)
                      {
                          LinkButton PageLink = new LinkButton();
                          PageLink.Text = (CurrentPageIndex + i).ToString();
                          //分隔符
                          Literal PageBlank = new Literal();
                          PageBlank.Text = " ";
                          //add to control
                          this.Controls.Add(PageLink);
                          this.Controls.Add(PageBlank);
                      }
                  }               
              }
          }
}
  }
  
  WEB頁面的
  aspx代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataRepeaterDemo.aspx.cs" Inherits="DataRepeaterDemo" %>
<%@ Register Assembly="Toiletry.UI" Namespace="Toiletry.UI" TagPrefix="cc1" %>
  
  <html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
      <title>未命名頁面</title>
  </head>
  <body>
      <form id="form1" runat="server">
      <div>
          <cc1:DataRepeater ID="rpList" runat="server" RecordCount="1001" EnablePaging="true">
         
             <HeaderTemplate>
              <table class="tby">
              <tr>
                   <th>Age</th>
                   <th>Name</th>
                </tr>
              </HeaderTemplate>
              <ItemTemplate>
              <tr>
               <td><%#Eval("Age")%></td>
               <td><%#Eval("Name")%></td>
               </tr>
              </ItemTemplate>
              <FooterTemplate>
              </table>
              </FooterTemplate>           
         
          </cc1:DataRepeater>
          <cc1:DataRepeater ID="DataRepeater1" runat="server" RecordCount="100">
                     <HeaderTemplate>
              <table class="tby">
              <tr>
                   <th>Age</th>
                   <th>Name</th>
                </tr>
              </HeaderTemplate>
              <ItemTemplate>
              <tr>
               <td><%#Eval("Age")%></td>
               <td><%#Eval("Name")%></td>
               </tr>
              </ItemTemplate>
              <FooterTemplate>
              </table>
              </FooterTemplate> 
          </cc1:DataRepeater>
          <cc1:DataRepeater ID="DataRepeater2" runat="server" RecordCount="100" EnablePaging="false">
              <HeaderTemplate>
                  <table>
                  </table>
                      <tr>
                          <td>
                              Header1</td>
                          <td>
                              Header2</td>
                      </tr>
              </HeaderTemplate>
              <ItemTemplate>
                  <tr>
                      <td>
                          Item1</td>
                      <td>
                          Item2</td>
                  </tr>
              </ItemTemplate>
              <FooterTemplate>
                  </table></FooterTemplate>
          </cc1:DataRepeater>
     
      </div>
      </form>
  </body>
  </html>
  
  
   










