[WebMethod]
public static int GetUsers()
{
string ss = "Data Source=.;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=123 ";
SqlConnection con = new SqlConnection(ss);
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = "select count(*) from student";
int s = Convert.ToInt32(com.ExecuteScalar());
return s;
}
//后台定义方法,获取数据条数
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string ss = "Data Source=.;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=123 ";
SqlConnection con = new SqlConnection(ss);
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = "select count(*) from student";
int s =Convert.ToInt32(com.ExecuteScalar());
con.Close();
this.TextBox1.Text = s.ToString();
}
}
//获取初始条数
前台代码
/style>
<script src="js/Jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//如果方法获取的条数和文本框初始条数不同,div弹出且重新给文本框赋值(当然也可以不用文本框我只是用了一个暂时储存值的简单方法,可以用其他方法),如果相同则不弹出
$('#a').mouseleave(function () {
document.getElementById("a").style.display = "none";
})
setInterval(on, 2000);
function on() {
$.ajax({
type: "post",
contentType: "application/json",
url: "WebForm2.aspx/GetUsers",
data: "{}",
success: function (result) {
if ($('#TextBox1').val() == result.d) {
}
else {
document.getElementById("a").style.display = "block";
$('#TextBox1').val(result.d) ;
}
}
})
}
})
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="btn" type="button" value="button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<div id="aaa"> </div>
<div class="classa" id="a">您有新的短消息</div>
</form>
</body>
</html>
思路:获取初始条数,ajax每2秒获取一次数据条数,如果不同则弹出div重新给初始条数赋值