Asp.net利用JQuery彈出層加載數據代碼
2024-07-10 12:38:30
供稿:網友
首先我們新建一個網站,在網站里面新增一般處理程序,命名為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"]);
//連接數據庫
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。輸入如下代碼:
代碼如下:
/*當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({
/*調用一般處理程序進行后臺抓取數據并返回txt變量*/
url: 'ReadData.ashx?i=' + $("#Text1").attr("value"),
/*抓取數據成功之后*/
onSuccess: function(txt) {
$("#load").append("<P>" + txt + "</P>");
$("#load").css("display", "block");
$("#mark").css("display", "block");
},
/*正在抓取*/
onRequest: function() {
$("#load").append("<P>正在加載</P>");
},
/*數據加載失敗*/
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
{