最后用AJAX写了个留言版,数据安要求读出来了,也是无刷新的,最就就当我把类别一换,当在index.aspx?type=53带上一个参数后,每隔几十秒到一分钟,他会有一个短暂的错误数据,也是就把TYPE=0的也读出来了,就一秒的样子,马上又自行恢复,效果参考如这个网址里面的,http://hd.jznews.com.cn/index.aspx?type=62。请问是不是Request.QueryString["type"]有时读出的数据会无效呢。要不怎么会出现这种怪现像。<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %><%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %><html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>直播留言</title>
    <style type="text/css">
        /* 全局样式 */
body,ul,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div{margin:0;padding:0;border:0;}
body{background:#fff;color:#333;font-size:12px; margin-top:5px;font-family:"宋体";}
 
ul,ol{list-style-type:none;}
select,input,img,select{vertical-align:middle;}
 
a{text-decoration:underline;}
a:link{color:#009;}
a:visited{color:#800080;}
a:hover,a:active,a:focus{color:#c00;}
.style1{
width: 100%;
height: 30px;
margin: 0px auto;
margin-bottom:-1px;
border:1px solid #96C2F1;
background-color: #EFF7FF;
table-layout:fixed;   overflow:hidden;line-height:30px;
overflow:hidden;}
.style3{
width: 100%;
height: auto;
margin: 0px auto;
margin-bottom:-1px;
border:1px solid #96C2F1;
background-color: #EFF7FF;
table-layout:fixed;  overflow:hidden;line-height:30px;
border : 1px dashed #96C2F1;
word-wrap : break-word ; overflow:auto;
white-space:normal;
}
.style4{
width: 100%;
height:auto;
margin: 0px auto;
margin-bottom:-1px;
border:1px solid #96C2F1;
background-color: #EFF7FF;
table-layout:fixed;  }
.style2{
width: 100%;
height: auto;
margin: 0px auto;
margin-bottom:-1px;
border:1px solid #96C2F1;
background-color: #EFF7FF;
text-align:center}
        #TextArea1
        {
            width: 297px;
            height: 200px;
        }
        #TextArea1
        {
            height: 206px;
            width: 334px;
        }
 
#menu ul {list-style:none;margin:0px;}
#menu ul li {float:left;margin:0 1px}.paginator { font: 10px Arial, Helvetica, sans-serif;padding:9px 12x 9px 0; margin: 0px;}
.paginator a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:1px}
.paginator a:visited {padding: 1px 5px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.paginator .cpb {padding: 1px 5px;font-weight: bold; font-size: 11px;border:none}
.paginator a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}
       
    </style>
</head>
<script type="text/jscript" language="JavaScript">   window.onload=function()   
  {   
  
      document.getElementById('words').onmouseout=function()   
      {   
            
          if(this.value.length>199)
           {  
            
            //document.getElementById('used').value=this.value.length;
             alert("留言不能超过 200 个字!");
             this.value = this.value.substring(0,199);
           } 
      }  
       document.getElementById('words').onkeyup=function()   
      {   
            
          if(this.value.length<=199)   
              event.returnValue=true;   
      } 
  }  
    </script>
   
<body>
    <form id="form1" name="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>  
<div class="style1">
<ul>
<li>
   &nbsp; 互动主题:<asp:DropDownList ID="DropDownList1"  runat="server"  Visible="false"
        Enabled="False">
    </asp:DropDownList>
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
   
</li>
</ul></div>
<div class="style1" id="divid" runat="server" visible="false">&nbsp;  <font color="red">*本主题需管理员审核后方可显示*</font>
    
   
</div><div id="bbb" class="style1">
<ul>
<li>&nbsp;昵&nbsp;&nbsp;称:<asp:TextBox ID="TextBox1" runat="server" 
        MaxLength="8" Width="106px"></asp:TextBox>
    最多8个字符<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="TextBox1" ErrorMessage="昵称不能为空"></asp:RequiredFieldValidator>
 </li>
</ul>
</div>
<div class="style3">&nbsp;内&nbsp;&nbsp;容:<textarea id="words" cols="40" rows="3" runat="server" name="words"  onkeydown="gbcount(this.form1.words,this.form1.total,this.form1.used,this.form1.remain)"; 
   onkeyup="gbcount(this.form1.words,this.form1.total,this.form1.used,this.form1.remain)"; ></textarea>
<asp:RequiredFieldValidator 
        ID="RequiredFieldValidator2" runat="server" ControlToValidate="words" 
        ErrorMessage="内容不能为空" Display="Dynamic"></asp:RequiredFieldValidator></div>
<%--<div class="style2">
<span class="STYLE4">最多字数:</span>
                <INPUT disabled maxLength=4 name=total size=3 value=100>
                <span class="STYLE4">已用字数:</span>
                <INPUT disabled maxLength=4 name=used id="used" size=3 value=0>
                <span class="STYLE4">剩余字数:</span>
              <INPUT disabled maxLength=4 name=remain size=3 value=100>
</div>--%>
<div  id="ccc" class="style1">
<ul>
<li>&nbsp;验证码: <asp:TextBox ID="TextBox2"  runat="server" Width="45px"></asp:TextBox><img id="imgVerify" alt="看不清?点击更换" onclick="this.src=this.src+'?'" src="VerifyCode.aspx" width="50px" height=26 />
    <asp:Label ID="Label1" runat="server" ForeColor="#CC0000" Text="Label" 
        Visible="False"></asp:Label>
        </li>
  </ul>
    </div>
<div class="style2">
    <asp:Button ID="Button1" runat="server" Text="提   交" onclick="Button1_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button2" runat="server" Text="重  置" onclick="Button2_Click" />
</div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
 
       
    <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
       
   </asp:Timer>  <div class="style1">            <webdiyer:AspNetPager ID="AspNetPager1" HorizontalAlign="Center" 
          CenterCurrentPageButton="true" CurrentPageButtonPosition="Center" 
          NumericButtonCount="4"  CssClass="paginator" runat="server" 
          CustomInfoHTML="第<font color='red'><b>%currentPageIndex%</b></font>页,共%PageCount%页"  
          FirstPageText="首页" LastPageText="尾页" PrevPageText="&lt;" NextPageText="&gt;"
          onpagechanged="AspNetPager1_PageChanged" ShowPageIndexBox="Never">
      </webdiyer:AspNetPager>       
</div>
    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <div id="menu" class="style3"   > 
    <ul>    <li>
    &nbsp;
    <font color="green">    <%# Eval("author")%>
    </font>
    :
    </li>
    
     <li>
    
    <%# Eval("Content")%>    <font color="#999999">(<%#Eval("adddate").ToString() %> ) </font>   </li>   
    
         </ul>
    </div>
    </ItemTemplate>
    </asp:Repeater>
 <div class="style1">
 &nbsp;&nbsp;总记录数:<asp:Label   ID="Label3" runat="server" Text="Label"></asp:Label>
    条&nbsp;
 </div>        
     
      </ContentTemplate>
    </asp:UpdatePanel>
    
    
   
    </form>
</body>
</html>

解决方案 »

  1.   


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Text;
    public partial class Index : System.Web.UI.Page
    {
        public static string type = "";
        private static StringBuilder outstr;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] == null)
            {
                type = "0";
            }
            else                                     
            {
                type = Request.QueryString["type"];
                if (!Is_int(type))
                {
                    type = "0";
                }
               
            }        if (!IsPostBack)
            {      
                 try
                {
                    if (Session["UserName"].ToString() != "")
                    {
                        TextBox1.Text = Session["UserName"].ToString();
                    }
                }
                catch 
                 { }
            
               CategroyDataBind();
               aa();
            }
       
        }    protected void aa()
        {
           
      
      
            //if (Request.QueryString["type"]==null)
            //{
            //     type = "0";  
            //}
            //else
            //{
            //     type = Request.QueryString["type"];
            //     if (Is_int(type))
            //     {        //     }
            //     else 
            //     {
            //         type = "0"; 
            //     }
            //}
                 int pagesize = 10;//分页数.每页多少条数据 
            int pageindex = AspNetPager1.CurrentPageIndex;//第几页
            Maticsoft.BLL.Toic aa = new Maticsoft.BLL.Toic();
            Maticsoft.BLL.Tops bb = new Maticsoft.BLL.Tops();
            Maticsoft.Model.Tops cc = new Maticsoft.Model.Tops();
            cc = bb.GetModel(Convert.ToInt32(type));
        
            if(cc==null)
                this.Label2.Text = "";
            else
                this.Label2.Text = cc.Top_Title;        if (cc==null||cc.Top_Class<1)
            {
                AspNetPager1.RecordCount = aa.GetCount(" title=" + type);
                AspNetPager1.DataBind();
                Repeater1.DataSource = aa.GetList(pagesize, pageindex, " title=" + type);
            }
            else
            {
                AspNetPager1.RecordCount = aa.GetCount(" title=" + type + "and IsPost=1");
                this.divid.Visible = true;
                AspNetPager1.DataBind();
                Repeater1.DataSource = aa.GetList(pagesize, pageindex, " title=" + type + "and IsPost=1");
            }       this.Label3.Text = AspNetPager1.RecordCount.ToString();
           Repeater1.DataBind();
      
        }
         
        //分类下拉框的绑定
        private void CategroyDataBind()
        {
     
            //if (Request.QueryString["type"] == null)
            //{
            //    type = "0";
            //}
            //else
            //{
            //    type = Request.QueryString["type"];
            //    if (Is_int(type))
            //    {        //    }
            //    else
            //    {
            //        type = "0";
            //    }        //}
            DropDownList1.Items.Clear();
            DropDownList1.Items.Add(new ListItem("暂无", "0"));
            Maticsoft.BLL.Tops Top = new Maticsoft.BLL.Tops();
            DataTable dt = Top.GetAllList().Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                DropDownList1.Items.Add(new ListItem(dr["Top_Title"].ToString(), dr["Top_Id"].ToString()));
            }
            DropDownList1.SelectedValue=type;
        }    protected void Button1_Click(object sender, EventArgs e)
        {
            //if (Request.QueryString["type"] == null)
            //{
            //    type = "0";
            //}
            //else
            //{
            //    type = Request.QueryString["type"];
            //    if (Is_int(type))
            //    {        //    }
            //    else
            //    {
            //        type = "0";
            //    }        //}
            string content = "";
            if (this.words.Value.Length > 200)
            {
                content = OutString(this.words.Value, 200, false);
            }
            else
            { content = this.words.Value.Trim(); }
            if (Request.Cookies["CheckCode"].Value != null)
            {
                if (String.Compare(Request.Cookies["CheckCode"].Value, this.TextBox2.Text.ToString().ToUpper(), true) != 0)
                {
                   
                    this.Label1.Visible = true;
                    this.Label1.Text = "验证码输入错误!";
                    //WebMessageBox.Show("验证码输入错误!");
                    return;
                }
                else
                {
                    Maticsoft.BLL.Toic aa = new Maticsoft.BLL.Toic();
                    Maticsoft.Model.Toic bb = new Maticsoft.Model.Toic();
                    bb.title = DropDownList1.SelectedValue;                bb.author = this.TextBox1.Text.Trim();
                    bb.Content = content;
                    bb.adddate = DateTime.Now;
                    bb.SubjectClass = 0;
                    bb.IsPost = 0;
                    bb.SendIp = GetIP();                aa.Add(bb);
                    Session["UserName"] = TextBox1.Text;
                    this.words.Value = "";
                   // this.TextBox1.Text = "";
                    this.TextBox2.Text = "";                ScriptManager.RegisterStartupScript(this, typeof(object), "", "<script>window.location.href='index.aspx?type=" + type + "';</script>", false);                
                }
            }
                 }
        protected void Button2_Click(object sender, EventArgs e)
        {
            this.words.Value = "";
            this.TextBox1.Text = "";
        }
        protected string GetIP()
        {        if (Request.ServerVariables["HTTP_VIA"] != null)
            {
                return Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                return Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
        }
        /// <summary>
        /// 防SQL 注入判断是否为数字  
        /// </summary>
        /// <param name="oText"></param>
        /// <returns></returns>
        public static bool Is_int(string oText)
        {
            try
            {
                int var1 = Convert.ToInt32(oText);
                return true;
            }
            catch
            {
                return false;
            }
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            CategroyDataBind();
            aa();
           
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
           // CategroyDataBind();
            aa();    }
        public string outAA(int a, string b)
        {
            if (a > 0)
            {
                return b;
            }
            else 
            {
                return "你发表的留言需管理员审核才能看到!";
            }
        }
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
           
           
        }
        /// <summary>
        /// 返回用于显示的字符串并用省略号代替超过限定字数的字符
        /// </summary>
        /// <param name="instr">要输出的文本</param>
        /// <param name="WordCount">要输出的字数</param>
        /// <param name="Prolong">增加延长符号</param>
        /// <returns></returns>
        public static string OutString(string instr, int WordCount, bool Prolong)
        {
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(instr);
            if (mybyte.Length > WordCount)
            {
                outstr = new StringBuilder();
                for (int i = 0; i < instr.Length; i++)
                {
                    byte[] tempByte = System.Text.Encoding.Default.GetBytes(outstr.ToString());
                    if (tempByte.Length < WordCount * 2)
                    {
                        outstr.Append(instr.Substring(i, 1));
                    }
                    else
                    {
                        if (Prolong)
                        {
                            outstr.Append("...");
                        }
                        break;
                    }
                }
                return outstr.ToString();
            }
            else
            {
                return instr;
            }
        }
    }