0
我創建了兩個自定義控件以在我的C#代碼中使用引導程序。第一個使用此代碼實現引導下拉列表;RaisePostBackEvent未在custum子控件上觸發
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中添加了我的控件)。謝謝你的幫助。