主要的功能是一个页面\WebSite.aspx从数据库读取所有的网址链接,供用户选择。
另一个页面Default.aspx用户呈现用户选择的网址,并可以进行删除。简单的页面图形如下所示。
WebSite.aspx 的部分截图

Default.aspx的部分截图

用户可以从WebSite.aspx页中自定义需要的网址链接,点击“确定”按钮后,自动存入aspnet_Profile数据表中,Default.aspx页的内容是从aspnet.Profile数据表读取出来的。这样就实现个性化设置。
该功能的核心思想是将一个自定义的类串行化,并存入到aspnet_Profile数据表中,以便实现个性化,如果将Default.aspx封装成一个WebPart控件,就可以实现更多的功能。
主要的代码:
1、配置文件

Web.Config
1
<connectionStrings>
2
      <add name="profileProvider" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
3
</connectionStrings>


1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
11
using System.Collections;
12
13
/**//// <summary>
14
/// WebSite 的摘要说明
15
/// </summary>
16
[Serializable]
17
public class WebSite
18

{
19
    public Hashtable _WebSiteItems = new Hashtable();
20
21
    public ICollection WebSiteItems
22
    
{
23
        get 
{ return _WebSiteItems.Values; }
24
    }
25
    public void AddItem(int ID, string Name, string  Url)
26
    
{
27
        //在向哈西表中添加内容前,先判断该商品是否已经添加上了,如果没有则添加,否则不变。
28
        WebSiteItem item = (WebSiteItem)_WebSiteItems[ID];
29
        if (item == null)
30
        
{
31
            _WebSiteItems.Add(ID, new WebSiteItem(ID, Name, Url));
32
        }
33
        else
34
        
{
35
36
            _WebSiteItems[ID] = item;
37
        }
38
    }
39
40
    public void RemoveItem(int ID)
41
    
{
42
        WebSiteItem item = (WebSiteItem)_WebSiteItems[ID];
43
        if (item == null)
44
        
{
45
            return;
46
        }        
47
        else
48
        
{
49
            _WebSiteItems.Remove(ID);
50
        }
51
       
52
    }
53
    
54
}
55
56
[Serializable]
57
public class WebSiteItem
58

{
59
    private string webSiteName;
60
    private string webSiteUrl;
61
    private int id;
62
63
    public int ID
64
    
{
65
        get
66
        
{
67
            return id;
68
        }
69
    }
70
71
    public string WebSiteName
72
    
{
73
        get
74
        
{
75
            return webSiteName;
76
        }
77
    }
78
    public string WebSiteUrl
79
    
{
80
        get
81
        
{
82
            return webSiteUrl;
83
        }
84
    }
85
86
    public  WebSiteItem(int id,string name,string url)
87
    
{
88
        webSiteUrl = url;
89
        webSiteName = name;
90
        this.id = id;
91
    }
92
}
93


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


1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
public partial class NavigationControls_WebSite : System.Web.UI.Page
13

{  
14
    
15
    protected void Page_Load(object sender, EventArgs e)
16
    
{
17
   
18
       
19
    }
20
    protected void Button1_Click(object sender, EventArgs e)
21
    
{
22
        if (Profile.websit==null)
23
        
{
24
            Profile.websit = new WebSite();
25
        }
26
        //思路:将用户选择的站点存入一个类中,以便在网站导航控件中使用。
27
28
        //WebSiteFunc myWebSite = new  WebSiteFunc();
29
30
        //int i = 0;
31
32
        foreach (DataListItem dl in this.dlWebSite.Items)
33
        
{
34
            CheckBox cb = (CheckBox)dl.FindControl("chbWebSite");
35
            if (cb.Checked)
36
            
{
37
                HyperLink  hl = (HyperLink )dl.FindControl("hlWebSite");
38
                Label myLabel = (Label)dl.FindControl("webId");
39
                int id = Convert.ToInt32(myLabel.Text);
40
41
                Profile.websit.AddItem(id, hl.Text, hl.NavigateUrl);
42
                               
43
            }
44
        }
45
46
      
47
48
49
    }
50
    protected void Button2_Click(object sender, EventArgs e)
51
    
{
52
       
53
        Response.Redirect("Default.aspx");
54
55
    }
56
}
57
5.Default.aspx


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


1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
public partial class NavigationControls_Default : System.Web.UI.Page
13

{
14
    protected void Page_Load(object sender, EventArgs e)
15
    
{
16
        if (!Page.IsPostBack)
17
        
{
18
            Bind();
19
        }
20
        //if ((Session["web"]) != null)
21
        //{
22
        //    WebSiteFunc myWeb = (WebSiteFunc)Session["web"];
23
24
        //    GridView1.DataSource = myWeb.WebSite;
25
        //    GridView1.DataBind();
26
        //}
27
    }
28
29
    protected void Bind()
30
    
{
31
        if (Profile.websit!= null)
32
        
{
33
            GridView1.DataSource = Profile.websit.WebSiteItems;
34
            GridView1.DataBind();
35
        }
36
    }
37
   
38
    protected void RemoveCartItem(object sender, EventArgs e)
39
    
{
40
        // 获取被选中商品的主键ID
41
        int id = (int)GridView1.SelectedDataKey.Value;
42
43
        //int ID = (int)CartGrid.SelectedDataKey.Value;
44
        // 利用ID,从Profile对象购物车中删除该商品
45
        Profile.websit.RemoveItem(id);
46
        // 显示购物车数据
47
        Bind();
48
    }
49
    
50
}
51
另一个页面Default.aspx用户呈现用户选择的网址,并可以进行删除。简单的页面图形如下所示。
WebSite.aspx 的部分截图

Default.aspx的部分截图

用户可以从WebSite.aspx页中自定义需要的网址链接,点击“确定”按钮后,自动存入aspnet_Profile数据表中,Default.aspx页的内容是从aspnet.Profile数据表读取出来的。这样就实现个性化设置。
该功能的核心思想是将一个自定义的类串行化,并存入到aspnet_Profile数据表中,以便实现个性化,如果将Default.aspx封装成一个WebPart控件,就可以实现更多的功能。
主要的代码:
1、配置文件
1
<connectionStrings>2
      <add name="profileProvider" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>3
</connectionStrings>1
 <profile defaultProvider="SqlProvider">
2
      <providers >
3
        <clear/>
4
        <add name ="SqlProvider" connectionStringName="profileProvider" type="System.Web.Profile.SqlProfileProvider"  applicationName="/" />
5
      </providers>
6
      <properties>
7
         <add name="websit" type="WebSite" serializeAs="Binary"  allowAnonymous="true"/>
8
      </properties>      
9
    </profile>
2、存入数据库的自定义类
 <profile defaultProvider="SqlProvider">2
      <providers >3
        <clear/>4
        <add name ="SqlProvider" connectionStringName="profileProvider" type="System.Web.Profile.SqlProfileProvider"  applicationName="/" />5
      </providers>6
      <properties>7
         <add name="websit" type="WebSite" serializeAs="Binary"  allowAnonymous="true"/>8
      </properties>      9
    </profile>1
using System;2
using System.Data;3
using System.Configuration;4
using System.Web;5
using System.Web.Security;6
using System.Web.UI;7
using System.Web.UI.WebControls;8
using System.Web.UI.WebControls.WebParts;9
using System.Web.UI.HtmlControls;10

11
using System.Collections;12

13

/**//// <summary>14
/// WebSite 的摘要说明15
/// </summary>16
[Serializable]17
public class WebSite18


{19
    public Hashtable _WebSiteItems = new Hashtable();20

21
    public ICollection WebSiteItems22

    
{23

        get 
{ return _WebSiteItems.Values; }24
    }25
    public void AddItem(int ID, string Name, string  Url)26

    
{27
        //在向哈西表中添加内容前,先判断该商品是否已经添加上了,如果没有则添加,否则不变。28
        WebSiteItem item = (WebSiteItem)_WebSiteItems[ID];29
        if (item == null)30

        
{31
            _WebSiteItems.Add(ID, new WebSiteItem(ID, Name, Url));32
        }33
        else34

        
{35

36
            _WebSiteItems[ID] = item;37
        }38
    }39

40
    public void RemoveItem(int ID)41

    
{42
        WebSiteItem item = (WebSiteItem)_WebSiteItems[ID];43
        if (item == null)44

        
{45
            return;46
        }        47
        else48

        
{49
            _WebSiteItems.Remove(ID);50
        }51
       52
    }53
    54
}55

56
[Serializable]57
public class WebSiteItem58


{59
    private string webSiteName;60
    private string webSiteUrl;61
    private int id;62

63
    public int ID64

    
{65
        get66

        
{67
            return id;68
        }69
    }70

71
    public string WebSiteName72

    
{73
        get74

        
{75
            return webSiteName;76
        }77
    }78
    public string WebSiteUrl79

    
{80
        get81

        
{82
            return webSiteUrl;83
        }84
    }85

86
    public  WebSiteItem(int id,string name,string url)87

    
{88
        webSiteUrl = url;89
        webSiteName = name;90
        this.id = id;91
    }92
}93

3、WebSite.aspx
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebSite.aspx.cs" Inherits="NavigationControls_WebSite" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
    <title>无标题页</title>8
</head>9
<body>10
    <form id="form1" runat="server">11
    <div>12
    <asp:DataList ID="dlWebSite" runat="server" DataSourceID="SqlDataSource1">13
    <ItemTemplate>14
    <asp:CheckBox  ID="chbWebSite" runat="server"/>15
    <asp:HyperLink ID="hlWebSite" NavigateUrl='<%#Eval("WebSiteUrl") %>' runat="Server" Text='<%#Eval("WebSiteName")%>'></asp:HyperLink>16
    <asp:Label ID="webId" Text='<%#Eval("Id") %>' runat="server" Visible="false"></asp:Label>17
    </ItemTemplate>18
     </asp:DataList>19
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:xqzyConnectionString %>" SelectCommand="SELECT * FROM [Navigator_WebSite]"></asp:SqlDataSource>20
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />21
        <asp:GridView ID="GridView1" runat="server" ShowHeader="False" AutoGenerateColumns="False" Height="139px" Width="103px">22
            <Columns>23
                <asp:TemplateField>24
                    <ItemTemplate>25
                    <table>26
                    <tr>27
                    <td><a href='<%#Eval("WebSiteUrl") %>'><%#Eval("WebSiteName") %></a>  </td>28
                    </tr>29
                    </table>                        30
                    </ItemTemplate>31
                </asp:TemplateField>32
            </Columns>33
          34
        35
        </asp:GridView>36
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></div>37
    </form>38
    39
   40
</body>41
</html>42

1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
public partial class NavigationControls_WebSite : System.Web.UI.Page13


{  14
    15
    protected void Page_Load(object sender, EventArgs e)16

    
{17
   18
       19
    }20
    protected void Button1_Click(object sender, EventArgs e)21

    
{22
        if (Profile.websit==null)23

        
{24
            Profile.websit = new WebSite();25
        }26
        //思路:将用户选择的站点存入一个类中,以便在网站导航控件中使用。27

28
        //WebSiteFunc myWebSite = new  WebSiteFunc();29

30
        //int i = 0;31

32
        foreach (DataListItem dl in this.dlWebSite.Items)33

        
{34
            CheckBox cb = (CheckBox)dl.FindControl("chbWebSite");35
            if (cb.Checked)36

            
{37
                HyperLink  hl = (HyperLink )dl.FindControl("hlWebSite");38
                Label myLabel = (Label)dl.FindControl("webId");39
                int id = Convert.ToInt32(myLabel.Text);40

41
                Profile.websit.AddItem(id, hl.Text, hl.NavigateUrl);42
                               43
            }44
        }45

46
      47

48

49
    }50
    protected void Button2_Click(object sender, EventArgs e)51

    
{52
       53
        Response.Redirect("Default.aspx");54

55
    }56
}57

1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="NavigationControls_Default" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
    <title>无标题页</title>8
</head>9
<body>10
    <form id="form1" runat="server">11
    <div>12
    <asp:GridView ID="GridView1" runat="server"  DataKeyNames="ID" AutoGenerateColumns="False" OnSelectedIndexChanged="RemoveCartItem">13
        <Columns>14
            <asp:TemplateField>15
                <ItemTemplate>16
                    <asp:HyperLink ID="HyperLink1"  Text='<%#Eval("WebSiteName") %>' NavigateUrl='<%#Eval("WebSiteUrl") %>' runat="server"></asp:HyperLink>17
                </ItemTemplate>18
            </asp:TemplateField>19
            <asp:CommandField ShowSelectButton="True" />20
        </Columns>21
        </asp:GridView>22
         23
    </div>24
        25
    </form>26
</body>27
</html>28

6、Default.aspx.cs
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
public partial class NavigationControls_Default : System.Web.UI.Page13


{14
    protected void Page_Load(object sender, EventArgs e)15

    
{16
        if (!Page.IsPostBack)17

        
{18
            Bind();19
        }20
        //if ((Session["web"]) != null)21
        //{22
        //    WebSiteFunc myWeb = (WebSiteFunc)Session["web"];23

24
        //    GridView1.DataSource = myWeb.WebSite;25
        //    GridView1.DataBind();26
        //}27
    }28

29
    protected void Bind()30

    
{31
        if (Profile.websit!= null)32

        
{33
            GridView1.DataSource = Profile.websit.WebSiteItems;34
            GridView1.DataBind();35
        }36
    }37
   38
    protected void RemoveCartItem(object sender, EventArgs e)39

    
{40
        // 获取被选中商品的主键ID41
        int id = (int)GridView1.SelectedDataKey.Value;42

43
        //int ID = (int)CartGrid.SelectedDataKey.Value;44
        // 利用ID,从Profile对象购物车中删除该商品45
        Profile.websit.RemoveItem(id);46
        // 显示购物车数据47
        Bind();48
    }49
    50
}51

                    
                
                
                
            
        
浙公网安备 33010602011771号