忙了2个下午,终于解决了不操作XML文件,通过编程动态修改SiteMap中节点的问题,msdn上的例子不是很好,自己研究了一下,在此感谢在msdn和csdn帮助我的朋友.SiteMap当中首先肯定要加入固定的节点,我的论坛只有Forum.aspx,Topic.aspx和Post.aspx3个页面,Forum.aspx显示大分类和在大分类下面的小板块,Topic.aspx显示每个小板块中的帖子,Post.aspx显示帖子和回复内容.一共有3个参数,ForumID,TopID,PostID,分别存于数据库中,用于标识各个记录,类型为Guid.3个页面url格式一般为forum.aspx,Topic.aspx?ForumID=xxx&TopID=xxx,Post.aspx?ForumID=xxx&TopID=xxx&PostID=xxx
在SiteMap中写入固定节点:
1 <?xml version="1.0" encoding="utf-8" ?>
2 <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
3     <siteMapNode url="Forum.aspx" title="FLT Forum"  description="">
4         <siteMapNode url="Topic.aspx" title="帖子列表"  description="" >
5           <siteMapNode url="Post.aspx" title="查看帖子"  description="" />
6         </siteMapNode>
7     </siteMapNode>
8 </siteMap>
基本上这个只标识一下网站结构
在master页的page_load中注册事件:
1 protected void Page_Load(object sender, EventArgs e)
2     {
3         if (!IsPostBack)
4         {
5            SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ForumPath);
6         }
7     }
事件处理代码:
总结一下关于编程动态修改SiteMap的问题SiteMapNode ForumPath(object sender, SiteMapResolveEventArgs e)
    }

下面是3个方法,返回ForumID,TopID,PostID3个参数
 1 private string getForumID(out string strClassName)
 2     {
 3         string strForumID = "";
 4         if (System.Web.HttpContext.Current.Request.QueryString["ForumID"!= null)//这里必须要写System.Web.HttpContext.Current.Request.QueryString,而不能直接写Request.QueryString,这个问题在csdn上很长时间没解决啊,后来在msdn上一位朋友指点.
 5         {
 6             strForumID=System.Web.HttpContext.Current.Request.QueryString["ForumID"].ToString();
 7             strClassName = xxx;//通过数据库或者Dataset获取分类名
 8             return strForumID;
 9         }
10         else
11         {
12             strPartName = "";
13             return strForumID;
14         }
15 
16     }
17 
18 private string getTopID(out string strTopName)
19     {
20         string strTopID = "";
21 
22         if (System.Web.HttpContext.Current.Request.QueryString["TopID"!= null)
23         {
24             strTopID = System.Web.HttpContext.Current.Request.QueryString["TopID"].ToString();
25             strTopName = xxx;//同上
26             return strTopID;
27         }
28         else
29         {
30             strTopName = "";
31             return strTopID;
32         }
33     }
34 
35 private string getPostID(out string strPostTitle)
36     {
37         string strPostID = "";
38 
39         if (System.Web.HttpContext.Current.Request.QueryString["PostID"!= null)
40         {
41             strPostID = System.Web.HttpContext.Current.Request.QueryString["PostID"].ToString();
42             strPostTitle = xxx;//同上
43             return strPostID;
44         }
45         else
46         {
47             strPostTitle = "";
48             return strPostID;
49         }
50     }


总结一下,这个方法对于结构比较简单(主要是深度比较小的)的网站,个人感觉要比操作XML文件简单,直接写程序就可以实现动态修改了,但是对于结构比较复杂(深度比较大的)网站,还是写XML比较直接.

相关文章:

  • 2022-03-02
  • 2022-02-12
  • 2022-02-02
  • 2021-12-03
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
猜你喜欢
  • 2022-12-23
  • 2021-07-03
  • 2021-11-20
  • 2022-12-23
  • 2021-09-27
  • 2021-12-24
  • 2022-01-20
相关资源
相似解决方案