当我在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();
}
任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:1)
您是否还在BindGrid
Page_Load
之前致电if(!IsPostBack)
- 请检查?
是没有if(!IsPostBack)
然后添加:)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
MultiView1.SetActiveView(vHome); b
btnBacktoHome.Visible = false;
lblStatus.Visible = false;
}
}
否则,您将使用数据库中的旧值对GridView
进行数据绑定,这也会阻止所有事件被触发。
答案 1 :(得分:0)
分页的必要属性:
PagerSettings-Visible="true"
PagerSettings-Mode="NextPreviousFirstLast"
PageSize="20"
AllowPaging="true"