//前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>
<!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>
<script src="js/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#txtUserName').blur(function () {
WebApplication1.WebService1.ValidatorUser($('#txtUserName').val(), onSuccess, onFailed);
})
function onSuccess(result) {
$('#spinfo').text(result);
}
function onFailed() {
$('#spinfo').text('调用服务器方法失败');
}
})
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div>
<table>
<tr>
<td>
用户名
</td>
<td>
<input id="txtUserName" type="text" /><span id="spinfo"></span>
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input id="txtPwd" type="text" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
/webservice
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
int pagesize = 20;
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetDateTime()
{
//return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
int numb= Convert.ToInt32(DateTime.Now);
return numb.ToString();
}
[WebMethod]
public string ValidatorUser(string username)
{
if (username == "onlifes")
{
return "用户名已存在";
}
else
{
return "恭喜,可以使用";
}
}
//返回当前在下人数
[WebMethod]
public string GetUserCount()
{
return Application["count"].ToString();
}
[WebMethod]
public int GetCount()
{
string strcon = @"Data Source=YHB-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=yhb@163";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
//cmd.CommandText = "SELECT T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20)+'......' AS NewsContent,T1.CreateTime,T2.ClassName,T3.RealName FROM T_News1 T1 INNER JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId INNER JOIN T_User T3 ON T1.NewsCreator=T3.UserId";
cmd.CommandText = " SELECT COUNT(*) FROM T_News1";
int totalcount = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
conn.Dispose();
int pagelastindex = 0;
if (totalcount % pagesize == 0)
{
pagelastindex = totalcount / pagesize;
}
else
{
pagelastindex = totalcount / pagesize + 1;
}
return pagelastindex;
}
[WebMethod]
public string GetData(string pageindex)
{
string strcon = @"Data Source=YHB-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=yhb@163";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
string sqlstr = "SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY T1.Id DESC)AS rownumber, T1.Id,T1.NewsTitle,SUBSTRING(T1.NewsContent,0,20)+'......' AS NewsContent,T1.CreateTime,T2.ClassName,T3.RealName FROM T_News1 T1 LEFT JOIN T_NewsClass T2 ON T1.ClassId=T2.ClassId LEFT JOIN T_User T3 ON T1.NewsCreator=T3.UserId)A WHERE A.rownumber>(@pageindex-1)*@pagesize AND A.rownumber<=@pageindex*@pagesize";
cmd.CommandText = sqlstr;
cmd.Parameters.AddWithValue("@pageindex", pageindex);
cmd.Parameters.AddWithValue("@pagesize", pagesize);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
//拼接字符串
StringBuilder sb1 = new StringBuilder();
sb1.Append("<table>");
for (int i = 0; i < dt.Rows.Count; i++)
{
sb1.Append("<tr>");
sb1.Append("<td>");
sb1.Append(dt.Rows[i]["Id"].ToString());
sb1.Append("</td>");
sb1.Append("<td>");
sb1.Append(dt.Rows[i]["NewsTitle"].ToString());
sb1.Append("</td>");
sb1.Append("<td>");
sb1.Append(dt.Rows[i]["NewsContent"].ToString());
sb1.Append("</td>");
sb1.Append("<td>");
sb1.Append(dt.Rows[i]["CreateTime"].ToString());
sb1.Append("</td>");
sb1.Append("<td>");
sb1.Append(dt.Rows[i]["ClassName"].ToString());
sb1.Append("</td>");
sb1.Append("<td>");
sb1.Append(dt.Rows[i]["RealName"].ToString());
sb1.Append("</td>");
sb1.Append("</tr>");
}
sb1.Append("</table>");
return sb1.ToString();
}
}
}