现在的位置: 首页 > 综合 > 正文

ASP.NET 用ICallbackEventHandler实现无刷新验证

2012年11月12日 ⁄ 综合 ⁄ 共 2481字 ⁄ 字号 评论关闭

 

C#代码

public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
    
private string result;
    
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
        
        
if (!Page.IsPostBack)
        
{
            
string cbReference = Page.ClientScript.GetCallbackEventReference(this"text""ReceiveServerData""");   //获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器端事件的客户端回调。
            string callbackScript = "function dos(text){" + cbReference + ";}";                                        //注册客户端方法
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "dos", callbackScript, true);








        }


      
    }

    
public void RaiseCallbackEvent(string text)
    
{
        
       
        
if (text[0== 'c')
        
{
            
string checkcode = (string)Session["validateCode"];
            
string j = text.Substring(1);
            checkcode 
= checkcode.ToLower();
            j 
= j.ToLower();
            
if (j == checkcode)
                result 
= "ctrue";
            
else
                result 
= "cfalse";

        }

        
else if (text[0== 'e')
        
{
            
string j = text.Substring(1);
            LinkDataBase myLink 
= new LinkDataBase();
            
if (myLink.CheckEmail(j) == 1)
                result 
= "efalse";
            
else
                result 
= "etrue";

        }

        
else if (text[0== 'u')
        
{
            
string j = text.Substring(1);
            LinkDataBase myLink 
= new LinkDataBase();
            
if (myLink.CheckUser(j) == 1)
                result 
= "ufalse";
            
else
                result 
= "utrue";
        }

    }

    
public string GetCallbackResult()
    
{
        
        
return result;
    }


  
  
}

 

Javascript 加上下面的函数

function ReceiveServerData(text)
{    
   
    
if(text=="cfalse")       
    

        checkValidateCode
="false2";
        SetCheckValidateCode(checkValidateCode);
        
     }

     
else if(text=="ctrue")
     
{

        checkValidateCode
="true";
        SetCheckValidateCode(checkValidateCode);
      }

      
else if(text=="etrue")
      
{
        checkEmail
="true";
        SetCheckEmail(checkEmail);
      }

      
else if(text=="efalse")
      

        checkEmail
="false3";
        SetCheckEmail(checkEmail);
      }

      
else if(text=="ufalse")
      
{
        checkUser
="false4";
        SetCheckUser(checkUser);
      }

      
else if(text=="utrue")
      
{
         checkUser
="true";
         SetCheckUser(checkUser);
      }

      checking
=false;
   
}

 

 

最后在Javascript 中用dos(str)调用 

抱歉!评论已关闭.