Home  >  Article  >  Backend Development  >  Use xml as the data source and cooperate with the asp:Menu class to freely expand menu items

Use xml as the data source and cooperate with the asp:Menu class to freely expand menu items

黄舟
黄舟Original
2017-02-27 16:36:301410browse

First create a new xml file named menu with the following content:

<?xml version="1.0" encoding="utf-8" ?>
<menu>
  <menuItem url="~/Default.aspx" title="首页"  description=""/>
  <menuItem url="~/News.aspx" title="新闻"  description="">
    <menuItem url="~/News.aspx" title="国内新闻"  description=""/>
    <menuItem url="~/News.aspx" title="国际新闻"  description=""/>
  </menuItem>
</menu>

Then drag and drop a menu control on the page, and drag and drop an XmlDataSource control with the id of XmlDataSource1, and set the DataSourceID of the menu to XmlDataSource1.
Configure the XmlDataSource control so that DataFile="~/Menu.xml"
At this point, bind the data of the menu control and tell the menu control how to display the data filtered from the xml file. Select the menu control, select DataBindings in the properties window, in the pop-up dialog box, select menuItem in the list in the upper left corner, click the Add button to add menuItem to the list in the lower left, then select menuItem in this list, this is the right The list will show the properties to bind, so NavigateUrlField="url" TextField="title". Press the OK button to return to the design interface. At this time, the two menu items "Home" and "News" will be displayed.
The design html code is as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CoAffiliate._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSource1" Orientation="Horizontal" 
        StaticEnableDefaultPopOutImage="False">
            <DataBindings>
                <asp:MenuItemBinding DataMember="menuItem" NavigateUrlField="url" TextField="title" />
            </DataBindings>
        </asp:Menu>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Menu.xml" XPath="menu/menuItem">
        </asp:XmlDataSource>
    
    </div>
    </form>
</body>
</html>

The above is to use xml as the data source and cooperate with the asp:Menu class to freely expand the content of the menu items. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn