Asp.net利用JQuery彈出層加載數(shù)據(jù)代碼
更新時(shí)間:2009年11月02日 14:04:18 作者:
最近看QQ空間里面的投票功能很使用。點(diǎn)擊一個(gè)鏈接就彈出一個(gè)層,然后再加載一些投票信息,旁邊的區(qū)域變成灰色不可用狀態(tài)。其實(shí)這不算什么高深的技術(shù),只要在ASP.NET中利用JQuery結(jié)合一般處理程序ASHX即可搞定了。
首先我們新建一個(gè)網(wǎng)站,在網(wǎng)站里面新增一般處理程序,命名為ReadData.ashx。然后在里面輸入如下代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient; //引入命名空間
using System.Data;
namespace 加載層
{
/// <summary>
/// $codebehindclassname$ 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ReadData : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
//獲取外部傳進(jìn)來的變量值i
int i = Int32.Parse(context.Request.QueryString["i"]);
//連接數(shù)據(jù)庫(kù)
SqlConnection con = new SqlConnection("data source=.;user id=sa;pwd=5239898;database=librarydatabase;");
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from reader where 序號(hào)='" + i + "'", con);
SqlCommandBuilder com = new SqlCommandBuilder(ada);
DataSet ds = new DataSet();
ada.Fill(ds, "reader");
con.Close();
//讀取表中欄位為“姓名”的字段值,并傳出
context.Response.Write(ds.Tables["reader"].Rows[0]["姓名"].ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
然后我們需要新建一個(gè)JavaScript文件,命名為loaddata.js。輸入如下代碼:
/*當(dāng)DOM加載完畢之后就自動(dòng)為兩個(gè)鏈接添加Click事件*/
$("document").ready(function() {
$("a[href=javascript]").click(function() {
chkform();
return false;
})
$("a[href=test]").click(function() {
$("#load").css("display", "none");
$("#mark").css("display", "none");
return false;
})
});
var chkform = function() {
new Request({
/*調(diào)用一般處理程序進(jìn)行后臺(tái)抓取數(shù)據(jù)并返回txt變量*/
url: 'ReadData.ashx?i=' + $("#Text1").attr("value"),
/*抓取數(shù)據(jù)成功之后*/
onSuccess: function(txt) {
$("#load").append("<P>" + txt + "</P>");
$("#load").css("display", "block");
$("#mark").css("display", "block");
},
/*正在抓取*/
onRequest: function() {
$("#load").append("<P>正在加載</P>");
},
/*數(shù)據(jù)加載失敗*/
onFailure: function() {
alert('信息加載失??!');
}
}).send();
}
還沒完,我們還要新增一個(gè)CSS文件,命名為main.css,深入如下樣式:
#mark{
width: 100%;
background-color:White;
position:absolute;
left: 0px;
top: 0px;
height: 700px;
filter:alpha(opacity=70);
-moz-opacity:70;
opacity:70;
z-index:2;
display:none;
}
#load
{
background-color:Gray;
border: 1px solid #999;
font-size: 12px;
position:relative;
margin:0 auto;
margin-top:100px;
width:200px;
height:150px;
z-index:99;
display:none;
}
我們?cè)诰W(wǎng)站的首頁(yè)里面源碼輸入如下代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="加載層._Default" %>
<!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>
<link type="text/css" href="main.css" rel="Stylesheet" />
<script type="text/javascript" src="mootools.js"></script> //記得引入這個(gè)javascript庫(kù)文件
<script type="text/javascript" src="jquery-1.3.1-vsdoc.js"></script> //VS支持智能提示的文件,可有可無
<script type="text/javascript" src="jquery-1.3.1.js"></script> //記得引入JQuery
<script type="text/javascript" src="loaddata.js"></script>
</head>
<body>
<div id="me">
<a href="javascript">點(diǎn)擊加載</a>
<input id="Text1" type="text" /></div>
<div id="load">
load <a href="test">試試</a>
</div>
<div id="mark">
</div>
</body>
</html>
至此完成了我們要的效果。
效果圖及DEMO一會(huì)放出。

看上面的鏈接以及文本框都處于“灰色”狀態(tài),不可編輯。點(diǎn)擊中間彈出層的鏈接可以回到最初狀態(tài)。整個(gè)過程中頁(yè)面都沒有刷新!
Demo下載地址: http://xiazai.jb51.net/200911/yuanma/asp.net_jquery_jiazaiceng.rar
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient; //引入命名空間
using System.Data;
namespace 加載層
{
/// <summary>
/// $codebehindclassname$ 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ReadData : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
//獲取外部傳進(jìn)來的變量值i
int i = Int32.Parse(context.Request.QueryString["i"]);
//連接數(shù)據(jù)庫(kù)
SqlConnection con = new SqlConnection("data source=.;user id=sa;pwd=5239898;database=librarydatabase;");
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from reader where 序號(hào)='" + i + "'", con);
SqlCommandBuilder com = new SqlCommandBuilder(ada);
DataSet ds = new DataSet();
ada.Fill(ds, "reader");
con.Close();
//讀取表中欄位為“姓名”的字段值,并傳出
context.Response.Write(ds.Tables["reader"].Rows[0]["姓名"].ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
然后我們需要新建一個(gè)JavaScript文件,命名為loaddata.js。輸入如下代碼:
復(fù)制代碼 代碼如下:
/*當(dāng)DOM加載完畢之后就自動(dòng)為兩個(gè)鏈接添加Click事件*/
$("document").ready(function() {
$("a[href=javascript]").click(function() {
chkform();
return false;
})
$("a[href=test]").click(function() {
$("#load").css("display", "none");
$("#mark").css("display", "none");
return false;
})
});
var chkform = function() {
new Request({
/*調(diào)用一般處理程序進(jìn)行后臺(tái)抓取數(shù)據(jù)并返回txt變量*/
url: 'ReadData.ashx?i=' + $("#Text1").attr("value"),
/*抓取數(shù)據(jù)成功之后*/
onSuccess: function(txt) {
$("#load").append("<P>" + txt + "</P>");
$("#load").css("display", "block");
$("#mark").css("display", "block");
},
/*正在抓取*/
onRequest: function() {
$("#load").append("<P>正在加載</P>");
},
/*數(shù)據(jù)加載失敗*/
onFailure: function() {
alert('信息加載失??!');
}
}).send();
}
還沒完,我們還要新增一個(gè)CSS文件,命名為main.css,深入如下樣式:
復(fù)制代碼 代碼如下:
#mark{
width: 100%;
background-color:White;
position:absolute;
left: 0px;
top: 0px;
height: 700px;
filter:alpha(opacity=70);
-moz-opacity:70;
opacity:70;
z-index:2;
display:none;
}
#load
{
background-color:Gray;
border: 1px solid #999;
font-size: 12px;
position:relative;
margin:0 auto;
margin-top:100px;
width:200px;
height:150px;
z-index:99;
display:none;
}
我們?cè)诰W(wǎng)站的首頁(yè)里面源碼輸入如下代碼:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="加載層._Default" %>
<!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>
<link type="text/css" href="main.css" rel="Stylesheet" />
<script type="text/javascript" src="mootools.js"></script> //記得引入這個(gè)javascript庫(kù)文件
<script type="text/javascript" src="jquery-1.3.1-vsdoc.js"></script> //VS支持智能提示的文件,可有可無
<script type="text/javascript" src="jquery-1.3.1.js"></script> //記得引入JQuery
<script type="text/javascript" src="loaddata.js"></script>
</head>
<body>
<div id="me">
<a href="javascript">點(diǎn)擊加載</a>
<input id="Text1" type="text" /></div>
<div id="load">
load <a href="test">試試</a>
</div>
<div id="mark">
</div>
</body>
</html>
至此完成了我們要的效果。
效果圖及DEMO一會(huì)放出。

看上面的鏈接以及文本框都處于“灰色”狀態(tài),不可編輯。點(diǎn)擊中間彈出層的鏈接可以回到最初狀態(tài)。整個(gè)過程中頁(yè)面都沒有刷新!
Demo下載地址: http://xiazai.jb51.net/200911/yuanma/asp.net_jquery_jiazaiceng.rar
您可能感興趣的文章:
- jQuery Dialog 彈出層對(duì)話框插件
- jQuery彈出層始終垂直居中相對(duì)于屏幕或當(dāng)前窗口
- jquery實(shí)現(xiàn)居中彈出層代碼
- jQuery拖動(dòng)div、移動(dòng)div、彈出層實(shí)現(xiàn)原理及示例
- JQuery彈出層示例可自定義
- jquery 彈出層注冊(cè)頁(yè)面等(asp.net后臺(tái))
- jQuery+html5實(shí)現(xiàn)div彈出層并遮罩背景
- jQuery插件zoom實(shí)現(xiàn)圖片全屏放大彈出層特效
- JQUERY THICKBOX彈出層插件
- jQuery實(shí)現(xiàn)彈出層效果
相關(guān)文章
asp.net 關(guān)于字符串內(nèi)范圍截取的一點(diǎn)方法總結(jié)
前兩天有一位網(wǎng)友提出了一個(gè)字符串內(nèi)截取字符串的問題,除了用普通的字符串截取的方式外,我推薦的是用LINQ方式來截取。兩者實(shí)際上差別不是很大,都是采用字符串截取方式,但后者從寫法和觀察效果會(huì)比前者簡(jiǎn)單實(shí)用得多。2010-02-02asp.net微信開發(fā)(高級(jí)群發(fā)文本)
這篇文章主要介紹了asp.net微信開發(fā)中有關(guān)高級(jí)群發(fā)文本的相關(guān)內(nèi)容,需要的朋友可以參考下2015-11-11asp.net登錄驗(yàn)證碼實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了asp.net登錄驗(yàn)證碼實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08使用ASP.NET中關(guān)于代碼分離的實(shí)例分享
本文主要簡(jiǎn)單介紹了如何讓代碼分離閱讀起來更方便,不至于代碼過于臃腫,這里舉一反三,希望對(duì)大家有所幫助。2016-04-04ASP.NET調(diào)用javascript腳本的常見方法小結(jié)
ASP.NET本身就提供了多種調(diào)用javascript腳本的方法,本文總結(jié)了六種調(diào)用方法,大家根據(jù)自己的使用習(xí)慣可以選擇相應(yīng)的調(diào)用方式了!2009-12-12