2

当我AllowPaging="True"在 gridview 中使用属性并导航到第 2 页时,我的代码会将我扔到我网站的主页上。代码没有在第 2 页上显示记录列表。它也没有抛出任何错误,所以我无法确定我哪里出错了。像这样的 .aspx 页面代码是:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="10" CellSpacing="4" OnPageIndexChanging="GridView1_PageIndexChanging" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="DocumentsId" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

.cs 文件代码为:

protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
    MultiView1.SetActiveView(vHome);
    btnBacktoHome.Visible = false;
    lblStatus.Visible = false;       
}

public void BindGrid()
{
    SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            String sql = "select [DocumentsID],[Ref],[Subject],[Src],[Dst],[Medium],[Date_Printed],[Date_Received],[Document_Type],[Action_Required],[Due_Date],[Actual_Date],[Content],[Tag],[Issue_No],[Attachment],[Notes],[Assigned_To],[Reply_Ref],[Priority],[Status],[Response],[Physical_File_No],[Physical_Rack_Location] from dbo.Documents";
            cmd.Connection = con;
            cmd.CommandText = sql;
            con.Open();
            DataSet ds = new DataSet();
            using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
            {
                adp.Fill(ds);

            } GridView1.DataSource = ds.Tables[0]; 
            GridView1.DataBind();
        }

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGrid();
}

任何帮助都感激不尽。提前致谢。

4

2 回答 2

1

分页的必要属性:

PagerSettings-Visible="true"     
PagerSettings-Mode="NextPreviousFirstLast"     
PageSize="20"     
AllowPaging="true"
于 2014-08-02T17:37:29.380 回答
1

您是否也BindGridPage_Load没有if(!IsPostBack)-check 的情况下拨打电话?

是的,没有 if(!IsPostBack)

然后添加:)

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack)
    {
        BindGrid(); 
        MultiView1.SetActiveView(vHome); b
        btnBacktoHome.Visible = false; 
        lblStatus.Visible = false; 
    }
}

否则,您将GridView使用数据库中的旧值进行数据绑定,这也可以防止触发所有事件。

于 2013-09-05T11:41:11.800 回答