在custum子控件上没有触发RaisePostBackEvent

时间:2016-08-04 09:44:10

标签: c# events controls

我已经创建了两个自定义控件来在我的C#代码中使用bootstrap。第一个使用此代码实现引导下拉列表;

using System.Web.UI;
using System.Web.UI.WebControls;
using static JDBootStrap.BSTypes;

namespace JDBootStrap.Components
{
    [ToolboxData("<{0}:BSDropDown runat=server></{0}:BSDropDown>")]
    public class BSDropDown : Panel
    {
        public enum OrientationType { Down, Up, Right }

        string _text;
    OrientationType _orientation;
    ControlCollection _controls;
    sizeType _size = sizeType.md;
    bool _useInGroup;
    bool useInList;
    Glyphicon.GlyphiconType? _glyphicon;
    string _style;

    public BSDropDown()
    {
        Controls = new ControlCollection(this);
    }

    public Glyphicon.GlyphiconType? Glyphicon
    {
        get
        {
            return _glyphicon;
        }

        set
        {
            _glyphicon = value;
        }
    }

    #region Properties

    protected override string TagName
    {
        get
        {
            return UseInList ? "li" : "div";
        }
    }

    protected override ControlCollection CreateControlCollection()
    {
        return Controls;
    }

    public string Text
    {
        get
        {
            return _text;
        }

        set
        {
            _text = value;
        }
    }

    public OrientationType Orientation
    {
        get
        {
            return _orientation;
        }

        set
        {
            _orientation = value;
        }
    }

    public bool UseInList
    {
        get
        {
            return useInList;
        }

        set
        {
            useInList = value;
        }
    }

    new public ControlCollection Controls
    {
        get
        {
            return _controls;
        }

        set
        {
            _controls = value;
        }
    }

    public sizeType Size
    {
        get
        {
            return _size;
        }

        set
        {
            _size = value;
        }
    }

    public bool UseInGroup
    {
        get
        {
            return _useInGroup;
        }

        set
        {
            _useInGroup = value;
        }
    }

    #endregion

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.Write("<" + TagName + " class=\"" + GetOrientationClass());
        if (_orientation == OrientationType.Right) writer.Write(" dropdown-submenu");
        if (!string.IsNullOrEmpty(CssClass)) writer.Write(" " + CssClass);

        if (_useInGroup) writer.Write(" btn-group");
        writer.Write(" btn-" + (UseInGroup ? "group-" : "") + _size.ToString().ToLower());
        writer.Write("\"");
        writer.Write(" name =\"" + UniqueID + "\"");
        writer.Write(" id=\"" + ClientID + "\"");
        writer.Write(">");

        writer.Write(UseInList ? "<a href =\"#\"" : "<button");
        writer.Write(" class=\"");
        writer.Write(_useInGroup ? "btn btn-default " : "");
        writer.Write("dropdown-toggle");
        writer.Write("\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">");
        if (!string.IsNullOrEmpty(_text)) writer.WriteEncodedText(_text);
        if (_glyphicon != null)
        {
            var g = new Glyphicon((Glyphicon.GlyphiconType)_glyphicon);
            g.RenderControl(writer);
        }
        if (_orientation != OrientationType.Right) writer.Write("<span class=\"caret\" style=\"margin-left:4px;\"></span>");
        writer.Write(UseInList ? "</a>" : "</button>");

        writer.Write("<ul class=\"dropdown-menu\">");
    }

    protected override void Render(HtmlTextWriter writer)
    {
        RenderBeginTag(writer);
        RenderChildren(writer);
        RenderEndTag(writer);
    }
    protected override void RenderChildren(HtmlTextWriter writer)
    {
        foreach (Control ctrl in Controls)
        {
            if (ctrl.GetType() == typeof(BSDropDown)) ((BSDropDown)ctrl).Orientation = OrientationType.Right;
            if (ctrl.GetType() == typeof(BSMenuItem)) ((BSMenuItem)ctrl).SubMenu = true;
            ctrl.RenderControl(writer);
        }
    }

    public override void RenderEndTag(HtmlTextWriter writer)
    {
        writer.Write("</ul>");
        writer.Write("</" + TagName + ">");
    }

    private string GetOrientationClass()
    {
        switch (Orientation)
        {
            case OrientationType.Up:
                return "dropup";
            default:
                return "dropdown";
        }
    }
}

}

第二个是这样的菜单:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JDBootStrap.Components
{
    [ToolboxData("<{0}:BSMenuSeparator runat=server></{0}:BSMenuSeparator>")]
    public class BSMenuSeparator : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<li class=\"divider\"></li>");
        }
    }
    [ToolboxData("<{0}:BSMenuHeader runat=server></{0}:BSMenuHeader>")]
    public class BSMenuHeader : WebControl
    {
        private string _title;

        public BSMenuHeader(string title)
        {
            _title = title;
        }

        public string Title
        {
            get
            {
                return _title;
            }

            set
            {
                _title = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<li class=\"dropdown-header\">");
            if (!string.IsNullOrEmpty(_title)) writer.Write(_title);
            writer.Write("</li>");
        }
    }
    [ToolboxData("<{0}:BSMenuItem runat=server></{0}:BSMenuItem>")]
    public class BSMenuItem : Panel, IButtonControl, IPostBackEventHandler
    {

        string _text;
        string _navigateUrl;
        bool _active ;
            b

ool _subItem;
        Glyphicon.GlyphiconType? _glyphicon;
        string _value;
        string _title;
        string _commandArgument;
        string _validationGroup;
        string _postBackUrl;
        string _commandName;
        bool _causesValidation;
        static readonly object EventClick = new object();

        public event EventHandler Click
        {
            add
            {
                Events.AddHandler(EventClick, value);
            }
            remove
            {
                Events.RemoveHandler(EventClick, value);
            }
        }

        public event CommandEventHandler Command;

        #region Property
        public BSMenuItem()
        {
        }

        public BSMenuItem(string text)
        {
            _text = text;
        }

        public BSMenuItem(string text, string navigateUrl)
        {
            _text = text;
            _navigateUrl = navigateUrl;
        }

        public string Text
        {
            get
            {
                return _text;
            }

            set
            {
                _text = value;
            }
        }

        public string NavigateUrl
        {
            get
            {
                return _navigateUrl;
            }

            set
            {
                _navigateUrl = value;
            }
        }

        public bool Active
        {
            get
            {
                return _active;
            }

            set
            {
                _active = value;
            }
        }

        public bool SubMenu
        {
            get
            {
                return _subItem;
            }

            set
            {
                _subItem = value;
            }
        }

        public Glyphicon.GlyphiconType? Glyphicon
        {
            get
            {
                return _glyphicon;
            }

            set
            {
                _glyphicon = value;
            }
        }

        public bool CausesValidation
        {
            get
            {
                return _causesValidation;
            }

            set
            {
                _causesValidation = value;
            }
        }

        public string CommandArgument
        {
            get
            {
                return _commandArgument;
            }

            set
            {
                _commandArgument = value;
            }
        }

        public string ValidationGroup
        {
            get
            {
                return _validationGroup;
            }

            set
            {
                _validationGroup = value;
            }
        }

        public string PostBackUrl
        {
            get
            {
                return _postBackUrl;
            }

            set
            {
                _postBackUrl = value;
            }
        }

        public string CommandName
        {
            get
            {
                return _commandName;
            }

            set
            {
                _commandName = value;
            }
        }

        public string Value
        {
            get
            {
                return _value;
            }

            set
            {
                _value = value;
            }
        }

        public string Title
        {
            get
            {
                return _title;
            }

            set
            {
                _title = value;
            }
        }
        #endregion


        protected override void Render(HtmlTextWriter writer)
        {
            if (Controls.Count == 0)
            {
                writer.Write("<li");
                writer.Write(" name =\"" + UniqueID + "\"");
                writer.Write(" id=\"" + ClientID+"\"");
                if (_active) writer.Write(" class=\"active\"");
                writer.Write("><a href=\"");
                writer.WriteEncodedUrl(NavigateUrl ?? "#");
                writer.Write("\"");
                if (Events != null) writer.Write(" OnClick=\"" + Page.ClientScript.GetPostBackEventReference(this, Value)+"\"");

                if (Title != null) writer.Write(" title=\"" + Title + "\"");
                writer.Write(">");

                if (Glyphicon!=null)
                {
                    var g = new Glyphicon((Glyphicon.GlyphiconType)_glyphicon);
                    g.RenderControl(writer);
                }
                if (!string.IsNullOrEmpty(_text)) writer.WriteEncodedText(_text);
                writer.Write("</a></li>");
            }
            else
            {
                var dd = new BSDropDown
                {
                    Text = _text,
                    UseInList = true                    
                };

                if (SubMenu) dd.Orientation = BSDropDown.OrientationType.Right;
                dd.Controls=Controls;


                dd.RenderControl(writer);

            }
        }

        protected virtual void OnClick(EventArgs e)
        {
            EventHandler handler = (EventHandler)Events[EventClick];
            handler?.Invoke(this, e);
        }
        protected virtual void OnCommand(CommandEventArgs e)
        {
            Command?.Invoke(this, e);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            OnClick(new EventArgs());
            OnCommand(new CommandEventArgs(CommandName, CommandArgument));
        }
    }

如果我单独使用我的菜单项,或者使用我的aspx页面设置它们作为我的下拉控件的子项,它可以正常工作。但是如果我以编程方式创建它们,我的RaisePostBackEvent永远不会被触发。 __EventTarget很好但没有任何反应。

只有当我在我的aspx页面中设置我的下拉列表并以编程方式添加我的孩子时才能工作...(我在Page_init中添加我的控件)。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我使用一个控件数组来覆盖我的下拉列表的控件。如果我删除此集合并使用继承的控件属性,则可以正常工作。

我正在使用自己的数组,因为我的menuitem可以改变行为,如果有孩子,就会成为下拉列表。所以我必须将控制子项从menuitem移动到下拉列表(列表和控件无法设置)。

现在我必须像这样移动控制:

int l = Controls.Count;
for (int a = 0; a < l; a++)
dd.Controls.Add(Controls[0]);
Controls.Clear();

Controls.Add(dd);

其中dd是我的新下拉列表。希望它可以帮助别人。