Building ASP.NET Server Controls-Copy of Pager.cs

// Title: Building ASP.NET Server Controls
//
// Chapter: 5 - Event-based Programming
// File: Pager.cs
// Written by: Dale Michalk and Rob Cameron
//
// Copyright ?2003, Apress L.P.
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using ControlsBookLib.Ch12.Design;

namespace ControlsBookLib.Ch05
{
   [ToolboxData("<{0}:Pager runat=server></{0}:Pager>"),Designer(typeof(CompCntrlDesigner))]
   public class Pager : Control, INamingContainer
   {
      private static readonly object PageCommandKey = new object();
      public event PageCommandEventHandler PageCommand
      {
         add
         {
            Events.AddHandler(PageCommandKey, value);
         }
         remove
         {
            Events.RemoveHandler(PageCommandKey, value);
         }
      }

      protected virtual void OnPageCommand(PageCommandEventArgs pce)
      {
         PageCommandEventHandler pageCommandEventDelegate =
            (PageCommandEventHandler) Events[PageCommandKey];
         if (pageCommandEventDelegate != null)
         {
            pageCommandEventDelegate(this, pce);
         }
      }

      protected override bool OnBubbleEvent(object source, EventArgs e)
      {
         bool result = false;
         CommandEventArgs ce = e as CommandEventArgs;

         if (ce != null)
         {
            if (ce.CommandName.Equals("Page"))
            {
               PageDirection direction;
               if (ce.CommandArgument.Equals("Right"))
                  direction = PageDirection.Right;
               else
                  direction = PageDirection.Left;

               PageCommandEventArgs pce =
                  new PageCommandEventArgs(direction);

               OnPageCommand(pce);
               result = true;
            }
         }
         return result;
      }

      public ButtonDisplay Display
      {
         get
         {
            EnsureChildControls();
            return buttonLeft.Display ;
         }
         set
         {
            EnsureChildControls();
            buttonLeft.Display  = value;
            buttonRight.Display = value;
         }
      }

      protected override void CreateChildControls()
      {
         Controls.Clear();
         CreateChildControlHierarchy();
      }

      public override ControlCollection Controls
      {
         get
         {
            EnsureChildControls();
            return base.Controls;
         }
      }
      
      private SuperButton buttonLeft ;
      private SuperButton buttonRight;
      private void CreateChildControlHierarchy()
      {
         LiteralControl tableStart = new
            LiteralControl("<table border=1><tr><td>");
         Controls.Add(tableStart);

          buttonLeft = new SuperButton();
         buttonLeft.ID = "buttonLeft";
         if (Context != null)
         {
            buttonLeft.Text = Context.Server.HtmlEncode("<") + " Left";
         }
         else
         {
            buttonLeft.Text = "< Left";
         }
         buttonLeft.CommandName = "Page";
         buttonLeft.CommandArgument = "Left";
         Controls.Add(buttonLeft);

         LiteralControl spacer = new LiteralControl("&nbsp;&nbsp;");
         Controls.Add(spacer);

         buttonRight = new SuperButton();
         buttonRight.ID = "buttonRight";
         buttonRight.Display = Display;
         if (Context != null)
         {
            buttonRight.Text = "Right " + Context.Server.HtmlEncode(">");
         }
         else
         {
            buttonRight.Text = "Right  >";
         }
         buttonRight.CommandName = "Page";
         buttonRight.CommandArgument = "Right";
         Controls.Add(buttonRight);

         LiteralControl tableEnd = new
            LiteralControl("</td></tr></table>");
         Controls.Add(tableEnd);
      }
   }
}

转载于:https://www.cnblogs.com/shihao/archive/2012/05/13/2498654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值