AjaxPro的使用方法

AjaxPro的使用方法

简单介绍下它的用法:

一.AjaxPro的使用

1.在项目中添加引用,浏览找到AjaxPro.2.dll文件

2.在Web.config中的system.web里面写入以下代码

</configuration>

<system.web>

<httpHandlers>

<add verb="*"path="*.ashx"type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>

</httpHandlers>

</system.web>

<//configuration>

3.在加载事件中,加入

AjaxPro.Utility.RegisterTypeForAjax(typeof(类名));

4.写的方法都要用

[AjaxPro.AjaxMethod]开头,然后在写方法

5.调用时必须写清楚

命名空间名.类名.方法,例:WebUI._Default.getData();

6.调用可分两中方法(同步调用,异步调用)

//在后台写的无参方法

[AjaxPro.AjaxMethod]

public string getStr()

{

return "hello my friends";

}

//在后台写的有参方法

[AjaxPro.AjaxMethod]

public string getString(string str)

{

return str + "Say: hello myfriends";

}

a.同步调用

(1).拖入html控件button

(2).双击,自动显示在.aspx的脚本中

(3).在里面写入你要输入的内容

例:

//------------------同步调用无参-----------

function Button1_onclick()

{

var res=WebUI._Default.getStr();

alert(res.value);

}

//------------------同步调用有参------------

function Button2_onclick() //TextBox1为服务器控件

{

varstr=document.getElementById("<%=TextBox1.ClientID%>").value;

var res=WebUI._Default.getStr(str);

alert(res.value);

}

b.异步调用

(1).拖入html控件button

(2).双击,自动显示在.aspx的脚本中

(3).在里面写入你要输入的内容

例:

//-----------------异步调用无参-----------------

function Button3_onclick() {

WebUI._Default.getStr(getStrCallBack);

}

function getStrCallBack(res)

{

alert(res.value);

}

//-----------------异步调用有参-----------------

function Button4_onclick() {

varstr=document.getElementById("<%=TextBox1.ClientID %>").value;

WebUI._Default.getString(str,getStringCallBack);

}

function getStringCallBack(res)

{

alert(res.value);

}

7.调用对象

//对象

[AjaxPro.AjaxMethod]

public Class getClass()

{

Class cla = new Class();

cla.C_Id = 100;

cla.C_Name = "34班";

cla.Count = 20;

return cla;

}

//------------------同步调用对象-----------

function Button5_onclick() {

var res=WebUI._Default.getClass().value;

alert("班级编号:"+res.C_Id+"名称:"+res.C_Name+"人数:"+res.Count);

}

//------------------异步调用对象-----------

function Button6_onclick() {

WebUI._Default.getClass(getClassCallBack);

}

function getClassCallBack(clas)

{

var res=clas.value;

alert("班级编号:"+res.C_Id+"名称:"+res.C_Name+"人数:"+res.Count);

}

8.数据集的使用

//方法

[AjaxPro.AjaxMethod]

public DataSet getInfo()

{

return WebUI.GetDataSet.getList();

}

//--------------------异步调用数据集--------------

function Button8_onclick() {

WebUI._Default.getInfo(getDataSetCallBack);

}

function getDataSetCallBack(res)

{

var dataset=res.value;

var strHtml="";

strHtml +='<table style="border-collapse:collapse ; border-color:Gray ;"border="1px">';

strHtml +=' <tr>';

strHtml +=' <td>学生编号</td>';

strHtml +=' <td>名称</td>';

strHtml +=' <td>年龄</td>';

strHtml +=' </tr>';

for(vari=0;i<dataset.Tables[0].Rows.length;i++)

{

strHtml +=' <tr>';

strHtml +=' <td>'+dataset.Tables[0].Rows[i].stu_id +'</td>';

strHtml +=' <td>'+dataset.Tables[0].Rows[i].stu_name +'</td>';

strHtml +=' <td>'+dataset.Tables[0].Rows[i].stu_age +'</td>';

strHtml +=' </tr>';

}

strHtml +=' </table>';

thedata.innerHTML=strHtml;//thedata是一个<divid="thedata"></div>中的thedata

}

9.验证码的使用

//----------------------验证码的使用(必须采用同步调用)----------------------

//验证码的使用

[AjaxPro.AjaxMethod]

public bool ValidCodeData(string code)

{

return(HttpContext.Current.Session["CheckCode"].ToString()==code);

}

function Button9_onclick() {

varcode=document.getElementById("<%=TextBox2.ClientID %>").value;

varbool=WebUI._Default.ValidCodeData(code).value;

if(bool==true)

{

alert("ok");

}else

{

alert("no");

}

}

AjaxPro.dll文件网上很多的,自己下,如果找不到呢,给我发个留言,我发你邮箱

二,直接调用:

javascript中:<%=后台方法%>

function says()

{

alert("<%=Say()%>");

}

function del()

{

alert("<%=DeleteByID(8)%>");//DeleteByID(8)后台方法名

}

三,采用ICallbackEventHandler回调

//必须声明System.Web.UI.ICallbackEventHandler接口

public partial class _Default :System.Web.UI.Page, System.Web.UI.ICallbackEventHandler

{

//定义一个回调的返回值

private string Result;

//定义两个变量,用来接收页面传过来到操作数

private string Num1;

private string Num2;

protected void Page_Load(object sender,EventArgs e)

{

}

/// <summary>

/// 该方法是回调执行的方法,根据参数在这个方法中处理回调的内容,该方法没有返回值

/// </summary>

/// <paramname="eventArgument">此参数是从客户端传过来的</param>

public void RaiseCallbackEvent(stringeventArgument)

{

//eventArgumeng 为javascript从客户端传递的参数,本例传过来三个参数用“/”分割将每个参数取出存入数组

string[] PagParams =eventArgument.Split('/');

Num1 = PagParams[1];

Num2 = PagParams[2];

//根据第一个参数(所选的操作符),调用不同的执行函数

switch (PagParams[0])

{

case "0":

Result = add(); break;

case "1":

Result = sub(); break;

case "2":

Result = multi(); break;

case "3":

Result = division(); break;

}

}

/// <summary>

/// 该方法是返回回调的结果给客户端

/// </summary>

/// <returns></returns>

public string GetCallbackResult()

{

return Result;

}

//一下四个函数是通过RaiseCallbackEvent方法,调用的回调要执行操作的函数

private string add()

{

double addResult = double.Parse(Num1) +double.Parse(Num2);

return addResult.ToString();

}

private string sub()

{

double addResult = double.Parse(Num1) -double.Parse(Num2);

return addResult.ToString();

}

private string multi()

{

double addResult = double.Parse(Num1) *double.Parse(Num2);

return addResult.ToString();

}

private string division()

{

double addresult = double.Parse(Num1) /double.Parse(Num2);

return addresult.ToString();

}

}

 

 

 

 

 

 

 

VS2005中AJAX使用方法

//声明一个AJAX命名空间Ajax_showPL,在每个类定义之前加上此句。(注意:Web2.0不支持命名空间,他把所有的类都放在自己生成的App_Code文件夹中)

[AjaxPro.AjaxNamespace("Ajax_showPL")]

// HT_ShowPL_QT是一个类,在这个类中注册AJAX远程处理方法,把这句代码加在页面加载的事件中。这里用到了反射(反射是.NET中获取运行时类型信息的方式,让程序员可以在程序运行期获得程序集,模块,类的相关信息)

AjaxPro.Utility.RegisterTypeForAjax(typeof(HT_ShowPL_QT));

//声明AJAX方法,在每个AJAX方法前面加上此句代码。

[AjaxPro.AjaxMethod]

//同时,要在Web.config配置文件中做相应的修改。

<?xmlversion="1.0"encoding="gb2312" ?>

<!--

    注意: 除了手动编辑此文件以外,您还可以使用

    Web 管理工具来配置应用程序的设置。可以使用Visual Studio 中的

     “网站”->Asp.Net 配置”选项。

    设置和注释的完整列表在

    machine.config.comments 中,该文件通常位于

   \Windows\Microsoft.Net\Framework\v2.x\Config

-->

<configuration>

 

     <!--

  AJAX配置begin===================

  -->

     <configSections>

         <sectionGroupname="ajaxNet">

              <!--

                   If you are using Microsoft .NET 1.1 please removethe two attributes

                   requirePermission and restartOnExternalChanges,they are only supported

                   with .NET 2.0.

              -->

              <sectionname="ajaxSettings"type="AjaxPro.AjaxSettingsSectionHandler,AjaxPro.2"requirePermission="false"restartOnExternalChanges="true"/>

         </sectionGroup>

     </configSections>

     <ajaxNet>

         <ajaxSettings>

              <urlNamespaceMappingsuseAssemblyQualifiedName="false">

                   <!--

                       Set the attribute useAssemblyQualifiedName totrue to enable

                        use ofassemblies placed in the GAC by using the full assembly

                       qualified name.

                  

                       To hide internal knowledge of assemblies,classes and namespace

                       you can override the name of the virtual httpendpoints.

                      

                       <add type="Namespace.Class1,Assembly"path="mypath" />

                   -->

              </urlNamespaceMappings>

              <jsonConverters>

                   <!--

                       This section can be used to add newIJavaScriptConverters to the

                       Ajax.NET Professional engine. If you want todisable built-in

                       converters you can use the remove tag.

                  

                       <removetype="Namespace.Class1,Assembly"/>

                       <addtype="Namespace.Class2,Assembly"/>

                   -->

              </jsonConverters>

              <!--

                   Set the enabled attribute to true to get Stack,TargetSize and Source

                   information if an exception has been thrown.

              -->

              <debugenabled="true"/>

              <!--

                   This is the default configuration used withAjax.NET Professional. You

                   can put there your static JavaScript files, orremove the path attribute

                   to completly disable the files.

             

                   <scriptReplacements>

                       <file name="core"path="~/ajaxpro/core.ashx" />

                       <file name="prototype"path="~/ajaxpro/prototype.ashx" />

                       <file name="converter"path="~/ajaxpro/converter.ashx" />

                   </scriptReplacements>

              -->

              <!-- <encryption cryptType=""keyType="" /> -->

              <!--

                   Set the enabled attribute to true to enable theuse of an Ajax.NET Professional

                   token. This will send a token to the client thatwill be used to identify if the

                   requests comes from the same PC.

              -->

              <tokenenabled="false"sitePassword="password"/>

              <!--

                   The oldStyle section can be used to enable oldstyled JavaScript code or

                   functions that are not used any more.

             

                   <oldStyle>

                       <objectExtendPrototype/>

                       <appCodeQualifiedFullName/>

                   </oldStyle>

              -->

         </ajaxSettings>

     </ajaxNet>

 

     <!-- Handler configuration for Ajax.NET Professional -->

     <locationpath="ajaxpro">

         <system.web>

              <httpHandlers>

                   <addverb="*"path="*.ashx"type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>

              </httpHandlers>

              <!--

                   If you need to have Ajax.NET Professional methodsrunning on the

                   login page you may have to enable your ownauthorization configuration

                   here.

              -->

              <!--

              <authorization>

                   <denyusers="?"/>

              </authorization>

              -->

         </system.web>

     </location>

 

     <!--

         If you are using Ajax.NET Professional with formsauthentication you may need

         to allow ASP.NET to have access to following three files.

     -->

     <!--

     <location path="ajaxpro/prototype.ashx">

         <system.web>

              <authorization>

                   <allowusers="*"/>

              </authorization>

         </system.web>

     </location>

 

     <location path="ajaxpro/core.ashx">

         <system.web>

              <authorization>

                   <allowusers="*"/>

              </authorization>

         </system.web>

     </location>

 

     <location path="ajaxpro/converter.ashx">

         <system.web>

              <authorization>

                   <allowusers="*"/>

              </authorization>

         </system.web>

     </location>

     -->

     <!--

  AJAX配置over===================

  -->

 

  <appSettings>

     <addkey="ConnectString"value="server=100ZZ-1\SQLEXPRESS;database=my_wxfl;userid=bbbb;password=bbbb"/>

    <addkey="open_Log"value="1"/> 

    <addkey="local_url"value="F:\ceshiB\upfile\"/>

  </appSettings>

     <connectionStrings/>

     <system.web>

         <!--

            设置compilationdebug="true" 将调试符号插入

            已编译的页面中。但由于这会

            影响性能,因此只在开发过程中将此值

            设置为true

        -->

         <compilationdebug="true">

              <assemblies>

                   <addassembly="System.Windows.Forms,Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

                   <addassembly="System.Design,Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

                   <addassembly="System.Management,Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

              </assemblies>

         </compilation>

 

         <!--

            通过<authentication> 节可以配置ASP.NET 使用的

            安全身份验证模式,

            以标识传入的用户。

        -->

    <identityimpersonate="true"/> <!--加用户权限-->

         <authenticationmode="Windows"/>

         <!--

            如果在执行请求的过程中出现未处理的错误,

            则通过<customErrors> 节可以配置相应的处理步骤。具体说来,

            开发人员通过该节可以配置

            要显示的html 错误页

            以代替错误堆栈跟踪。

 

        <customErrorsmode="RemoteOnly"defaultRedirect="GenericErrorPage.htm">

            <errorstatusCode="403" redirect="NoAccess.htm" />

            <errorstatusCode="404" redirect="FileNotFound.htm" />

        </customErrors>

        -->

         <globalizationrequestEncoding="gb2312"responseEncoding="gb2312" />

         <roleManagerenabled="true"/>

    <!--

        上传最大限制容量6M

    <httpRuntimeexecutionTimeout="9000"

             maxRequestLength="709600"

            useFullyQualifiedRedirectUrl="false"

             minFreeThreads="8"

            minLocalRequestFreeThreads="4"

            appRequestQueueLimit="100"

            enableVersionHeader="true"/>

            

             -->

     </system.web 

     <!--=========== AJAX配置============ -->

</configuration>

 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

trassion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值