JQuery Form AjaxSubmit(options)在Asp.net中的应用注意事项

所需引用的JS:

在http://www.malsup.com/jquery/form/#download 下载:http://malsup.github.com/jquery.form.js

在http://jquery.com/ 下载:http://code.jquery.com/jquery-1.7.2.min.js

注意事项:

//dataType: "json",       //get的方式再设置此属性

//注意:from 如果是 runat="server" 那option的url只能是提交给自己的.aspx,如果不是则可以提交给其他.aspx接收。
//注意:from中的<input 标签 必须带有name属性,否则只有id Request.Form[] 会获得不到后增加的标签。
//不先Clear的话会返回整个页面的html文件内容,也不要用Response.Write();应该是:HttpContext.Current.Response.Write,注意
HttpContext.Current.Response.ContentType = "text/html";

HttpContext.Current.Response.Clear();


示例代码:

ExpandedBlockStart.gif aspx页
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " JqueryFormAjaxSubmit.aspx.cs "  Inherits = " GaryTestPro.JqueryFormAjaxSubmit "   %>

<! 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 >
     < script  type ="text/javascript"  src ="JS/JQuery/jquery-1.7.1.min.js" ></ script > 
     < script  type ="text/javascript"  src ="JS/JQuery/jquery.form.js" ></ script > 
     < script  type ="text/jscript" language ="jscript" >
        
function  SubInfo() {
        
            $(
" #Order " ).append( " <input name=txtDns value=\"DNS解析\" id=txtDns type=\"text\" /> " );
            $(
" #Order " ).append( " <input name=txtIP value=\"IP地址\" id=txtIP type=\"text\" /> " );
            
            
var  options  =  {
                beforeSubmit: 
function () {
                    
return   true ;
                },
                url: 
' JqueryFormAjaxSubmit.aspx?Mode=SF ' ,
                type: 
' POST ' ,
                
// dataType: "json",       //get的方式再设置此属性
                success:  function (data) {
                    
if  (data  ==   "" ) {
                        document.getElementById(
' Order ' ).style.display  =   " none " ;
                        alert(
" 操作成功! " );
                    }
                    
else  {
                        alert(data);
                    }
                },
                error: 
function () {
                    
// 请求出错处理
                    alert( " error " );
                }
            };
            
// 注意:from 如果是 runat="server" 那option的url只能是提交给自己的.aspx,如果不是则可以提交给其他.aspx接收。
             // 注意:from中的<input 标签 必须带有name属性,否则只有id Request.Form[] 会获得不到后增加的标签。
            $( " #form1 " ).ajaxSubmit(options);
        }
    
</ script > 
</ head >
< body >
     < form  id ="form1"  runat ="server"  method ="post" >
     < div >
         < div  id ="Order" >
            
         </ div >
         < input  name ="txtName"  value ="星期一"  id ="txtName"  type ="text"   />
         < input  name ="txtUser"  value ="星期二"  id ="txtUser"  type ="text"   />
         < href ="javascript:void(0);"  onclick ="SubInfo();" >提交 </ a >
     </ div >
     </ form >
</ body >
</ html >

 aspx.cs 代码:

protected  void Page_Load( object sender, EventArgs e)
        {
             if (Request.Form[ " txtName "] !=  null)
            {
                 string sName = Request.Form[ " txtName "].ToString();
            }
             if (Request.Form[ " txtDns "] !=  null)
            {
                 string sDns = Request.Form[ " txtDns "].ToString();
                 // 不先Clear的话会返回整个页面的html文件内容
                HttpContext.Current.Response.Clear();

                HttpContext.Current.Response.ContentType =  " text/html ";

                HttpContext.Current.Response.Write( " {result:true} ");

                HttpContext.Current.Response.End();
            }
            
        }

转载于:https://www.cnblogs.com/hanguoji/archive/2012/03/23/2413854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值