当前位置: 首页 > 图文教程 > 网络编程 > ASP > 股票报价的WebService之四

ASP
ASP 编程中20个非常有用的例子(一)
ASP 编程中20个非常有用的例子(二)
ASP基础教程:ADO存取数据库时如何分页显示
ASP基础教程:其它的ASP常用组件
ASP基础教程:学习ASP中子程序的应用
ASP基础教程之ASP程序对Cookie的处理
ASP基础教程之实例学习ASP Response 对象
ASP基础教程之ASP AdRotator 组件的使用
ADO初学者教程:ADO 通过GetString()加速脚本
初学者来认识OLEDB和ODBC的区别
ASP常见数学函数 Abs Atn Cos 等详细详解
VBScript新手入门初学教程:VBScript简介
有用的无声递交表单的客户端函数
Windows 2003 安装设置iis
ASP技巧实例:几行代码解决防止表单重复提交
ASP读sql数据时出现乱码问题的解决方法
ASP技巧实例:使用ASP记录在线用户的数量
ASP技巧实例:关于对表单操作的程序
ASP技巧实例:ASP实现最简洁的多重查询的解决方案
ASP实例:利用缓存提高数据显示效率

ASP 中的 股票报价的WebService之四


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 27 ::
收藏到网摘: n/a

  创建 Web应用程序用户

下面创建一个Web应用程序StockConsumer.aspx,它作为这个StockQuote(股票报价) Web服务的第一个用户。

  <%@ Page language="C#" %>
  <%@ Import Namespace="System.Xml" %>
  <%@ Import Namespace="Quotes" %>

  以上引入必要的名称空间。要记住也要引入 Quotes名称空间,它是代理库的名称空间。

  <html>
  <head>
  <script runat=server>
   // Wire up the onClick event for a button
   protected void button1_Click(object sender, EventArgs e)
   {
    file://Create a object of the class DailyStock (the proxy class)
    DailyStock ds = new DailyStock();

    // Call the GetQuote method of the proxy class DailyStock and
    // pass the symbol string from the textbox
    string res = ds.GetQuote(symbol.Text);

    // The returned string has values which are separated
    // by commas.
    // Hence we split the returned string into parts
    char[] splitter = {','} ;
    string[] temp = res.Split(splitter);

    // Check if the string array returned has more than one
    // elements since if there are less than one elements
    // then an exception must have been returned
    if(temp.Length >1)
     {
      // The WebService returns a lot of information about the
      // stock. We only show the relevant portions
      // Set the label to current Index
      curindex.Text = "Current Index :"+temp[1];

      // Set the label to current Date Time
      curdate.Text ="Last Update on"+temp[2]+" at "+temp[3];
     }
    else
     {
      error.Text = "Error :"+res ; file://set the error label
     }
    }
   </script>

以上ASP.NET页面代码中,首先对Web 服务DailyStock进行例示。由于已经生成了代理库,因此Web服务的调用方法与其它任何库的调用方法都相同。调用DailyStock 类的GetQuote()方法后,将返回一个字符串,其中包含了以逗号分隔的列表符号的完整信息。

  我们将限制显示给客户的信息为只显示当前指数和所报告指数的日期/时间。为了将字符串分成若干不同的部分,这里使用了字符串类的Split方法,在出现逗号的地方将字符串分割成部分。并且,将分割开的字符串组成数组之后,再使用相关的数值为Web页面设置不同的标签。

  代码的其余部分

  <body>
  <center>
  <h2>.NET101 Stock Quote Consumer </h2>

  <form runat=server >
   <table border=1 celspacing=1>
    <tr><th>Please enter the symbol below</th></tr>
    <tr><td>
    <asp:textbox id=symbol runat=server />
    <asp:button id=button1 text="Get Quote" onClick="button1_Click" runat=server />
    </td></tr>
    <tr><td><asp:label id=curindex runat=server /></td></tr>
    <tr><td><asp:label id=curdate runat=server /></td></tr>
    <tr><td><asp:label id=error runat=server /></td></tr>
   </table>
  </form>

  </center>
  </body>
  </html>