Extaspnet application tips (5)-dynamically create a toolbar menu

Source: Internet
Author: User
Article-better Layout

Problem description
Shguo users mentioned in an email that they wanted to dynamically create toolbar menus.

The general steps are as follows:
1. Provide a menu. xml file:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Menu> <menuitem text = "Homepage" navigateurl = "default. aspx "/> <menuitem text =" news "> <menuitem text =" domestic news "navigateurl =" News. aspx? Type = guonei "/> <menuitem text =" International News "navigateurl =" news. aspx? Type = Guoji "> <menuitem text =" Asia News "navigateurl =" news. aspx? Type = Yazhou "/> <menuitem text =" European News "navigateurl =" news. aspx? Type = ouzhou "/> </menuitem> </menu>

2. Place a panel and a toolbar control on the page:

<Ext: pagemanager id = "pagemanager1" runat = "server"/> <Ext: Panel showborder = "false" showheader = "false" runat = "server"> <Ext: toolbar runat = "server"> </ext: toolbar> </ext: Panel>

3. Generate a page like this:

Implemented in the form of page declaration
This method is relatively simple. You need to note that you can click a toolbar item-homepage-to transfer it to a link address.
However, if you use the ext: hyperlink control, the page displays poorly. So we still use Ext: button, but we can achieve the purpose of link through this method:

 
Enablepostback = "false" onclientclick = "window. Open ('default. aspx ',' _ blank ');"

AllSource code:

<Ext: Panel showborder = "false" showheader = "false" runat = "server"> <Ext: toolbar runat = "server"> <Ext: button enablepostback = "false" onclientclick = "window. open ('default. aspx ',' _ blank '); "text =" Homepage "runat =" server "> </ext: button> <Ext: button enablepostback = "false" text = "news" runat = "server"> <menu> <Ext: menuhyperlink runat = "server" target = "_ blank" navigateurl = "News. aspx? Guonei "text =" domestic news "> </ext: menuhyperlink> <Ext: menuhyperlink runat =" server "target =" _ blank "navigateurl =" news. aspx? Guoji "text =" International News "> <menu> <Ext: menuhyperlink runat =" server "target =" _ blank "navigateurl =" news. aspx? Yazhou "text =" Asia News "> </ext: menuhyperlink> <Ext: menuhyperlink runat =" server "target =" _ blank "navigateurl =" news. aspx? Ouzhou "text =" European News "> </ext: menuhyperlink> </menu> </ext: button> </ext: toolbar> </ext: Panel>

Implemented in programming
1. First load the xml configuration file to xmldocument:

 
Private xmldocument loadxml () {// load the xml configuration file string xmlpath = server. mappath ("~ /Other/menu. XML "); string xmlcontent = string. empty; using (streamreader sr = new streamreader (xmlpath) {xmlcontent = sr. readtoend ();} xmldocument Doc = new xmldocument (); Doc. loadxml (xmlcontent); Return Doc ;}

2. Generate the toolbar button of the toolbar

Xmldocument Doc = loadxml (); // The child nodes of the root node xmlnodelist nodes = Doc. documentelement. childnodes; foreach (xmlnode node in nodes) {extaspnet. button BTN = new button (); BTN. TEXT = node. attributes ["text"]. value; BTN. enablepostback = false; toolbar1.items. add (BTN); // if this node does not have a subnode if (node. childnodes. count = 0) {xmlattribute attrurl = node. attributes ["navigateurl"]; If (attrurl! = NULL) {BTN. onclientclick = string. format ("window. open ('{0}', '_ blank') ", attrurl. value) ;}} else {// recursively add all menu resolvemenu (BTN, node. childnodes );}}

3. Add all menus recursively.

Private void resolvemenu (imenu BTN, xmlnodelist nodes) {foreach (xmlnode node in nodes) {xmlattribute attrurl = node. attributes ["navigateurl"]; If (attrurl! = NULL) {extaspnet. menuhyperlink lnk = new extaspnet. menuhyperlink (); lnk. TEXT = node. attributes ["text"]. value; lnk. navigateurl = attrurl. value; lnk. target = "_ blank"; BTN. menu. items. add (lnk); If (node. childnodes. count> 0) {resolvemenu (lnk, node. childnodes );}}}}

Note: The first parameter of resolvemenu is an interface imenu, which can accept all class instances with the menu attribute.
This interface is used to be compatible with toolbar tool buttons and menuhyperlink.

Go to http://extaspnet.codeplex.com/sourcecontrol/listdownloadablecommits.aspxto download the latest sourceCodeThis example is in other \ menu_dynamic_run.aspx.

Further, add background event processing functions for the toolbar buttons.

 
Protected void page_load (Object sender, eventargs e) {loadtoolbar (); extaspnet. button BTN = new button (); BTN. TEXT = "Get toolbar buttons"; BTN. click + = new eventhandler (btn_click); toolbar1.items. add (BTN);} private void btn_click (Object sender, eventargs e) {alert. show ("toolbar buttons:" + toolbar1.items. count );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.