不用组件实现Ajax效果.(2)

2016-02-19 16:30 2 1 收藏

生活已是百般艰难,为何不努力一点。下面图老师就给大家分享不用组件实现Ajax效果.(2),希望可以让热爱学习的朋友们体会到设计的小小的乐趣。

【 tulaoshi.com - Web开发 】

     我们写一个检测用户是否已经注册的小程序,这在每个用户注册页面上都可以用到。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

前台Default.aspx:

%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %
!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"
    titleICallbackEventHandler Demo/title
    style type="text/css"
    *{font: 12px "verdana";}
    #user{border:1px solid #080; height:50px;width:500px;padding:20px;}
    input{border:1px solid #508FCC;background:#FFF;}
    .ok{color:#090;}
    .bad{color:#F00}
    #result{display:inline;margin: 0 5px 0;}
    /style
    script type="text/javascript"
    function GetFlag(arg)//这里的javascript函数就是服务器查询完成要调用的函数,我们把服务器返回的数据直接显示上ID为result的div中。
    {
        document.getElementById("result").innerHTML=arg;
    }
    /script
/head
body
    form id="form1" runat="server"
        div id="user"
            input type="text" value="99love" maxlength="10" id="userid" /
            input type="button" value="check it!" onclick="callServer()" /div id="result"查询用户是否被使用。/div
        /div
    /form
/body
/html

上面有一个input type="button" value="check it!" onclick="callServer()" /,这是我说的第5步的内容,这个函数名字callServer应该和Lage_Load中注册的相对应的。

后台Default.aspx.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;
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    private string _arg = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        string script = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetFlag", "");//上面的第3步,取得这个客户端函数名,script的值可能是这样的:WebForm_DoCallback('__Page',arg,GetFlag,"",null,false)
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callServer", "nfunction callServer(){ndocument.getElementById("result").innerHTML="正在检查 "+document.getElementById("userid").value+" 的可用性,请稍候...";nvar arg=document.getElementById("userid").value;n" + script + ";n}", true);
        //上面这一行有点长了,第二个参数最长,看到function callServer()了吗,这就是和前台对应的那个函数名input onclick=XXX,请记得,一定要在callServer函数中加入script的值,也就是上一行的上一行(17行)那个值。注意后面的var arg=...,这个arg和17行的arg是对应的。
    }
    #region ICallbackEventHandler Members
    public string GetCallbackResult()
    {
        System.Threading.Thread.Sleep(1000);//让线程睡一会,我们好看效果。这是用来模拟不良网络状况的。
        return _arg;//返回一个string型,这个string在RaiseCallbackEvent函数中是赋过值的,看下面哦。
    }
    public void RaiseCallbackEvent(string eventArgument)
    {
        if (eventArgument.Equals("99love") || eventArgument.Equals("blueidea"))//假设这两个名字不能注册,实际操作中,你要是比对数据库记录的,我们现在简化处理了。
            _arg = string.Format("span class="bad"很遗憾,{0} 已被使用。/span", eventArgument);//要么已经注册了
        else
            _arg = string.Format("span class="ok"恭喜您,{0} 可以注册。/span", eventArgument);//要么就可以注册
    }
    #endregion
}

运行截图:

  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1612319.html

延伸阅读
标签: Web开发
客户端部分: 代码如下: html head meta http-equiv="Content-Type" content="text/html"/ script language="javascript" var ajax; function createAjax() { if(window.ActiveXObject) { try { return new ActiveXObject("Msxm12.XMLHTTP"); } catch(e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } cat...
标签: Web开发
Adobe™ Flash™ Ajax Video (FAVideo) 组件是一个轻量级的,开源的用 Ajax+Flash 来播放flv的组件。它可以运用在 Ajax 的程序里。它可以使在 Javascript 里控制 Video 像在 Flash 那样容易。 html head   titleVarious ways to work with FAVideo /title   script src="AC_RunActiveContent.js"...
标签: flash教程
2.组件样式属性 组件的样式属性很多,这些属性可以由globalStyleFormat对象调用并设定属性的值。表1中是组件样式属性表。 表1 属性功能一览表 属性的用法 属性的功能 arrow 设置滚动条和下拉菜单中箭头元件的颜色 background 设置列表框、组合框、单选钮和复选框组件...
标签: Web开发
代码如下: document.write('DIV id="loadingg"  style="HEIGHT:65px; WIDTH: 205px;POSITION: absolute; Z-INDEX:1000;border:3px #fff solid;text-align:center; font-size:12px; font-family:Arial, Helvetica, sans-serif;color:#660000;background:#222;opacity:.7;-m...
标签: Web开发
由于查询返回的数据量很大,超过10w条数据,因此需要对页面查询功能进行优化。放弃原有程序中使用DataGrid的做法,自己编写分页显示模块。     首先在页面上添加几个DIV:         div id="div_trackpoint" style=" border:solid 1px gray; height:230px; width:99%; overflow-y...

经验教程

775

收藏

77
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部