.net如何实现10秒倒计时

    技术2025-07-19  12

    我用的是vs2005

    问题是 :现在有个查询手机号码的功能 点击查询的时候先把号码插入到一个表中 同时显示10秒倒计时 等到时间为0的时候就从另一个表中查询数据并显示出来 我之前用js写的只是时间到0的时候再插入数据同时查询数据 可是查询的结果为空 原因是后台的处理程序并没有那么快把插入的号码处理掉

     

     

    页面代码

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!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>    <style type="text/css">    body    {     font-size:small;    }       .bt   {    filter : alpha(opacity=0);    display:none;    }    </style>        <script language="javascript" type="text/javascript">         var t = 10;// 规定为10s         function sarTime() {             var phone = document.getElementById("<%=this.txtPhone.ClientID %>").value;             if (phone == "") {                 alert("手机号码不能为空!");                 return false;             }             else if (phone != "") {                 var re = /^1[3|4|5|8][0-9]/d{4,8}$/;                 if (!re.test(phone)) {                     alert("请输入正确的手机号码!");                     return false;                 } else {                                  if (t > 0) {                                      document.getElementById("<%=this.lblMes.ClientID %>").innerHTML = "正在查询,请稍后...(" + t.toString() + ")";                     t--;                                           if (t == 8) {//执行一次即可  到这步的时候时间就不动了                         document.getElementById("<%=this.Button1.ClientID %>").click();                                             }                                          setTimeout("sarTime()", 1000);                                         } else {                     document.getElementById("<%=this.btnSearch.ClientID %>").click();                     t = 10; //重新赋值计算                 }                  }              }                                 }    </script></head><body>    <form id="form1" runat="server">    <div  style="text-align:left;">

                  <asp:Label ID="Label2" runat="server" Text="音乐盒查询" Font-Bold="True"             Font-Size="Large"></asp:Label>        <br />        <br />               <div>        <asp:Label ID="Label1" runat="server" Text="请输入手机号码:"></asp:Label>        <asp:TextBox ID="txtPhone" runat="server" Width="192px"></asp:TextBox>        &nbsp;        <input id="btnS" type="button" value="查询" οnclick="sarTime()" />        <asp:Button ID="btnSearch" runat="server" Text="" οnclick="btnSearch_Click"  CssClass="bt"  />        <asp:Button ID="Button1" runat="server" Text="" οnclick="Button1_Click"  CssClass="bt"  />            <br />        <asp:Label ID="lblMes" runat="server" ForeColor="Red"></asp:Label>                <asp:GridView ID="gvPhone" runat="server" AutoGenerateColumns="False"                 onrowdatabound="gvPhone_RowDataBound" Width="801px" AllowPaging="True">            <RowStyle HorizontalAlign="Center" />            <Columns>                <asp:BoundField DataField="createtime" HeaderText="查询时间" />                <asp:BoundField DataField="mobile" HeaderText="手机号码" />                <asp:BoundField DataField="tonecode" HeaderText="音乐盒编号" />                <asp:BoundField DataField="name" HeaderText="音乐盒名称" />                <asp:BoundField DataField="price" HeaderText="资费(分)" />            </Columns>            <HeaderStyle BackColor="#9999FF" />        </asp:GridView>            </div>    </form></body></html>

     

    后台代码

     

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;using System.Configuration;

    public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)         {            this.lblMes.Text = "";//清空提示消息

            }    }

        protected void Button1_Click(object sender, EventArgs e)    {         //输入的号码        string phone = this.txtPhone.Text.Trim().ToString();               this.lblMes.Text = "";//清空提示消息        this.txtPhone.Enabled = false;//输入号码框不可用               //先插入到cring_MessageQueue表里        string strSql = string.Format("insert into cring_MessageQueue(message) values ('TYPE=command;AppID=SMS;OP=QueryBox;Mobile={0}')", phone);

            DBHelper.ExecuteNonQuery(strSql); //如果插入数据成功,则执行里面操作            }

        protected void btnSearch_Click(object sender, EventArgs e)    {        try        {            //输入的号码            string phone = this.txtPhone.Text.Trim().ToString();                   this.lblMes.Text = "";//清空提示消息                       //System.Threading.Thread.Sleep(10000);//让程序停止10秒

                //从这张表里去数据并显示            string strSql2 = "select createtime,mobile,tonecode,name,price from cring_RecentSession_BoxInfo where mobile='" + phone + "'";

                DataTable dt = DBHelper.GetDataTable(strSql2);//获取查询的集合

                //绑定数据源            if (dt == null)            {                this.lblMes.Text = "没有您要查询的信息!";                this.txtPhone.Text = "";                this.txtPhone.Enabled = true;            }            this.txtPhone.Enabled = true;//输入号码框可用            this.txtPhone.Text = "";            this.gvPhone.DataSource = dt;            this.gvPhone.DataBind();                }        catch (Exception ex)        {

                throw new Exception("查询出错!" + ex.ToString());        }

        }    protected void gvPhone_RowDataBound(object sender, GridViewRowEventArgs e)    {        if (e.Row.RowType == DataControlRowType.DataRow)         {            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#999999'");            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");        }    }

       }

    最新回复(0)