标签:style blog http color io os 使用 ar for
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Webservice调用实例</title> </head> <body> <form id="form2" runat="server"> <div> <asp:TextBox ID="Num1" runat="server"></asp:TextBox> <select id="selectOper" runat = "server"> <option>+</option> <option>-</option> <option>*</option> <option>/</option> </select> <asp:TextBox ID="Num2" runat="server"></asp:TextBox> <span id = E runat = "server"></span> <asp:TextBox ID="Result" runat="server"></asp:TextBox> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice Button btn = new Button(); btn.Width = 20; btn.Text = " = "; btn.Click += new EventHandler(btn_Click); E.Controls.Add(btn); } /// <summary> /// 定义动态创建Button的Click事件,在这个事件中调用Webservice /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void btn_Click(object sender, EventArgs e) { if (Num1.Text != "" && Num2.Text != "") { //实例化引用的webservice对象 localhost.Service1 WebserviceInstance = new localhost.Service1(); int Oper = selectOper.SelectedIndex; switch (Oper) { //通过实例化的webservice对象来调用Webservice暴露的方法 case 0: Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; case 1: Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; case 2: Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; case 3: Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); break; } } } } }
整个计算并不是在本地进行的,是在Web服务端进行计算的然后将结果通过XML返还给了调用方的,所以,在运行该程序的时候,WebService程序还必须启动,否则会报无法连接远程服务器的异常
实战基础技能(18)--------用ASP.NET调用Web Service
标签:style blog http color io os 使用 ar for
原文地址:http://www.cnblogs.com/hongmaju/p/4024053.html