实现一个可自定制的个性化导航

主要的功能是一个页面\WebSite.aspx从数据库读取所有的网址链接,供用户选择。
另一个页面Default.aspx用户呈现用户选择的网址,并可以进行删除。简单的页面图形如下所示。
WebSite.aspx 的部分截图
未标题-1 拷贝.jpg
Default.aspx的部分截图
���-1 ����.jpg
用户可以从WebSite.aspx页中自定义需要的网址链接,点击“确定”按钮后,自动存入aspnet_Profile数据表中,Default.aspx页的内容是从aspnet.Profile数据表读取出来的。这样就实现个性化设置。

该功能的核心思想是将一个自定义的类串行化,并存入到aspnet_Profile数据表中,以便实现个性化,如果将Default.aspx封装成一个WebPart控件,就可以实现更多的功能。
主要的代码:
1、配置文件
ContractedBlock.gif ExpandedBlockStart.gif Web.Config
1None.gif<connectionStrings>
2None.gif      <add name="profileProvider" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
3None.gif</connectionStrings>

1 None.gif   < profile defaultProvider = " SqlProvider " >
2 None.gif       < providers  >
3 None.gif         < clear />
4 None.gif         < add name  = " SqlProvider "  connectionStringName = " profileProvider "  type = " System.Web.Profile.SqlProfileProvider "   applicationName = " / "   />
5 None.gif       </ providers >
6 None.gif       < properties >
7 None.gif          < add name = " websit "  type = " WebSite "  serializeAs = " Binary "   allowAnonymous = " true " />
8 None.gif       </ properties >       
9 None.gif     </ profile >
2、存入数据库的自定义类

ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Web;
 5None.gifusing System.Web.Security;
 6None.gifusing System.Web.UI;
 7None.gifusing System.Web.UI.WebControls;
 8None.gifusing System.Web.UI.WebControls.WebParts;
 9None.gifusing System.Web.UI.HtmlControls;
10None.gif
11None.gifusing System.Collections;
12None.gif
13ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
14InBlock.gif/// WebSite 的摘要说明
15ExpandedBlockEnd.gif/// </summary>

16None.gif[Serializable]
17None.gifpublic class WebSite
18ExpandedBlockStart.gifContractedBlock.gifdot.gif{
19InBlock.gif    public Hashtable _WebSiteItems = new Hashtable();
20InBlock.gif
21InBlock.gif    public ICollection WebSiteItems
22ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
23ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gifreturn _WebSiteItems.Values; }
24ExpandedSubBlockEnd.gif    }

25InBlock.gif    public void AddItem(int ID, string Name, string  Url)
26ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
27InBlock.gif        //在向哈西表中添加内容前,先判断该商品是否已经添加上了,如果没有则添加,否则不变。
28InBlock.gif        WebSiteItem item = (WebSiteItem)_WebSiteItems[ID];
29InBlock.gif        if (item == null)
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            _WebSiteItems.Add(ID, new WebSiteItem(ID, Name, Url));
32ExpandedSubBlockEnd.gif        }

33InBlock.gif        else
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif
36InBlock.gif            _WebSiteItems[ID] = item;
37ExpandedSubBlockEnd.gif        }

38ExpandedSubBlockEnd.gif    }

39InBlock.gif
40InBlock.gif    public void RemoveItem(int ID)
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        WebSiteItem item = (WebSiteItem)_WebSiteItems[ID];
43InBlock.gif        if (item == null)
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            return;
46ExpandedSubBlockEnd.gif        }
        
47InBlock.gif        else
48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
49InBlock.gif            _WebSiteItems.Remove(ID);
50ExpandedSubBlockEnd.gif        }

51InBlock.gif       
52ExpandedSubBlockEnd.gif    }

53InBlock.gif    
54ExpandedBlockEnd.gif}

55None.gif
56None.gif[Serializable]
57None.gifpublic class WebSiteItem
58ExpandedBlockStart.gifContractedBlock.gifdot.gif{
59InBlock.gif    private string webSiteName;
60InBlock.gif    private string webSiteUrl;
61InBlock.gif    private int id;
62InBlock.gif
63InBlock.gif    public int ID
64ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
65InBlock.gif        get
66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
67InBlock.gif            return id;
68ExpandedSubBlockEnd.gif        }

69ExpandedSubBlockEnd.gif    }

70InBlock.gif
71InBlock.gif    public string WebSiteName
72ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
73InBlock.gif        get
74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
75InBlock.gif            return webSiteName;
76ExpandedSubBlockEnd.gif        }

77ExpandedSubBlockEnd.gif    }

78InBlock.gif    public string WebSiteUrl
79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
80InBlock.gif        get
81ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
82InBlock.gif            return webSiteUrl;
83ExpandedSubBlockEnd.gif        }

84ExpandedSubBlockEnd.gif    }

85InBlock.gif
86InBlock.gif    public  WebSiteItem(int id,string name,string url)
87ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
88InBlock.gif        webSiteUrl = url;
89InBlock.gif        webSiteName = name;
90InBlock.gif        this.id = id;
91ExpandedSubBlockEnd.gif    }

92ExpandedBlockEnd.gif}

93None.gif


3、WebSite.aspx

ContractedBlock.gif ExpandedBlockStart.gif
 1None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebSite.aspx.cs" Inherits="NavigationControls_WebSite" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4None.gif
 5None.gif<html xmlns="http://www.w3.org/1999/xhtml" >
 6None.gif<head runat="server">
 7None.gif    <title>无标题页</title>
 8None.gif</head>
 9None.gif<body>
10None.gif    <form id="form1" runat="server">
11None.gif    <div>
12None.gif    <asp:DataList ID="dlWebSite" runat="server" DataSourceID="SqlDataSource1">
13None.gif    <ItemTemplate>
14None.gif    <asp:CheckBox  ID="chbWebSite" runat="server"/>
15None.gif    <asp:HyperLink ID="hlWebSite" NavigateUrl='<%#Eval("WebSiteUrl") %>' runat="Server" Text='<%#Eval("WebSiteName")%>'></asp:HyperLink>
16None.gif    <asp:Label ID="webId" Text='<%#Eval("Id") %>' runat="server" Visible="false"></asp:Label>
17None.gif    </ItemTemplate>
18None.gif     </asp:DataList>
19None.gif     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:xqzyConnectionString %>" SelectCommand="SELECT * FROM [Navigator_WebSite]"></asp:SqlDataSource>
20None.gif        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
21None.gif        <asp:GridView ID="GridView1" runat="server" ShowHeader="False" AutoGenerateColumns="False" Height="139px" Width="103px">
22None.gif            <Columns>
23None.gif                <asp:TemplateField>
24None.gif                    <ItemTemplate>
25None.gif                    <table>
26None.gif                    <tr>
27None.gif                    <td><a href='<%#Eval("WebSiteUrl") %>'><%#Eval("WebSiteName"%></a>  </td>
28None.gif                    </tr>
29None.gif                    </table>                        
30None.gif                    </ItemTemplate>
31None.gif                </asp:TemplateField>
32None.gif            </Columns>
33None.gif          
34None.gif        
35None.gif        </asp:GridView>
36None.gif        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></div>
37None.gif    </form>
38None.gif    
39None.gif   
40None.gif</body>
41None.gif</html>
42None.gif
4、WebSite.aspx.cs
ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gif
12None.gifpublic partial class NavigationControls_WebSite : System.Web.UI.Page
13ExpandedBlockStart.gifContractedBlock.gifdot.gif{  
14InBlock.gif    
15InBlock.gif    protected void Page_Load(object sender, EventArgs e)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17InBlock.gif   
18InBlock.gif       
19ExpandedSubBlockEnd.gif    }

20InBlock.gif    protected void Button1_Click(object sender, EventArgs e)
21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
22InBlock.gif        if (Profile.websit==null)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            Profile.websit = new WebSite();
25ExpandedSubBlockEnd.gif        }

26InBlock.gif        //思路:将用户选择的站点存入一个类中,以便在网站导航控件中使用。
27InBlock.gif
28InBlock.gif        //WebSiteFunc myWebSite = new  WebSiteFunc();
29InBlock.gif
30InBlock.gif        //int i = 0;
31InBlock.gif
32InBlock.gif        foreach (DataListItem dl in this.dlWebSite.Items)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            CheckBox cb = (CheckBox)dl.FindControl("chbWebSite");
35InBlock.gif            if (cb.Checked)
36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
37InBlock.gif                HyperLink  hl = (HyperLink )dl.FindControl("hlWebSite");
38InBlock.gif                Label myLabel = (Label)dl.FindControl("webId");
39InBlock.gif                int id = Convert.ToInt32(myLabel.Text);
40InBlock.gif
41InBlock.gif                Profile.websit.AddItem(id, hl.Text, hl.NavigateUrl);
42InBlock.gif                               
43ExpandedSubBlockEnd.gif            }

44ExpandedSubBlockEnd.gif        }

45InBlock.gif
46InBlock.gif      
47InBlock.gif
48InBlock.gif
49ExpandedSubBlockEnd.gif    }

50InBlock.gif    protected void Button2_Click(object sender, EventArgs e)
51ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
52InBlock.gif       
53InBlock.gif        Response.Redirect("Default.aspx");
54InBlock.gif
55ExpandedSubBlockEnd.gif    }

56ExpandedBlockEnd.gif}

57None.gif
5.Default.aspx
ContractedBlock.gif ExpandedBlockStart.gif
 1None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="NavigationControls_Default" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4None.gif
 5None.gif<html xmlns="http://www.w3.org/1999/xhtml" >
 6None.gif<head runat="server">
 7None.gif    <title>无标题页</title>
 8None.gif</head>
 9None.gif<body>
10None.gif    <form id="form1" runat="server">
11None.gif    <div>
12None.gif    <asp:GridView ID="GridView1" runat="server"  DataKeyNames="ID" AutoGenerateColumns="False" OnSelectedIndexChanged="RemoveCartItem">
13None.gif        <Columns>
14None.gif            <asp:TemplateField>
15None.gif                <ItemTemplate>
16None.gif                    <asp:HyperLink ID="HyperLink1"  Text='<%#Eval("WebSiteName") %>' NavigateUrl='<%#Eval("WebSiteUrl") %>' runat="server"></asp:HyperLink>
17None.gif                </ItemTemplate>
18None.gif            </asp:TemplateField>
19None.gif            <asp:CommandField ShowSelectButton="True" />
20None.gif        </Columns>
21None.gif        </asp:GridView>
22None.gif        &nbsp;
23None.gif    </div>
24None.gif        
25None.gif    </form>
26None.gif</body>
27None.gif</html>
28None.gif

6、Default.aspx.cs
ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gif
12None.gifpublic partial class NavigationControls_Default : System.Web.UI.Page
13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
14InBlock.gif    protected void Page_Load(object sender, EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        if (!Page.IsPostBack)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            Bind();
19ExpandedSubBlockEnd.gif        }

20InBlock.gif        //if ((Session["web"]) != null)
21InBlock.gif        //{
22InBlock.gif        //    WebSiteFunc myWeb = (WebSiteFunc)Session["web"];
23InBlock.gif
24InBlock.gif        //    GridView1.DataSource = myWeb.WebSite;
25InBlock.gif        //    GridView1.DataBind();
26InBlock.gif        //}
27ExpandedSubBlockEnd.gif    }

28InBlock.gif
29InBlock.gif    protected void Bind()
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
31InBlock.gif        if (Profile.websit!= null)
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            GridView1.DataSource = Profile.websit.WebSiteItems;
34InBlock.gif            GridView1.DataBind();
35ExpandedSubBlockEnd.gif        }

36ExpandedSubBlockEnd.gif    }

37InBlock.gif   
38InBlock.gif    protected void RemoveCartItem(object sender, EventArgs e)
39ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
40InBlock.gif        // 获取被选中商品的主键ID
41InBlock.gif        int id = (int)GridView1.SelectedDataKey.Value;
42InBlock.gif
43InBlock.gif        //int ID = (int)CartGrid.SelectedDataKey.Value;
44InBlock.gif        // 利用ID,从Profile对象购物车中删除该商品
45InBlock.gif        Profile.websit.RemoveItem(id);
46InBlock.gif        // 显示购物车数据
47InBlock.gif        Bind();
48ExpandedSubBlockEnd.gif    }

49InBlock.gif    
50ExpandedBlockEnd.gif}

51None.gif

转载于:https://www.cnblogs.com/qyjun/archive/2007/09/02/878744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值