今天,来为大家讲解一下无刷新页面(也称伪静态页面的操作方法)
原理:
伪静态通俗点说就是假的静态页面,也就是通过各种技术手段,让动态页面的URL看上去和静态页面的一样。
所使用工具:
Microsoft Visual Studio 2010
SQL Server Management Studio
先建立一个数据库Table_4,方便我们一会的操作。
Table_4:
然后继续我们的三层建立,项目结构图如下:
如果不会建立三层的同学可以去看我前面的文章
https://my.oschina.net/u/3913001/blog/1858562
然后就开始对各层实施代码了。
上代码~
Model层(user):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class user
{
public string id { set; get; }
public string username { set; get; }
public string userpwd { set; get; }
public string realname { set; get; }
}
}
DAL层(dal):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class dal
{
public List<Model.user> EModel(string where)
{
DataSet ds = DB.ToGetData(" select * from Table_4" + where);
if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
Model.user Myuserlist = null;
List<Model.user> MyList = new List<Model.user>();
foreach (DataRow item in ds.Tables[0].Rows)
{
Myuserlist = new Model.user();
Myuserlist.id = item["id"].ToString();
Myuserlist.username = item["username"].ToString();
Myuserlist.userpwd = item["userpwd"].ToString();
Myuserlist.realname = item["realname"].ToString();
MyList.Add(Myuserlist);
}
return MyList;
}
else
{
return null;
}
}
public class DB
{
static string ConnStr = "Data Source=.;Initial Catalog=调用的数据库名;Persist Security Info=True;User ID=数据库用户名;Password=数据库密码";
public static DataSet ToGetData(string Sql)
{
using (SqlConnection Conn = new SqlConnection(ConnStr))
{
using (SqlDataAdapter da = new SqlDataAdapter(Sql, Conn))
{
DataSet ds = new DataSet();
Conn.Open();
da.Fill(ds);
da.Dispose();
return ds;
}
}
}
}
}
}
UI层(前端):
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="fengye.aspx.cs" Inherits="UI.fengye" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
请输入用户ID:<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1"
runat="server" Text="查¨¦询¡¥" onclick="Button1_Click" /><br />
<asp:DataList ID="DataList1" runat="server"
>
<HeaderTemplate><table>
<tr>
<td>用户ID</td>
<td>用户名字</td>
<td>用户密码</td>
<td>真实姓名</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# Eval("id") %></td>
<td> <%# Eval("username")%></td>
<td> <%# Eval("userpwd")%></td>
<td> <%# Eval("realname")%></td>
</tr>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
UI层(后端):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UI
{
public partial class fengye : System.Web.UI.Page
{
BLL.bll Ebll = new BLL.bll();
protected void Page_Load(object sender, EventArgs e)
{
}
public void Button1_Click(object sender, EventArgs e)
{
string Byid = TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(Byid))
{
DataList1.DataSource = Ebll.MyModel(Byid);
DataList1.DataBind();
}
}
}
}
当完成以后执行,会发现查询改变的时候,页面没有刷新,是不是很神奇?