如何解决索引-5是负数还是行数以上。 [英] How do I resolve index -5 is either negative or above rows count.

查看:80
本文介绍了如何解决索引-5是负数还是行数以上。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改我的datalist视图以进行分页,我使用了下面的代码,我在c-sharpconer上找到了,但是当我点击前面的按钮时,我收到错误消息,所有其他按钮都能正常工作。

错误消息说:

I am trying to modify my datalist view to have paging , I used the below code which I found on c-sharpconer, but I get error message when I click on the previous buttons, all other buttons work work.
The error message says:

Quote:

索引-5为负数或行数以上。

Index -5 is either negative or above rows count.





这是我的aspx



This is my aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PagininDataList.WebForm1" %> 
<!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">
        .style1
        {
            width: 672px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
   <div>
    <table style="height: 79px; width: 429px">
        <asp:DataList ID="DataList1" runat="server">
        <HeaderTemplate>
        <h1> Details </h1>
        </HeaderTemplate>
      <ItemTemplate>
        <tr>

        My Custom Datalist Template here
        </tr>

        </ItemTemplate>
        </asp:DataList>
       </table>
   <table>
  <tr>
    <td>
        <asp:Button ID="btnfirst" runat="server" Font-Bold="true" Text="<<" Height="31px"
                    Width="43px" onclick="btnfirst_Click" /></td>
        <td>
            <asp:Button ID="btnprevious" runat="server" Font-Bold="true" Text="<" Height="31px"
                    Width="43px" onclick="btnprevious_Click" /></td>
            <td>
                <asp:Button ID="btnnext" runat="server" Font-Bold="true" Text=">" Height="31px"
                    Width="43px" onclick="btnnext_Click" /></td>
                <td>
                    <asp:Button ID="btnlast" runat="server" Font-Bold="true" Text=">>" Height="31px"
                    Width="43px" onclick="btnlast_Click" /></td>
    </tr>
   </table>
    </div>
    </form>
</body>
</html>





这是我的C#代码





this is my C# code

namespace PagininDataList
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlDataAdapter dadapter;
        DataSet dset;
        PagedDataSource adsource = new PagedDataSource();
        string connstring = "My connection string";
        int pos;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ViewState["vs"] = 0;
            }
            pos = (int)this.ViewState["vs"];        
            databind();
        }             
        public void databind()
        {
            dadapter = new SqlDataAdapter("my SELECT query", connstring);
            dset = new DataSet();
            
            dadapter.Fill(dset);
            adsource.DataSource = dset.Tables[0].DefaultView;
            adsource.PageSize = 3;
            adsource.AllowPaging=true;
            adsource.CurrentPageIndex = pos;
            btnfirst.Enabled = !adsource.IsFirstPage;
            btnprevious.Enabled = !adsource.IsFirstPage;
            btnlast.Enabled = !adsource.IsLastPage;
            btnnext.Enabled = !adsource.IsLastPage;
            DataList1.DataSource = adsource;
            DataList1.DataBind(); 
        }
 
        protected void btnfirst_Click(object sender, EventArgs e)
        {
            pos = 0; 
            this.ViewState["vs"] = 0; 
            databind();
        }
 
        protected void btnprevious_Click(object sender, EventArgs e)
        {
            pos = (int)this.ViewState["vs"];
            pos -= 1;
            this.ViewState["vs"] = pos;
            databind();
        }
 
        protected void btnnext_Click(object sender, EventArgs e)
        {
            pos = (int)this.ViewState["vs"];
            pos += 1;
            this.ViewState["vs"] = pos;
            databind();
        }
 
        protected void btnlast_Click(object sender, EventArgs e)
        {
            pos = adsource.PageCount - 1; 
        this.ViewState["vs"] = pos; 
        databind(); 
        }
    }
}





我尝试了什么:



我尝试更改页面大小,但仍然会出错。



What I have tried:

I try changing the page size, but it still give the error.

推荐答案

嗯..如果你在csharp角落找到了代码,那么显而易见的事情就是:问问那里。

与作者交谈,解释你的问题,看看他能提供多少帮助。



在随机网站上询问软件支持来自不同的网站并不是最明智的开始方式......
Well ... if you found the code on csharp corner, then the obvious thing to do is: ask there.
Talk to the author, explain your problem, see how much help he can be.

Asking at a random site for support on software from a different site isn't the most sensible way to get started...


这篇关于如何解决索引-5是负数还是行数以上。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆