Ajax的简单应用之2

1. 加入引用

2. web.config 文件的配置

<?xml version="1.0"?><configuration>
    
<configSections>
        
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                    
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
                    
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                
</sectionGroup>
            
</sectionGroup>
        
</sectionGroup>
    
</configSections>
    
<system.web>
        
<pages>
            
<controls>
                
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                
<add namespace="AjaxControlToolkit" assembly="AjaxControlToolkit" tagPrefix="ajaxToolkit"/>
            
</controls>
        
</pages>
        
<!--
          Set compilation debug="true" to insert debugging
          symbols into the compiled page. Because this
          affects performance, set this value to true only
          during development.
    
-->
        
<compilation debug="true">
            
<assemblies>
                
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            
                
<add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>
        
</compilation>
        
<httpHandlers>
            
<remove verb="*" path="*.asmx"/>
            
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
            
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
        
</httpHandlers>
        
<httpModules>
            
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        
</httpModules>
    
</system.web>
    
<system.web.extensions>
        
<scripting>
            
<webServices>
                
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
                
<!--
      <jsonSerialization maxJsonLength="500">
        <converters>
          <add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>
        </converters>
      </jsonSerialization>
      
-->
                
<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
                
<!--
        <authenticationService enabled="true" requireSSL = "true|false"/>
      
-->
                
<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
           and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
           writeAccessProperties attributes. 
-->
                
<!--
      <profileService enabled="true"
                      readAccessProperties="propertyname1,propertyname2"
                      writeAccessProperties="propertyname1,propertyname2" />
      
-->
            
</webServices>
            
<!--
      <scriptResourceHandler enableCompression="true" enableCaching="true" />
      
-->
        
</scripting>
    
</system.web.extensions>
    
<system.webServer>
        
<validation validateIntegratedModeConfiguration="false"/>
        
<modules>
            
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        
</modules>
        
<handlers>
            
<remove name="WebServiceHandlerFactory-Integrated"/>
            
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        
</handlers>
    
</system.webServer>
</configuration>
3. web页面文件
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace
="System.Web.UI" TagPrefix="asp" 
%>
<!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 runat="server">
    
<title>无标题页</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" />

    
<script language="javascript" type="text/javascript">
  
function testAjax()
  
{
   
var first=document.getElementById("txtfirst");
   WebForm1.GetNIVNumber(first.value,callback_GetNIVNumber);
  }

  
function callback_GetNIVNumber(res)
  
{
   
var obj=document.getElementById("txtsecond");   
   obj.value
=res.value;    
  }

    
</script>

</head>
<body>
    
<form id="Form1" method="post" runat="server">
            
<div>
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
        
</asp:ScriptManager>
    
    
</div>
        
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            
<ContentTemplate>
                
<ajaxToolkit:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="server"
                    TargetControlID
="Panel2"
                    CollapsedSize
="0"
                    Collapsed
="false"
                    ExpandControlID
="LinkButton1"
                    CollapseControlID
="LinkButton1"
                    AutoCollapse
="false"
                    AutoExpand
="true"
                    ScrollContents
="false"
                    TextLabelID
="LinkButton1"
                    CollapsedText
="显示Panel"
                    ExpandedText
="隐藏Panel"
                    ExpandDirection
="Vertical">
                
</ajaxToolkit:CollapsiblePanelExtender>
                
<asp:Panel ID="Panel1" runat="server" BackColor="#FFC080" Height="17px" Width="288px">
                    
<asp:LinkButton ID="LinkButton1" runat="server" Height="18px" Width="288px">LinkButton</asp:LinkButton></asp:Panel>
                    
                
<asp:Panel ID="Panel2" runat="server" BackColor="#FFE0C0" Height="88px" Width="288px">
                    
<br />
                    显示Panel:展开Panel
<br />
                    
<br />
                    隐藏Panel:隐藏Panel
<br />
                    
&nbsp;
                
</asp:Panel>
                
&nbsp;
            
</ContentTemplate>
        
</asp:UpdatePanel>
         
<br />
                
&nbsp;
                
<br />
                
<input type="button" value="Button" onclick="testAjax();" id="Button1" />
                
<br />
                
<asp:TextBox ID="txtfirst" runat="server"></asp:TextBox><br />
                
<asp:TextBox ID="txtsecond" runat="server"></asp:TextBox>
        
<br />
        
<br />
        
<ajaxToolkit:DragPanelExtender ID="DragPanelExtender1" runat="server"
             TargetControlID
="Panel3"
             DragHandleID
="Panel4"
        
>
        
</ajaxToolkit:DragPanelExtender>
        
        
<asp:Panel ID="Panel3" runat="server" BackColor="yellow" Height="50px" Width="125px">
            
<asp:Panel ID="Panel4" runat="server" BackColor="Blue" Height="2px" Width="125px">
                title
</asp:Panel>
            content 我是可以拖动的。
</asp:Panel>
        
<br />
                
                
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="2" Height="256px">
                    
<ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="编程天地">
                        
<ContentTemplate>
                            fghgfhfgh
                        
</ContentTemplate>
                        
<HeaderTemplate>
                            编程天地
                        
</HeaderTemplate>
                    
</ajaxToolkit:TabPanel>
                    
<ajaxToolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="英文天地">
                        
<ContentTemplate>
                            
<table border="1">
                                
<tr>
                                    
<td>11111111 </td>
                                    
<td>22222222</td>
                                
</tr>
                                
<tr>
                                    
<td>aaaaaaa</td>
                                    
<td>bbbbbbb</td>
                                
</tr>
                            
</table>
                        
</ContentTemplate>
                    
</ajaxToolkit:TabPanel>
                    
<ajaxToolkit:TabPanel ID="TabPanel3" runat="server" HeaderText="电影之家">
                        
<ContentTemplate>
                            
<asp:Image ID="Image1" runat="server" ImageUrl="~/PIC/rose.JPG" />
                        
</ContentTemplate>
                    
</ajaxToolkit:TabPanel>
                
</ajaxToolkit:TabContainer>&nbsp;
    
</form>
</body>
</html>
4. cs文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication7
{
    
public partial class WebForm1 : System.Web.UI.Page
    
{
        
protected void Page_Load(object sender, EventArgs e)
        
{
            
//注册,其中Document.Pages.AjaxTest.AjaxMethod为AjaxMethod.cs方法所在的命名空间
            Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication7.WebForm1));
        }

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        
public static string GetNIVNumber(string str)
        
{
            
string returnVal = str + "返回值";
            
return returnVal;
        }

    }

}

5. 运行的结果
posted @ 2007-12-05 13:38  wj-conquer  阅读(422)  评论(0编辑  收藏  举报