ASP.NET AJAX入門系列:使用ScriptManagerProxy控制項

來源:互聯網
上載者:User

在ASP.NET AJAX中,由於一個ASPX頁面上只能有一個ScriptManager控制項,所以在有主版頁面的情況下,如果需要在Master-Page和Content-Page中需要引入不同的指令碼時,這就需要在Content-page中使用ScriptManagerProxy,而不是ScriptManager,ScriptManager 和 ScriptManagerProxy 是兩個非常相似的控制項。

主要內容

1.ScriptManagerProxy控制項概述

2.簡單樣本

 

一.ScriptManagerProxy控制項概述

在ASP.NET AJAX中,由於一個ASPX頁面上只能有一個ScriptManager控制項,所以在有Master-Page的情況下,如果需要在Master-Page和Content-Page中需要引入不同的指令碼時,就需要在Content-page中使用ScriptManagerProxy,而不是ScriptManager,ScriptManagerProxy和 ScriptManager是兩個非常相似的控制項。簡單定義形式如下:

<asp:ScriptManagerProxy id="ScriptManagerProxy1" runat="server">

    <Services>

                <asp:ServiceReference Path="CalculWebService.asmx" />

     </Services>

</asp:ScriptManagerProxy>

在它下面可以添加的子標籤有:Services,Scripts,AuthenticationService,ProfileService

二.簡單樣本

下面看一個簡單的使用ScriptManagerProxy的例子。

1.首先我們準備兩個WebService,在Master-Page中我們輸入一個字串,而在Content-Page中我們求兩個數的和。

SimpleWebService.asmx

[ScriptService]

public class SimpleWebService : System.Web.Services.WebService {

    public SimpleWebService () {

        //Uncomment the following line if using designed components 

        //InitializeComponent(); 

    }

    [WebMethod]

    public string EchoString(String s)

    {
        return "Hello " + s;
    }

}

2.添加一個Master-Page,在它上面添加一個ScriptManager控制項,並引入WebService SimpleWebService.asmx,並添加相應的HTML元素:

<div>

    <asp:ScriptManager ID="ScriptManager1" runat="server" >

        <Services>

            <asp:ServiceReference Path="SimpleWebService.asmx" />

        </Services>

    </asp:ScriptManager>

    <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

    </asp:contentplaceholder>

    &nbsp;<h3>請輸入名稱:</h3>

    <input id="inputName" type="text" />

    <input id="button" type="button" value="確 定" onclick="return OnbuttonGo_click()" />

</div>

3.添加一個Content-Page,在它上面添加一個ScriptManagerProxy控制項,並引入WebService CalculWebService.asmx,並添加相應的HTML元素:

<div>

    <asp:ScriptManagerProxy id="ScriptManagerProxy1" runat="server">

        <Services>

                    <asp:ServiceReference Path="CalculWebService.asmx" />

         </Services>

    </asp:ScriptManagerProxy>

    <h3>請輸入兩個數:</h3>&nbsp;<input id="inputA" type="text" style="width: 110px" />&nbsp;+&nbsp;

    <input id="inputB" style="width: 110px" type="text" />&nbsp;

    <input id="buttonEqual" type="button" value=" = "  onclick="return OnbuttonEqual_click()"/>

</div>

編寫相應的JS代碼:

<script type="text/javascript" language="JavaScript">

    function OnbuttonEqual_click() 
    {
        requestSimpleService = CalculWebService.Add(

            document.getElementById('inputA').value,       //params

            document.getElementById('inputB').value,       //params

            OnRequestComplete    //Complete event

            );

        return false;
    }

    function OnRequestComplete(result) 

    {
        alert(result);
    }

</script>

4.運行後介面如下:

測試Master-Page中的Web Service:

測試Content-Page中的Web Service:

關於ScriptManagerProxy就介紹到這兒,有個問題就是在我的IDE中為什麼ScriptManagerProxy總是提示為未知元素,但運行起來並不抱錯,結果也可以出來?

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.