在菜单中显示数据库值 [英] Displaying Database values into a menu
问题描述
我正在尝试动态创建菜单.
我在sql server中有数据库(MenuDb)和表(Menu).
菜单表由
组成
MenuId,MenuText,父ID.
1硕士0
2交易0
3个报告0
4帮助0
5公司1
6工厂1
7仓库1
8位用户1
9个项目1
10项商品内向2
11个商品外寄商品2
12生产3
13 UserMatric 3
14其次3
15关于4
16 SNO 9
17类别9
18 R1 14
19 R2 14
这是我定义asp的aspx页面代码:menu
Hi,
I am trying to dynamically create a menu.
I have database(MenuDb) and table(Menu) in sql server.
The menu table consists of
MenuId , MenuText , Parent ID.
1 Masters 0
2 Transactions 0
3 Reports 0
4 Help 0
5 Company 1
6 Factory 1
7 Warehouse 1
8 Users 1
9 Items 1
10 GoodsInward 2
11 GoodsOutward 2
12 Production 3
13 UserMatric 3
14 Secondly 3
15 About 4
16 SNO 9
17 Category 9
18 R1 14
19 R2 14
Here is my aspx page code where I am defining asp:menu
<asp:menu id=""menu"" datasourceid=""xmlDataSource"" runat=""server"" backcolor=""#FFFBD6"" dynamichorizontaloffset=""2"" font-names=""Verdana"" forecolor=""#990000"" staticsubmenuindent=""10px"" staticdisplaylevels=""1"" orientation=""Horizontal"><br" mode="hold" xmlns:asp="#unknown" /> <DataBindings>
<asp:MenuItemBinding DataMember="Text" NavigateUrlField="NavigateUrl" TextField="MenuText" ToolTipField="ToolTip"/>
</DataBindings>
</asp:Menu>
<asp:XmlDataSource ID="xmlDataSource" runat="server" TransformFile="~/TransformXSLT.xslt" XPath="MenuItems/MenuItem"/>
以及页面后面的代码
And the code behind page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblTime.Text = DateTime.Now.ToString()
Dim ds As New DataSet()
Dim connStr As String = "Data Source=LAKSHMI-E31FABD;Initial Catalog=MenuDb;User Id=sa;Password=rootanupama;Trusted_Connection=true;"
Using con As New SqlConnection(connStr)
Dim sql As String = "Select MenuID,MenuText,ParentID from Menu"
Dim da As SqlDataAdapter = New SqlDataAdapter(sql, con)
da.Fill(ds)
da.Dispose() 'optional
End Using
ds.DataSetName = "Menus"
ds.Tables(0).TableName = "Menu"
Dim relation As DataRelation = New DataRelation("ParentChild", ds.Tables("Menu").Columns("MenuID"), ds.Tables("Menu").Columns("ParentID"), False)
relation.Nested = True
ds.Relations.Add(relation)
xmlDataSource.Data = ds.GetXml()
If Request.Params("Sel") IsNot Nothing Then
Page.Controls.Add(New System.Web.UI.LiteralControl("You selected " + Request.Params("Sel")))
End If
End Sub
我的xml xslt文件
My xml xslt file
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="/Menus">
<MenuItems>
<xsl:call-template name="MenuListing" />
</MenuItems>
</xsl:template>
<!-- Allow for recursive 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="MenuText"/>
</xsl:attribute>
<xsl:attribute name="NavigateUrl">
<xsl:text>?Sel=</xsl:text>
<xsl:value-of select="Text"/>
</xsl:attribute>
<!-- Recursively call MenuListing forchild menu nodes -->
<xsl:if test="count(Menu) >0">
<xsl:call-template name="MenuListing" />
</xsl:if>
</MenuItem>
</xsl:template>
</xsl:stylesheet>
我的问题是,当我尝试调试此代码时,我没有将值获取到菜单中,而是根据数据库正确显示了关系.
例如.对于主选项卡,我有5个子节点,它显示了主标题下方的5个链接,但所有值都显示为菜单项".
请让我知道我要去哪里哪里或需要更改什么才能获取输出.
在此先感谢.
My Problem is when I am trying to debug this code I am not getting the values to the menu but it is show the relation correctly according to the database.
Eg. For the master tab I have 5 child nodes and it is showing the 5 links below the main heading but all the values are being displayed as ''Menu Item''.
Please let me know where I am going wrong or what I need to change in order to get the output.
Thanks in Advance.
推荐答案
安纳帕玛(Anupama),
我根本没有遇到您要描述的问题,但是仍然因为前三个字符(Name.Substring(0,3))在我们的名字中是相同的... :-D :-D :-D .. ...所以我很想帮助您.您可以尝试下面给出的链接,可能对您有帮助.我建议您通过其他方法而不是使用XML.
http://www.codeproject.com/KB/MFC/Dynamic_Menu_and_ToolBar
http://www.codeproject.com/KB/menus/csdynmenudemo.aspx
http://www.codeproject.com/KB/scripting/dynamic_menu
阿努拉格
@cheers @
Hi Anupama,
I am not at all getting the problem you are trying to describe, but still since the first three characters (Name.Substring(0,3)) are same in our names... :-D :-D :-D .....so I have an urge to help you out. You please try the links given below, may be these could be of any help to you. I would suggest you doing by some other method rather than using XML.
http://www.codeproject.com/KB/MFC/Dynamic_Menu_and_ToolBar
http://www.codeproject.com/KB/menus/csdynmenudemo.aspx
http://www.codeproject.com/KB/scripting/dynamic_menu
Anurag
@cheers@
这篇关于在菜单中显示数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!