我想要發生的是有一個分頁,有一個清晰的數據看看。 這裏是我的GridView的HTML代碼:添加分頁到gridview asp.net
<asp:gridview ID = "grid" runat="server" AllowPaging="true" OnPageIndexChanging="gdview_PageIndexChanging">
和後面的代碼:
public static string cs = "Server=PAULO;Database=ShoppingCartDB;Integrated Security=true";
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] != null)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(cs);
con.Open();
string sql = "SELECT * FROM CustomerDetails Where CustomerName = '" + Session["New"] +"'";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
Label2.Text += Session["New"].ToString();
linkLogout.Visible = true;
//linkOrderHistory.Visible = true;
Label2.Visible = true;
linkViewProfile.Visible = true;
grid.DataSource = dt;
grid.DataBind();
}
}
}
private void CustomBindData()
{
SqlConnection con = new SqlConnection(cs);
con.Open();
string sql = "SELECT * FROM CustomerDetails Where CustomerName = '" + Session["New"] + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
}
protected void gdview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
CustomBindData();
grid.PageIndex = e.NewPageIndex;
grid.DataBind();
}
不知我的代碼不能正常工作。它有頁面,但是當我點擊頁面2時,沒有數據顯示。我認爲它有什麼關於如何從sql中獲取數據。對此有何技巧?
將數據綁定部分放入一個函數中,並在兩個地方調用它,一個是在load中,另一個是在grid.DataBind()之前的pageIndexChanging。 –
還不夠,你需要在自定義函數中設置源碼 –
@ARUN我在想什麼? –