发布个SqlSiteMapProvider组件
2007-04-27 23:11 xiaosonl 阅读(2528) 评论(5) 编辑 收藏 举报该组件用于实现基于SQLServer的站点导航提供程序.
使用方法:
1.引用SqlSiteMapProvider.dll.
2.新建个类,取名为MySqlSiteMapProvider,继承CustomProvider.SqlSiteMapProviderBase抽象类.
3.实现抽象类的四个抽象方法,代码如下:
/// <summary>
/// 获取首页的URL
/// </summary>
public override string GetIndexPageURL()
{
return "~/Default.aspx";
}
/// <summary>
/// 根据ID返回所选页URL
/// </summary>
public override string GetPageURL(int id)
{
return "~/Category.aspx?id=" + id.ToString();
}
/// <summary>
/// 是否在菜单中显示首页
/// </summary>
public override bool IsShowIndexPage
{
get { return true; }
}
/// <summary>
/// 菜单标识符,用于标识一个菜单
/// </summary>
public override string NodesPortal
{
get { return "Main"; }
}
/// 获取首页的URL
/// </summary>
public override string GetIndexPageURL()
{
return "~/Default.aspx";
}
/// <summary>
/// 根据ID返回所选页URL
/// </summary>
public override string GetPageURL(int id)
{
return "~/Category.aspx?id=" + id.ToString();
}
/// <summary>
/// 是否在菜单中显示首页
/// </summary>
public override bool IsShowIndexPage
{
get { return true; }
}
/// <summary>
/// 菜单标识符,用于标识一个菜单
/// </summary>
public override string NodesPortal
{
get { return "Main"; }
}
4.建立数据库:
CREATE TABLE [dbo].[T_Menu](
[NodeID] [int] NOT NULL,
[ParentID] [int] NOT NULL,
[NodeName] [nvarchar](50) NOT NULL,
[Description] [nvarchar](255) NULL,
[PortalID] [varchar](50) NOT NULL
) ON [PRIMARY]
[NodeID] [int] NOT NULL,
[ParentID] [int] NOT NULL,
[NodeName] [nvarchar](50) NOT NULL,
[Description] [nvarchar](255) NULL,
[PortalID] [varchar](50) NOT NULL
) ON [PRIMARY]
5.配置Web.Config
<connectionStrings>
<add name="connect" connectionString="server=XIAOSONL;database=SqlMenu;uid=sa;pwd=aN541084" />
</connectionStrings>
<system.web>
<siteMap enabled="true" defaultProvider="MySqlSiteMapProvider">
<providers>
<add name="MySqlSiteMapProvider" type="MySqlSiteMapProvider" securityTrimmingEnabled="false" connectionStringName="connect" description="SiteMapProvider for SQL2000/2005"/>
</providers>
</siteMap>
</system.web>
<add name="connect" connectionString="server=XIAOSONL;database=SqlMenu;uid=sa;pwd=aN541084" />
</connectionStrings>
<system.web>
<siteMap enabled="true" defaultProvider="MySqlSiteMapProvider">
<providers>
<add name="MySqlSiteMapProvider" type="MySqlSiteMapProvider" securityTrimmingEnabled="false" connectionStringName="connect" description="SiteMapProvider for SQL2000/2005"/>
</providers>
</siteMap>
</system.web>
6.在Web页上放置一个SiteMapDataSource控件,再拖一个Menu或TreeView控件,将数据源设为SiteMapDataSource.运行就可以看到结果了.
数据库的名称也可以自定义,只需重写NodesTable属性:
/// <summary>
/// 储存节点信息的数据库表结构
/// </summary>
public virtual NodeTableSchema NodesTable
{
get
{
CustomProvider.NodeTableSchema nodesTable = new CustomProvider.NodeTableSchema();
nodesTable.TableName = "T_Menu1";
nodesTable.ID = "NodeID1";
nodesTable.ParentID = "ParentID1";
nodesTable.Name = "NodeName1";
nodesTable.Description = "Description1";
nodesTable.PortalID = "PortalID1";
return nodesTable;
}
}
/// 储存节点信息的数据库表结构
/// </summary>
public virtual NodeTableSchema NodesTable
{
get
{
CustomProvider.NodeTableSchema nodesTable = new CustomProvider.NodeTableSchema();
nodesTable.TableName = "T_Menu1";
nodesTable.ID = "NodeID1";
nodesTable.ParentID = "ParentID1";
nodesTable.Name = "NodeName1";
nodesTable.Description = "Description1";
nodesTable.PortalID = "PortalID1";
return nodesTable;
}
}
但是结构必需相同.列的数据类型要求转换成C#数据类型后一致.
这是小弟在园里了第一次发文章,写的不好莫怪.
附件:
1. SqlSiteMapProvider.dll
2.Demo
需要源代码的朋友可以发邮件向我要,xiaosonl@163.com
--------------------------个人签名的分割线--------------------------------------
我的个人综合博客:http://www.xiaosonl.com