首页 > 开发 > .Net > 正文

Asp.net利用JQuery弹出层加载数据代码

2020-04-24 22:12:01
字体:
来源:转载
供稿:网友
首先我们新建一个网站,在网站里面新增一般处理程序,命名为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
{
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表