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)在生成组件时,竟然无法直接编译,出现下面的错误:
解决方法如图所示:“更改调试模式”
通过两个步骤来实现:
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)在生成组件时,竟然无法直接编译,出现下面的错误:
解决方法如图所示:“更改调试模式”
相关文章推荐
- ASP.NET中使用组件服务(2)
- 在ASP.NET AJAX中使用应用程序服务和本地化(1):身份认证组件AuthenticationService
- 在ASP.NET AJAX中使用应用程序服务和本地化(3):用户个性化组件ProfileService
- 【ASP.Net MVC】在AspNet Mvc使用JQuery AutoComplete组件
- asp.net服务组件-自动事务处理
- 使用js 文件参数 以及IHttpModule实现服务验证asp.net 版的初步实现
- ASP.NET(C#)下使用AspJpeg组件
- asp.net中Fine Uploader文件上传组件使用介绍
- 使用MONO移植ASP.NET 编写的Window服务到Linux
- ASP.NET分页组件学与用——使用篇
- 使托管在Asp.net中的WCF服务也使用Form验证
- asp.net使用组件koogra读取Excel数据批量上传导入
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【开篇】【持续更新中。。。】
- 使用HttpClient消费ASP.NET Web API服务
- N-Tiers开发方式(ASP/ASP.NET、VB6/VB.NET呼叫使用COM+组件)
- 如何使用 ASP.NET 应用程序以使用 Visual C#.NET 来查询索引服务目录
- 使用ASP.Net 3.5 的Ajax与Web服务开发实例
- 把Asp.Net Core 2.0部署在Linux上,使用Nginx代理服务器,并且用Systemctl命令以服务的方式监听项目
- 使用 ASP.NET 创建的 XML Web 服务的概述
- ASP.NET:使用Flurl制作可复用的分页组件