比如页面中有一条新闻,我看完这一条想看下一条,我点击下一条就到下一条新闻,点击上一条就到上一条新闻。
同时,上一条和下一条新闻的标题也在此页面显示。
上一条可以用sql :select top 1 * from news where id<当前id
下一条可以用sql: select top 1 * from news where id>当前id
上一条与下一条我写在linkbutton的事件里写这是上一条事件里的代码,下一条以此类推,其中News_Show.Aspx是本页面,我将传送的参数放在viewstate里
     
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int aa = 0;
        DataTable dt = DAL.db.dt("select top 1 id,title from news where classid like '," + ViewState["typeid"] + ",' and id<" + Convert.ToInt32(ViewState["id"]) + " order by id desc");
        if (dt.Rows.Count > 0)
        {
            aa = int.Parse(dt.Rows[0]["id"].ToString());
            
        }
        if (aa != 0)
        {
            Response.Redirect("News_Show.Aspx?NewsID=" + aa + "&NewsClassID=" + ViewState["typeid"]);
        }
        else
            this.LinkButton1.Enabled = false;
    }
问题1:当我连续点击下一条或上一条太多的时候,有时会出现一个“不允许回调或回发的错误”,在页面中加上这句EnableEventValidation="true"还是不行,请高手指教。
问题2:有没有更好的办法,我感觉这个方法很不好用,有的话可以贴出来看看我将感激不尽。我曾就这个问题发过一贴,今我再发一贴,如不行,我还要发。

解决方案 »

  1.   

    我也出过页面拒绝访问,看提示信息是说from表单提交被拒绝了。加了句EnableEventValidation="true" 也不行。建议你在取数据的时候用 id=value 的方式去取,另外在你取出本页的时候,就把上一页与下一页的ID 值取出来保存。当点击下一页或上一页就可以直接去取数据。帮你顶一下。
      

  2.   

    请参考<asp:DataList runat="server" ID="recommendedproductlistdl" RepeatColumns="5" RepeatDirection="Horizontal" CellSpacing="10">
                    <ItemTemplate>
                       <div align="left">
                            <table  width="140" height="142" cellspacing="1" cellpadding="2"  border="0">
                              <tbody>
                                <TR> 
                                  <TD align="center" background="images/136.jpg" height=140> 
                                     <a  title='<%#Eval("productname") %>' href='productinfo.aspx?pid=<%#Eval("productid") %>'><img src='pic/<%#Eval("imagepath").ToString() %>' width="100" height="100" border="0" align="absmiddle" /></a> 
                                  </TD>
                                </TR>
                                <tr>
                                   <td valign="top" align="center">
                                      <a title='<%#Eval("productname") %>' href='productinfo.aspx?pid=<%#Eval("productid") %>'>
                                     <b>
                                        <%#ClothesShop.Web.usaulclass.LeftTitle(Eval("productname").ToString(),18) %> 
                                     </b>
                                     </a>
                                     <br />
                                     市场价:<s><%#Eval("price") %></s> 元 <br />
                                     会员价:<%#Eval("userprice") %> 元 <br />
                                   </td>
                                </tr>
                              </tbody>
                        </table>
                  </div>
                    </ItemTemplate>
                </asp:DataList>
                <asp:Panel runat="server" ID="pagepanel"  style="text-align:center; width:100%; font-size:12px; padding:2px 5px 2px 5px;" Width="167px">
               <asp:Label runat="server" ID="countlabel"></asp:Label>
              <asp:Label runat="server" ID="currentlabel"></asp:Label>
              <asp:HyperLink runat="server" ID="firstlinkbutton" Text="第一页"></asp:HyperLink>
              <asp:HyperLink runat="server" ID="prevlinkbutton" Text="上一页"></asp:HyperLink>
              <asp:HyperLink runat="server" ID="newlinkbutton" Text="下一页"></asp:HyperLink>
              <asp:HyperLink runat="server" ID="lastlinkbutton" Text="最后一页"></asp:HyperLink>
          </asp:Panel>private void Bind()
            {
                ClothesShop.BLL.product pro = new ClothesShop.BLL.product();
                DataTable table = pro.getrecommendedproduct();
                if (table.Rows.Count > 0)
                {
                    this.recommendedproductlistdl.Visible = true;
                    this.pagepanel.Visible = true;                PagedDataSource objPds = new PagedDataSource();
                    objPds.DataSource = table.DefaultView;
                    objPds.AllowPaging = true;
                    objPds.PageSize = 15;                this.countlabel.Text = "共:" + objPds.PageCount.ToString() + "页";
                    int CurPage;
                    if (Request.QueryString["Page"] != null)
                        CurPage = Convert.ToInt32(Request.QueryString["Page"]);
                    else
                        CurPage = 1;                objPds.CurrentPageIndex = CurPage - 1;                this.currentlabel.Text = "当前页:" + CurPage.ToString();                if (!objPds.IsFirstPage)
                    {
                        this.firstlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";                    this.prevlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
                    }                if (!objPds.IsLastPage)
                    {
                        this.newlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);                    this.lastlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPds.PageCount.ToString();
                    }                this.recommendedproductlistdl.DataSource = objPds;
                    this.recommendedproductlistdl.DataBind();
                }
                else
                {
                    this.recommendedproductlistdl.Visible = false;
                    this.pagepanel.Visible = false;
                }        }
      

  3.   

    System.Web.UI.WebControls.PagedDataSource
    这个东西好好研究一下.
      

  4.   


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = DAL.db.dt("select * from news where classid like '," + ViewState["typeid"] + ",' order by id desc");
                if (ViewState["id"]!=null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["ID"].ToString() == ViewState["id"].ToString())
                        {
                            LinkButton1.PostBackUrl = "News_Show.Aspx?NewsID=" + dt.Rows[(i + dt.Rows.Count - 1) % dt.Rows.Count]["ID"].ToString() + "&NewsClassID=" + ViewState["typeid"];
                            LinkButton2.PostBackUrl = "News_Show.Aspx?NewsID=" + dt.Rows[(i + 1) % dt.Rows.Count]["ID"].ToString() + "&NewsClassID=" + ViewState["typeid"];
                        }
                    }
                }            
            }
        }