asp.net調用WebService

asp.net調用WebService
2008-06-11 10:37
這裏一定要添加WEB 引用菜單步驟如下project->add web reference...,然後輸入我們Web Service的路徑,這裏是http://localhost/WebService1/Service1.asmx,點擊添加就OK了。這時你將在類視圖中看到localhost命名空間了。
WebApplication1
WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
          <title>WebForm1</title>
          <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
          <meta name="CODE_LANGUAGE" Content="C#">
          <meta name="vs_defaultClientScript" content="JavaScript">
          <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
          <form id="Form1" method="post" runat="server">
              <asp:Panel id="Panel1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"
                  Width="568px" Height="256px">
                  <P>Panel
                      <asp:Button id="Button3" runat="server" Text="Button3"></asp:Button>
                      <asp:Button id="Button2" runat="server" Text="Button2"></asp:Button>
                      <asp:Button id="Button1" runat="server" Text="Button1"></asp:Button>
                      <asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
                  <P>
                      <asp:Label id="Label1" runat="server">Label</asp:Label></P>
                  <P>
                      <asp:DataGrid id="DataGrid1" runat="server" Width="568px" Height="120px"></asp:DataGrid></P>
              </asp:Panel>
          </form>
      </body>
</HTML>
====================================================
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
      /// <summary>
      /// WebForm1 的摘要說明。
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
          protected System.Web.UI.WebControls.Panel Panel1;
          protected System.Web.UI.WebControls.Button Button1;
          protected System.Web.UI.WebControls.Button Button2;
          protected System.Web.UI.WebControls.Button Button3;
          protected System.Web.UI.WebControls.TextBox TextBox1;
          protected System.Web.UI.WebControls.DataGrid DataGrid1;
          protected System.Web.UI.WebControls.Label Label1;
    
          private void Page_Load(object sender, System.EventArgs e)
          {
            
              // 在此處放置用戶代碼以初始化頁面
          }
          #region Web 窗體設計器生成的代碼
          override protected void OnInit(EventArgs e)
          {
              //
              // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
              //
              InitializeComponent();
              base.OnInit(e);
          }
        
          /// <summary>
          /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
          /// 此方法的內容。
          /// </summary>
          private void InitializeComponent()
          {
              this.Button1.Click += new System.EventHandler(this.Button1_Click);
              this.Button2.Click += new System.EventHandler(this.Button2_Click);
              this.Button3.Click += new System.EventHandler(this.Button3_Click);
              this.Load += new System.EventHandler(this.Page_Load);
          }
          #endregion
          private void Button2_Click(object sender, System.EventArgs e)
          {
              DataSet ds=new DataSet();
              localhost.WebService1 oService=new localhost.WebService1();
              //返回記錄集
              ds=oService.GetService1Table();
              if (ds!=null)
              {
                  //顯示記錄集的記錄
                  DataGrid1.DataSource=ds.Tables["mfTable1"];
                  DataGrid1.DataBind();
              }
              else
              {
                  this.Response.Write("加載數據錯誤!");
              }
          }
          //測試GetByUser()
          private void Button1_Click(object sender, System.EventArgs e)
          {
              DataSet ds=new DataSet();
              localhost.WebService1 oService=new localhost.WebService1();
              string mfCommand=TextBox1.Text;
              ds=oService.GetByUser(mfCommand);
              if (ds!=null)
              {
                  DataGrid1.DataSource=ds;
                  DataGrid1.DataBind();
              }
              else
              {
                  Response.Write("錯誤!有可能是SQL命令有問題!");
              }
          }
          //測試About()
          private void Button3_Click(object sender, System.EventArgs e)
          {
              localhost.WebService1 oService=new localhost.WebService1();
              Label1.Text=oService.About();
          }
      }
}
====================================================
WebService1
Service1.asmx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
namespace WebService1
{
      /// <summary>
      /// Service1 的摘要說明。
      /// </summary>
      public class Service1 : System.Web.Services.WebService
      {
          public Service1()
          {
              //CODEGEN: 該調用是 ASP.NET Web 服務設計器所必需的
              InitializeComponent();
          }
          #region 組件設計器生成的代碼
        
          //Web 服務設計器所必需的
          private IContainer components = null;
                
          /// <summary>
          /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
          /// 此方法的內容。
          /// </summary>
          private void InitializeComponent()
          {
          }
          /// <summary>
          /// 清理所有正在使用的資源。
          /// </summary>
          protected override void Dispose( bool disposing )
          {
              if(disposing && components != null)
              {
                  components.Dispose();
              }
              base.Dispose(disposing);        
          }
        
          #endregion
          // WEB 服務示例
          // HelloWorld() 示例服務返回字符串 Hello World
          // 若要生成,請取消註釋下列行,然後保存並生成項目
          // 若要測試此 Web 服務,請按 F5 鍵
//          [WebMethod]
//          public string HelloWorld()
//          {
//              return "Hello World";
//          }
//連接字符串常量
          const string mfConn="uid=sa;pwd=mikecat
發佈了6 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章