问题描述
我正在创建一个网站,希望在其中提供一些简洁的上下文菜单控件.我已经找到了一些方便的代码来创建菜单(但是,如果您拥有"em",则发布"em"),但是我正在寻找为该站点创建标准UC的代码.我希望能够以与HTML select甚至asp:dropdown相同的方式向控件添加项目(更可能是后者,因为HTML将以相同的方式从代码中生成). />
这有可能吗?
Hi,
I am creating a website where I would like some neat context menu controls. I have already found some handy code to create the menu (but post ''em if you''ve got ''em) but I''m looking to create a standard UC for the site. I was hoping to be able to add items to the control in the same way that an HTML select does or even the asp:dropdown does (more likely the latter as the HTML will be produced from the code in the same way).
Is this even possible?
<uc:ContextMenu id="CustomControl">
<uc:MenuLink Value="blah" Text="blah" Url="\blah" />
<uc:MenuButton Value="blah" Text="blah" önClick="Blah_OnClick" />
</uc:ContextMenu>
以及如何完成?
也许与winform用户控件中的设计时属性相同?
PS:我还没有真正开始查看控件属性,因此请发布有关将这些属性添加到控件的任何有用信息
在此先感谢^ _ ^
And how is it done?
Maybe it''s the same as design time attributes in winform user controls?
PS: I haven''t really started looking at control attributes yet so please post any useful info on adding these to the control
Thanks in advance ^_^
推荐答案
[ParseChildren(false)]
[PersistChildren(true)]
public partial class ContextMenu : UserControl
{...}
这样,您就可以在UserControl的开始和结束标记之间添加项目.我还在ContextMenuitem类中包含了这些标签,以便可以添加无限嵌套的子菜单
您可能要使用的另一个重载是
That will allow you to add items between the opening and closing tags of your UserControl. I have also included those tags on my ContextMenuitem class so that I can add infinite nested submenus
Another overload you may want to use is
protected override void AddedControl(Control control, int index)
{
try
{
ContextMenuItem cmitem = ((ContextMenuItem)control);
if (_subItems == null)
_subItems = new List<contextmenuitem>();
List<contextmenuitem> newlist = new List<contextmenuitem>();
newlist.AddRange(_subItems.AsQueryable().Take(index).ToList());
newlist.Add(cmitem);
newlist.AddRange(_subItems.AsQueryable().TakeWhile((item, i) => i >= index));
_subItems = newlist;
}
catch (InvalidCastException ice)
{
//This is an expected error more several controls which should not be parsed here anyway
}
}
</contextmenuitem></contextmenuitem></contextmenuitem>
您可以对几种不同的类类型执行检查,也可以使用继承,只要每个控件具有相同的属性即可.
我个人使用了一个项目类,使RenderControl重载,因此它什么也不做,并在一个新的RenderHtml中编写了标记,我从ContectMenu类的``Render控件''调用了该标记.这样,您无需担心HtmlControl RenderControl被调用.自己写吧:
You could perform a check for several different class types or use inheritance, so long as each control has the same properties.
I personally used one item class, overloaded the RenderControl so that it did nothing and wrote the markup in a new RenderHtml that I called from the ContectMenu class'' Render control. That way you don''t need to worry about the HtmlControl RenderControl being called. Just write it yourself:
public override void RenderControl(HtmlTextWriter writer){}
public void Renderhtml(HtmlTextWriter writer, string previousId, int iteration = 0)
{
writer.WriteFullBeginTag("li");
string currentId = previousId + "_" + iteration.ToString();
if (this.ViewState["ControlStyle"] != null)
{
Control_Style style = (Control_Style)Enum.Parse(typeof(Control_Style), this.ViewState["ControlStyle"].ToString());
string closeTag = string.Empty;
switch (style)
{
case Control_Style.Button:
writer.WriteBeginTag("button");
closeTag = "</button>";
break;
case Control_Style.Hyperlink:
writer.WriteBeginTag("a");
closeTag = "";
break;
case Control_Style.SubMenu:
writer.WriteBeginTag("span");
closeTag = "";
break;
}
foreach (object key in this.ViewState.Keys)
{
if (this.ViewState[key.ToString()] != null && this.ViewState[key.ToString()].ToString().Length > 0)
writer.WriteAttribute(key.ToString(), this.ViewState[key.ToString()].ToString());
}
writer.Write(">");
writer.WriteLine(this.Text);
writer.WriteLine(closeTag);
if (_subItems != null && _subItems.Count > 0)
{
writer.WriteBeginTag("div");
writer.WriteAttribute("id", currentId);
writer.Write(">");
writer.WriteFullBeginTag("ul");
foreach (ContextMenuItem cmi in _subItems)
{
cmi.Renderhtml(writer, currentId);
}
writer.WriteEndTag("ul");
writer.WriteEndTag("div");
}
}
else
throw new HttpUnhandledException("Menu Items must be either a HtmlControls or a Sub Menu!");
writer.WriteEndTag("li");
}
这实在是太过分了,但是我还确保了我的子菜单div具有独特的ID,以便可以使每个javascript都能正常工作.我仍然还没有工作,但是如果有人在完成后需要它,我会把它贴出来:D
This is prolly overkill but I have also made sure that my submenu divs have distinctive IDs so that I can make the javascript work over each one. I still haven''t got that bit working yet but I''ll post it if anyone needs it when I''m done :D
这篇关于将项目添加到Web用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!