Asp.net利用JQuery彈出層加載數(shù)據(jù)代碼
更新時間:2009年11月02日 14:04:18 作者:
最近看QQ空間里面的投票功能很使用。點擊一個鏈接就彈出一個層,然后再加載一些投票信息,旁邊的區(qū)域變成灰色不可用狀態(tài)。其實這不算什么高深的技術(shù),只要在ASP.NET中利用JQuery結(jié)合一般處理程序ASHX即可搞定了。
首先我們新建一個網(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");
//獲取外部傳進來的變量值i
int i = Int32.Parse(context.Request.QueryString["i"]);
//連接數(shù)據(jù)庫
SqlConnection con = new SqlConnection("data source=.;user id=sa;pwd=5239898;database=librarydatabase;");
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from reader where 序號='" + 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;
}
}
}
}
然后我們需要新建一個JavaScript文件,命名為loaddata.js。輸入如下代碼:
/*當(dāng)DOM加載完畢之后就自動為兩個鏈接添加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)用一般處理程序進行后臺抓取數(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();
}
還沒完,我們還要新增一個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;
}
我們在網(wǎng)站的首頁里面源碼輸入如下代碼:
<%@ 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> //記得引入這個javascript庫文件
<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">點擊加載</a>
<input id="Text1" type="text" /></div>
<div id="load">
load <a href="test">試試</a>
</div>
<div id="mark">
</div>
</body>
</html>
至此完成了我們要的效果。
效果圖及DEMO一會放出。

看上面的鏈接以及文本框都處于“灰色”狀態(tài),不可編輯。點擊中間彈出層的鏈接可以回到最初狀態(tài)。整個過程中頁面都沒有刷新!
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");
//獲取外部傳進來的變量值i
int i = Int32.Parse(context.Request.QueryString["i"]);
//連接數(shù)據(jù)庫
SqlConnection con = new SqlConnection("data source=.;user id=sa;pwd=5239898;database=librarydatabase;");
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from reader where 序號='" + 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;
}
}
}
}
然后我們需要新建一個JavaScript文件,命名為loaddata.js。輸入如下代碼:
復(fù)制代碼 代碼如下:
/*當(dāng)DOM加載完畢之后就自動為兩個鏈接添加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)用一般處理程序進行后臺抓取數(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();
}
還沒完,我們還要新增一個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;
}
我們在網(wǎng)站的首頁里面源碼輸入如下代碼:
復(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> //記得引入這個javascript庫文件
<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">點擊加載</a>
<input id="Text1" type="text" /></div>
<div id="load">
load <a href="test">試試</a>
</div>
<div id="mark">
</div>
</body>
</html>
至此完成了我們要的效果。
效果圖及DEMO一會放出。

看上面的鏈接以及文本框都處于“灰色”狀態(tài),不可編輯。點擊中間彈出層的鏈接可以回到最初狀態(tài)。整個過程中頁面都沒有刷新!
Demo下載地址: http://xiazai.jb51.net/200911/yuanma/asp.net_jquery_jiazaiceng.rar
相關(guān)文章
asp.net 關(guān)于字符串內(nèi)范圍截取的一點方法總結(jié)
前兩天有一位網(wǎng)友提出了一個字符串內(nèi)截取字符串的問題,除了用普通的字符串截取的方式外,我推薦的是用LINQ方式來截取。兩者實際上差別不是很大,都是采用字符串截取方式,但后者從寫法和觀察效果會比前者簡單實用得多。2010-02-02ASP.NET調(diào)用javascript腳本的常見方法小結(jié)
ASP.NET本身就提供了多種調(diào)用javascript腳本的方法,本文總結(jié)了六種調(diào)用方法,大家根據(jù)自己的使用習(xí)慣可以選擇相應(yīng)的調(diào)用方式了!2009-12-12