您的位置:首页 > 编程语言 > ASP

ASP.NET中使用组件服务(1)

2005-07-02 09:47 316 查看
1.1、使用.NET组件
通过两个步骤来实现:
i、创建业务对象
ii、使用业务对象
1.2、创建业务对象
(1)首先建立一个Class Library 项目文件
项目文件名称为:MathCom。
(2)工程文件和他所包含的文件
此时,在工程文件中的“解决方案资源管理器”中已经加了两个C#类。分别是:AssemblyInfo.cs和Class1.cs。可以把Class1.cs改为更清晰易懂的名字。这里将他改为:Cmath。
(3)添加方法
在“类视图”中右键单击Cmath类,在弹出的快捷菜单中选择“添加”|“添加方法”命令。最后的Cmath.cs代码内容如下:

1 using System;
2 namespace MathCom
3 {
4 public class CMath
5 {
6 public CMth()
7 {
8 }
9 privete bool bTest = false;
10 public long add(long a,long b)
11 {
12 return a+b;
13 }
14 public bool Extra
15 {
16 get
17 {
18 return bTest;
19 }
20 set
21 {
22 bTest = value;
23 }
24 }
25 }
26 } (4)生成组件
编译工程文件即可生成组件,该组件会在工程文件夹的bin\dubug目录里。文件名为:MathCom.dll。
1.3、使用业务对象
(1)新建“ASP.NET Web应用程序”
(2)添加引用
在VS.NET中选择“项目”|“添加引用”,如图所示:



(3)代码实现

1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Web;
7 using System.Web.SessionState;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.HtmlControls;
11 using MathCom;
12
13 namespace TestMathCom
14 {
15 /// <summary>
16 /// WebForm1 的摘要说明。
17 /// </summary>
18 public class WebForm1 : System.Web.UI.Page
19 {
20 private void Page_Load(object sender, System.EventArgs e)
21 {
22 CMath math = new CMath();
23 long lResult = math.add(44,88);
24 Response.Write(lResult.ToString()+"<br>");
25 math.Extra = true;
26 bool bResult = math.Extra;
27 Response.Write(bResult.ToString()+"<br>");
28 math.Extra = false;
29 bResult = math.Extra;
30 Response.Write(bResult.ToString()+"<br>");
31
32 }
33
34 #region Web 窗体设计器生成的代码
35 override protected void OnInit(EventArgs e)
36 {
37 //
38 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
39 //
40 InitializeComponent();
41 base.OnInit(e);
42 }
43
44 /// <summary>
45 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
46 /// 此方法的内容。
47 /// </summary>
48 private void InitializeComponent()
49 {
50 this.Load += new System.EventHandler(this.Page_Load);
51
52 }
53 #endregion
54 }
55 }注:
(1)我的输出结果是错误的,郁闷?????最后一个True应该是False。



(2)在生成组件时,竟然无法直接编译,出现下面的错误:



解决方法如图所示:“更改调试模式”

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: