2010-09-02 88 views
1

在asp.net visual studio 2008中创建一个web应用程序.. 在我的应用程序中,我手动创建了一个菜单控件.. 因为菜单根据需要变化,我希望加载它动态地从SQL表.. 帮我用简单的应用程序,动态加载菜单控制,我可以使用这些概念开发矿山....在web应用程序中动态创建菜单

感谢ü非常

回答

1

加载菜单的DataTable。 然后在你的ascx页面中创建一个中继器(我猜你的menur是一个用户控件) 为中继器创建模板。 将数据表绑定为该中继器的数据源。 你去了。一个简单的动态菜单。

不要忘了在你的控件上做一个缓存。或缓存数据表。所以你不会开到数据库中每个请求页面的连接上你的网站

这里是使用中继器的一些例子:

http://articles.sitepoint.com/article/asp-net-repeater-control http://www.w3schools.com/aspnet/aspnet_repeater.asp http://www.asp101.com/articles/john/repeater/default.asp

0

Download Source Code Here

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title>DataBase Driven Menu</title> 

</head> 
<body> 
<form id="form1" runat="server"> 
<div id="myslidemenu" class="jqueryslidemenu"> 
<asp:Menu ID="Menu1" runat="server" StaticEnableDefaultPopOutImage="False" 
Orientation="Horizontal" StaticSubMenuIndent="10px" BackColor="#FFFBD6" 
DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" 
ForeColor="#990000"> 
<DataBindings> 
<asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text" 
ToolTipField="ToolTip" /> 
</DataBindings> 
<DynamicHoverStyle BackColor="#990000" ForeColor="White" /> 
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> 
<DynamicMenuStyle BackColor="#FFFBD6" /> 
<DynamicSelectedStyle BackColor="#FFCC66" /> 
<StaticHoverStyle BackColor="#990000" ForeColor="White" /> 
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> 
<StaticSelectedStyle BackColor="#FFCC66" /> 
</asp:Menu> 

</div> 
</form> 
</body> 
</html> 

代码隐藏文件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Xml; 
using System.Data; 

public partial class _Default : System.Web.UI.Page 
{ 

protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
DataSet ds = new DataSet(); 
XmlDataSource xmlDataSource = new XmlDataSource(); 
xmlDataSource.ID = "xmlDataSource"; 
xmlDataSource.EnableCaching = false; 

string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\Admin\WebSite28\App_Data\Database.mdf;Integrated Security=True;User Instance=True"; 
using (SqlConnection conn = new SqlConnection(connStr)) 
{ 
string sql = "Select ID, Text,NavigateUrl,ParentID from Menu"; 
SqlDataAdapter da = new SqlDataAdapter(sql, conn); 
da.Fill(ds); 
da.Dispose(); 
} 

&nbsp; 

ds.DataSetName = "Menus"; 
ds.Tables[0].TableName = "Menu"; 
DataRelation relation = new DataRelation("ParentChild", 
ds.Tables["Menu"].Columns["ID"], 
ds.Tables["Menu"].Columns["ParentID"], 
true); 

relation.Nested = true; 
ds.Relations.Add(relation); 

xmlDataSource.Data = ds.GetXml(); 

//Reformat the xmldatasource from the dataset to fit menu into xml format 
xmlDataSource.TransformFile = Server.MapPath("~/TransformXSLT.xsl"); 

//assigning the path to start read all MenuItem under MenuItems 
xmlDataSource.XPath = "MenuItems/MenuItem"; 

//Finally, bind the source to the Menu1 control 
Menu1.DataSource = xmlDataSource; 
Menu1.DataBind(); 
} 

} 
} 

创建TransformXSLT.xsl

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes" encoding="utf-8"/> 
<!-- Find the root node called Menus 
and call MenuListing for its children --> 
<xsl:template match="/Menus"> 
<MenuItems> 
<xsl:call-template name="MenuListing" /> 
</MenuItems> 
</xsl:template> 

<!-- Allow for recusive child node processing --> 
<xsl:template name="MenuListing"> 
<xsl:apply-templates select="Menu" /> 
</xsl:template> 

<xsl:template match="Menu"> 
<MenuItem> 
<!-- Convert Menu child elements to MenuItem attributes --> 
<xsl:attribute name="Text"> 
<xsl:value-of select="Text"/> 
</xsl:attribute> 
<xsl:attribute name="ToolTip"> 
<xsl:value-of select="ToolTip"/> 
</xsl:attribute> 
<xsl:attribute name="NavigateUrl"> 
<xsl:value-of select="NavigateUrl"/> 
</xsl:attribute> 

<!-- Call MenuListing if there are child Menu nodes --> 
<xsl:if test="count(Menu) > 0"> 
<xsl:call-template name="MenuListing" /> 
</xsl:if> 
</MenuItem> 
</xsl:template> 
</xsl:stylesheet> 

创建SQL表

CREATE TABLE [dbo].[Menu](
[ID] [int] IDENTITY(1,1) NOT NULL, 
[Text] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, 
[ToolTip] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, 
[NavigateUrl] [varchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, 
[ParentID] [int] NULL)