![](/img/trans.png)
[英]Adding a control to to the footer of gridview (asp.net) next to the paging stuff
[英]Adding paging to my ASP.NET GridView
我必須顯示並提取100k +記錄表。
我正在使用GridView,但由於memoryException發生而沒有顯示數據。
所以我想向我的GridView添加分頁系統。 我嘗試了各種教程,但所有內容都涵蓋了使用gridview加載頁面時的情況。 但是在我的情況下,GridView在按按鈕要求時加載。
如何綁定我的代碼進行分頁,以便每個頁面顯示10-20條記錄?
這是我的代碼背后:
protected void ExportToExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Pfilename.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
List<Control> controls = new List<Control>();
//Add controls to be removed to Generic List
foreach (Control control in cell.Controls)
{
controls.Add(control);
}
//Loop through the controls to be removed and replace then with Literal
foreach (Control control in controls)
{
switch (control.GetType().Name)
{
case "HyperLink":
cell.Controls.Add(new Literal { Text = (control as HyperLink).Text });
break;
case "TextBox":
cell.Controls.Add(new Literal { Text = (control as TextBox).Text });
break;
case "LinkButton":
cell.Controls.Add(new Literal { Text = (control as LinkButton).Text });
break;
case "CheckBox":
cell.Controls.Add(new Literal { Text = (control as CheckBox).Text });
break;
case "RadioButton":
cell.Controls.Add(new Literal { Text = (control as RadioButton).Text });
break;
}
cell.Controls.Remove(control);
}
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
protected void ViewPP_Click(object sender, EventArgs e)
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
最后更新了我的代碼:
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
問題是,當我將文件導出到excel時。 它會在excel文件中創建分頁,但無法正常運行。 它在excel工作表中顯示分頁,並且單擊不起作用。
分頁是一項非常基本的任務
這是一個指導您的教程: 對GridView的數據進行分頁和排序
<asp:GridView ID="GridView1" Runat="server"
AutoGenerateColumns="False"
AllowPaging="True" >
但是,主要問題是您要將所有數據加載到DataTable中。 這會將所有數據加載到內存中。 您應該改用SqlDataSource 。 上面的教程還向您展示了如何使用SqlDataSource。
編輯 :
在按鈕單擊上設置SqlDatasource:
protected void Button_Click(object sender, EventArgs e)
{
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.