gpt4 book ai didi

c# - GridView PageSize 不在下一页显示记录

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:18 25 4
gpt4 key购买 nike

当我在 gridview 中使用 AllowPaging="True" 属性并导航到第 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();
}

任何帮助将不胜感激。提前致谢。

最佳答案

您是否也从 Page_Load 调用 BindGrid 而没有 if(!IsPostBack) 检查?

Yes without if(!IsPostBack)

然后添加:)

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

否则,您将使用数据库中的旧值对 GridView 进行数据绑定(bind),这也会阻止触发所有事件。

关于c# - GridView PageSize 不在下一页显示记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18634826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com