先看一下效果图:

上图为运行后,效果图。

上图为设计ASCX文件时的切图。

上图为数据库表中的字段。
如何在VS 2005中创建工程文件就不说了,直接进入主题,在工程中新建一个用户控件;
CompanyProd.ascx 用户控件文件,代码:
1
<%
@ Control Language="C#" AutoEventWireup="true" CodeFile="CompanyProd.ascx.cs" Inherits="CompanyProd"
%>
2
<
TABLE
WIDTH
=550
BORDER
=0
CELLPADDING
=0
CELLSPACING
=0
>
3
<
TR
>
4
<
TD
COLSPAN
=3
>
5
<
IMG
SRC
="images/CompanyProd_01.gif"
WIDTH
=550
HEIGHT
=50
ALT
=""
></
TD
>
6
</
TR
>
7
<
TR
>
8
<
TD
width
="27"
>
9
<
IMG
SRC
="images/CompanyProd_02.gif"
WIDTH
=27
HEIGHT
=201
ALT
=""
></
TD
>
10
<
TD
width
="494"
valign
="top"
background
="images/CompanyProd_03.gif"
>
<
asp:DataList
ID
="DataList1"
runat
="server"
Font-Size
="Small"
ForeColor
="Gray"
>
11
<
ItemTemplate
>
12
<
table
border
="0"
cellpadding
="0"
cellspacing
="1"
bgcolor
="#999999"
>
13
<
tr
>
14
<
td
bgcolor
="#FFFFFF"
><
table
width
="155"
height
="130"
border
="0"
>
15
<
tr
>
16
<
td
height
="51"
><
div
align
="center"
><
img
src
="<%# DataBinder.Eval(Container.DataItem, "
ProdPhoto").ToString()%
>
" width="80" height="80" />
</
div
></
td
>
17
</
tr
>
18
<
tr
>
19
<
td
style
="font-size: 12px; color: dimgray;"
>
产品名称:
<%
# DataBinder.Eval(Container.DataItem, "ProdName").ToString()
%>
<
br
/><
br
/>
20
详细介绍:
<
br
/>
<%
# DataBinder.Eval(Container.DataItem, "ProdDetail").ToString()
%>
21
<
br
/></
td
>
22
</
tr
>
23
</
table
></
td
>
24
</
tr
>
25
</
table
>
26
</
ItemTemplate
>
27
</
asp:DataList
></
TD
>
28
<
TD
width
="29"
>
29
<
IMG
SRC
="images/CompanyProd_04.gif"
WIDTH
=29
HEIGHT
=201
ALT
=""
></
TD
>
30
</
TR
>
31
<
TR
>
32
<
TD
COLSPAN
=3
>
33
<
IMG
SRC
="images/CompanyProd_05.gif"
WIDTH
=550
HEIGHT
=34
ALT
=""
></
TD
>
34
</
TR
>
35
</
TABLE
>
36
37
38



2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19



20



21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

CompanyProd.ascx.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
CompanyProd : System.Web.UI.UserControl
13
{
14
15
private String _ProClass = "";
16
17
public String ProClass
18
{
19
20
get
21
{
22
return _ProClass;
23
}
24
set
25
{
26
this.ShowProd();
27
}
28
}
29
30
31
protected void Page_Load(object sender, EventArgs e)
32
{
33
34
}
35
private void ShowProd()
36
{
37
LixyClass CProd = new LixyClass();
38
this.DataList1.DataSource = CProd.ExecuteSqlRead("select * from Product order by ID desc");
39
this.DataList1.DataBind();
40
41
}
42
}
43

2

3

4

5

6

7

8

9

10

11

12

13



14

15

16

17

18



19

20

21



22

23

24

25



26

27

28

29

30

31

32



33

34

35

36



37

38

39

40

41

42

43

然后创建一个WEB窗体文件;
Default.aspx 代码如下:
1
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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
13
</
div
>
14
</
form
>
15
</
body
>
16
</
html
>
17



2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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
_Default : System.Web.UI.Page
13
{
14
protected void Page_Load(object sender, EventArgs e)
15
{
16
Page.Controls.Add(new HtmlGenericControl("CP"));
17
18
Control c1 = LoadControl("CompanyProd.ascx");
19
((CompanyProd)c1).ProClass = "All";
20
Page.Controls.Add(c1);
21
}
22
23
24
25
26
27
}
28

2

3

4

5

6

7

8

9

10

11

12

13



14

15



16

17

18

19

20

21

22

23

24

25

26

27

28

以上代码文件基本实现了自定义用户控件的实现,以及在代码中创建一个用户控件。
需要说明的是几个关键的代码:
1、将数据库中的数据读取出来,绑定到用户控件:
1
public
partial
class
CompanyProd : System.Web.UI.UserControl
2
{
3
4
private String _ProClass = "";
5
6
public String ProClass
7
{
8
9
get
10
{
11
return _ProClass;
12
}
13
set
14
{
15
this.ShowProd();
16
}
17
}
18
19
20
protected void Page_Load(object sender, EventArgs e)
21
{
22
23
}
24
private void ShowProd()
25
{
26
LixyClass CProd = new LixyClass();
27
this.DataList1.DataSource = CProd.ExecuteSqlRead("select * from Product order by ID desc");
28
this.DataList1.DataBind();
29
30
}
31
}

2



3

4

5

6

7



8

9

10



11

12

13

14



15

16

17

18

19

20

21



22

23

24

25



26

27

28

29

30

31

2、读取数据库的执行代码,我写在了LixyClass类中,其ExecuteSqlRead函数代码如下:
1
public
DataTable ExecuteSqlRead(
string
SqlString)
2
{
3
OleDbCommand SqlCmd = new OleDbCommand();
4
SqlCmd.Connection = new OleDbConnection(ConnStr);
5
SqlCmd.CommandText = SqlString;
6
SqlCmd.CommandType = CommandType.Text;
7
OleDbDataAdapter SqlAd = new OleDbDataAdapter(SqlCmd);
8
DataSet Rs = new DataSet();
9
SqlAd.Fill(Rs);
10
return Rs.Tables[0];
11
}

2



3

4

5

6

7

8

9

10

11

3、在代码中进行创建用户控件在WEB FORM上:
1
protected
void
Page_Load(
object
sender, EventArgs e)
2
{
3
Page.Controls.Add(new HtmlGenericControl("CP"));
4
5
Control c1 = LoadControl("CompanyProd.ascx");
6
((CompanyProd)c1).ProClass = "All";
7
Page.Controls.Add(c1);
8
9
10
}

2



3

4

5

6

7

8

9

10

好了,锡远就写到这里啦,关于自定义用户控件的实现就完成啦!随便在这里说一句,非常感谢湖南.NET俱乐部里面的兄弟姐妹们,因为有你们,所以我的生活更精彩,因为有你们,所以编程的生活不再孤单。 另外,希望陈老大的书快点写完,我好拿出去卖钱钱,然后买个微软的鼠标来,哈哈~~~