首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

生成控件异常.请帮忙看看

2012-01-09 
生成控件错误.请帮忙看看//ToolBar.csusingSystemusingSystem.CollectionsusingSystem.Collections.Gene

生成控件错误.请帮忙看看
//ToolBar.cs
using   System;
using   System.Collections;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Text;
using   System.Web;
using   System.Web.UI;
using   System.Web.UI.WebControls;
namespace   Lib.UI
{
        [ToolboxData( " <{0}:ToolBar   runat=server> </{0}:ToolBar> ")]
        [ParseChildren(true, "ButtonCollection ")]
        public   class   ToolBar   :   WebControl
        {
                private List <ToolBarButton>  buttonCollection;

                [
            Category( "集合 "),
            Description( "项集合 "),
            DesignerSerializationVisibility(
                    DesignerSerializationVisibility.Content),
            PersistenceMode(PersistenceMode.InnerDefaultProperty)

            ]

                public   List <ToolBarButton>   ButtonCollection
                {
                        get
                        {
                                if   (buttonCollection   ==   null)
                                        buttonCollection   =   new   List <ToolBarButton> ();
                                return   buttonCollection;
                        }
                        set
                        {
                                buttonCollection   =   value;
                        }
                       
                }
                protected   override   void   CreateChildControls()
                {
                        this.EnsureChildControls();
                        base.CreateChildControls();
                }

                protected   override   void   Render(HtmlTextWriter   writer)
                {
                       


                        writer.AddStyleAttribute(HtmlTextWriterStyle.Width,   "100% ");
                        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor,   "#ffccee ");
                        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle,   BorderStyle.Groove.ToString());
                        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px ");
                        writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign,   TextAlign.Right.ToString());
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        foreach   (ToolBarButton   bt   in   buttonCollection)
                        {
                                bt.RenderControl(writer);
                        }
                        writer.RenderEndTag();
                }
             
          }
}
//ToolBarButton.cs
using   System;
using   System.ComponentModel;
using   System.Web.UI;
namespace   Lib.UI
{
        ///   <summary>
        ///   ToolBar按钮
        ///   </summary>
        ///  
        public   class   ToolBarButton:Control,IPostBackEventHandler

        {
                private   string   _text;
                private   string   _commandname;
                private   string   _clientscript;

                //   声明Click事件委托
                private   static   readonly   object   ClickKey   =   new   object();

               

                [Description( "显示文本 ")]
                [NotifyParentProperty(true)]
                [Category( "按钮集合 ")]
                public   string   Text
                {
                        get   {   return   _text;   }
                        set   {   _text   =   value;   }
                }



                [Description( "按钮名称 ")]
                [NotifyParentProperty(true)]
                [Category( "按钮集合 ")]
                public   string   CommandName
                {
                        get   {   return   _commandname;   }
                        set   {   _commandname   =   value;   }
                }

                [Description( "客户端脚本对象 ")]
                [NotifyParentProperty(true)]
                [Category( "按钮集合 ")]
                public   string   ClientScript
                {
                        get   {   return   _clientscript;   }
                        set   {   _clientscript   =   value;   }
                }
                public   event   EventHandler   Click
                {
                        add
                        {
                                Events.AddHandler(ClickKey,   value);
                        }
                        remove
                        {
                                Events.RemoveHandler(ClickKey,   value);
                        }
                }

                //   定义OnClick事件处理程序
                protected   virtual   void   OnClick(EventArgs   e)
                {
                        EventHandler   clickEventDelegate   =
                              (EventHandler)Events[ClickKey];
                        if   (clickEventDelegate   !=   null)
                        {
                                clickEventDelegate(this,   e);


                        }
                }

                //   实现RaisePostBackEvent方法,处理回发事件
                public   void   RaisePostBackEvent(string   eventArgument)
                {
                        OnClick(EventArgs.Empty);
                }

                protected   override   void   Render(HtmlTextWriter   writer)
                {
                        writer.AddStyleAttribute( "CommandName ",   this._commandname);
                        writer.AddAttribute( "onclick ",   Page.ClientScript.GetPostBackClientHyperlink(this,   this._commandname));
//这里Page对象是null的,为何这样?
                        writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor,   "hand ");
                        writer.AddAttribute(HtmlTextWriterAttribute.Title,   this._text);
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        writer.Write(this._text);
                        writer.RenderEndTag();
                }
             
        }
}


[解决办法]
当前控件并没有添加到页面中,当成了普通的类来运行。
[解决办法]
可以改成: Page p = (Page)System.Web.HttpContext.Current.Handler; writer.AddAttribute( "onclick ", p.ClientScript.GetPostBackClientHyperlink(this, this._commandname));

热点排行