Vừa rồi mình làm chương trình thấy làm 1 menu động khá là vất vả. (có lẽ vì khả năng tìm kiếm còn kém)
Giờ thì mình cũng làm dc 1 men tạm ưng ý.Mình muốn hướng dẫn lại cho những ai còn chưa biết.
OK..
B1.Tạo db
Mã:
CREATE DATABASE MenuDb
GO
USE MenuDb
GO
CREATE TABLE [tblMenu] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Ten] [varchar] (50) COLLATESQL_Latin1_General_CP1_CI_AS NULL ,
[Mota] [varchar] (255) COLLATESQL_Latin1_General_CP1_CI_AS NULL ,
[ParentID] [int] NULL ,
CONSTRAINT [PK_Menu] PRIMARY KEY CLUSTERED
(
[MenuID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO tblMenu
Select 'Product','A List of Products', NULL
UNION ALL Select 'Applications','Appliations',NULL
UNION ALL Select 'Document','Documentation', NULL
UNION ALL Select 'Support','Support', NULL
UNION ALL Select 'Download','Download', NULL
UNION ALL Select 'Background','ProductBackground', 1
UNION ALL Select 'Details','Product Details', 1
UNION ALL Select 'Mobile Device','Mobile DeviceApplications', 2
UNION ALL Select 'Portal','Portal Applications',2
UNION ALL Select 'Web Applicaitons','WebApplications', 2
UNION ALL Select 'Demo','Demo Applicaitons', 2
UNION ALL Select 'Performance Tests','ApplicationPerformance Tests', 2
UNION ALL Select 'Tutorials','TutorialDocumentation', 3
UNION ALL Select 'Programmers','ProgrammDocumentation', 3
UNION ALL Select 'FAQ','Frequently AskedQuestions', 4
UNION ALL Select 'Forum','Forum', 4
UNION ALL Select 'Contact Us','Contact Us', 4
UNION ALL Select 'InternetRestrictions','Internet Restrictions', 6
UNION ALL Select 'Speed Solution','Speed Solutions',6
UNION ALL Select 'Application Center Test','Application Center Test Results', 12
UNION ALL Select 'Modem Results','Modem Results',12
GO
B2.Trong Default.cs
Mã:
protected void Page_Load(object sender, EventArgs e)
{
//-----------------lay du lieu dua vao dataset
DataSet ds = new DataSet();
string connStr = "Data Source=.\\sqlexpress;Initial Catalog=DB_Myvnexpress;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "Select * from tblMenu";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
da.Dispose();
}
//đặt tên cho dataset
ds.DataSetName = "Menus";
//đặt tên cho bảng
ds.Tables[0].TableName = "tblMenu";
//tao datarelation
DataRelation relation = new DataRelation("ParentChild",
ds.Tables["tblMenu"].Columns["ID"],
ds.Tables["tblMenu"].Columns["ParentID"],
true);
//
relation.Nested = true; //Kiểm tra mối quan hệ đã được lồng vào nhau
//đưa relation vào ds
ds.Relations.Add(relation);
//ds.GetXml() sẽ trả về 1 chuỗi
xmlDataSource1.Data = ds.GetXml();
}
B3.tạo file XSLT :Tên file XSLT là : TransformXSLT.xsl Vs2k8 đuôi mở rộng là .xslt
Bạn nên tạo file có đuôi giống với bài này
//trang hướng dẫn http://www.ibm.com/developerworks/vn...slt/index.html
toàn bộ file:
Mã:
<?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="tblMenu" />
</xsl:template>
<xsl:template match="tblMenu">
<MenuItem>
<!-- Convert Menu child elements to MenuItem attributes -->
<xsl:attribute name="Text">
<xsl:value-of select="Ten"/>
</xsl:attribute>
<xsl:attribute name="ToolTip">
<xsl:value-of select="Mota"/>
</xsl:attribute>
<xsl:attribute name="NavigateUrl">
<xsl:text>?Sel=</xsl:text>
<xsl:value-of select="Ten"/>
</xsl:attribute>
<!-- Call MenuListing if there are child Menu nodes -->
<xsl:if test="count(tblMenu) > 0">
<xsl:call-template name="MenuListing" />
</xsl:if>
</MenuItem>
</xsl:template>
</xsl:stylesheet>
B4.trong file Default.aspx
Mã:
<div align="center">
<asp:Menu ID="Menu1" DataSourceID="xmlDataSource1" runat="server"
BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana"
ForeColor="#990000" StaticSubMenuIndent="10px" StaticDisplayLevels="1"
Orientation="Horizontal" >
<DataBindings>
<asp:MenuItemBinding DataMember="MenuItem"
NavigateUrlField="NavigateUrl" TextField="Text" ToolTipField="ToolTip"/>
</DataBindings>
<StaticSelectedStyle BackColor="#FFCC66" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicMenuStyle BackColor="#FFFBD6" />
<DynamicSelectedStyle BackColor="#FFCC66" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#990000" Font-Bold="False" ForeColor="White"/>
<StaticHoverStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />
</asp:Menu>
<asp:XmlDataSource ID="xmlDataSource1" TransformFile="~/TransformXSLT.xsl"
XPath="MenuItems/MenuItem" runat="server"/>
</div>
Ok---F5--->
View more random threads:
Trứng rung điều khiển từ xa Vibrator Spark Of Love Sản phẩm trứng rung bluetooth có điều khiển từ xa này được thiết kế với tông màu Rose Gold viền kim khí trải qua. kích tấc 7,4cm x 3,5cm giúp trứng...
TOP 3 trùy rung điều khiển từ xa...