用户控件中利用DATAGRID实现分页,但是在ASPX页面中必须点击分页按钮两次才可实现分页功能?真不不明白为什么? 你的具体代码帖出来看看呢?-----------------------------------http://ceocio.blogger.cn----------------------------------- 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 namespace XSCWeb.UserControl{ using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; /// <summary> /// NewsGrid 的摘要说明。 /// </summary> public class NewsGrid : ControlBase { protected System.Web.UI.WebControls.DataGrid DataGrid1; private int classID=0; private int specialID=0; private string className=""; private string specialName=""; private int categoryID=0; private string categoryName=""; private string selectSql=""; private int titleWidth=0; private int gridWidth=0; protected System.Web.UI.WebControls.HyperLink HyperLink1; protected System.Web.UI.WebControls.HyperLink HyperLink2; protected System.Web.UI.WebControls.HyperLink HyperLink3; protected System.Web.UI.WebControls.Panel Panel2; private string itemImgUrl=""; public int ClassID { set { classID=value; } get { return classID; } } public int SpecialID { set { specialID=value; } get { return specialID; } } public string ClassName { get { if(classID!=0) { base.showSubClass=new ShowSubClass(classID); className=base.showSubClass.ClassName; } return className; } } public string SpecialName { get { if(specialID!=0) { base.showSpecial=new ShowSpecial(specialID); specialName=base.showSpecial.SpecialName; } return specialName; } } public int CategoryID { get { if(classID!=0) { base.showSubClass=new ShowSubClass(classID); categoryID=base.showSubClass.CategoryID; } else { base.showSpecial=new ShowSpecial(specialID); categoryID=base.showSpecial.CategoryID; } return categoryID; } } public string CategoryName { get { base.showCategory=new ShowCategory(this.CategoryID); categoryName=base.showCategory.CategoryName; return categoryName; } } public string SelectSql { set { selectSql=value; } } public string ItemImgUrl { set { itemImgUrl=value; } } public int TitleWidth { set { titleWidth=value; } } public int GridWidth { set { gridWidth=value; } } public DataGrid NewsDataGrid { get { return DataGrid1; } } private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { if(gridWidth!=0) DataGrid1.Width=new Unit(gridWidth); else DataGrid1.Width=new Unit(510); BindtoDataGrid(); HyperLink1.Text="首页"; HyperLink1.NavigateUrl=base.WebUrl+"index.aspx"; HyperLink2.Text=this.CategoryName; HyperLink2.NavigateUrl=""; if(classID!=0) { HyperLink3.Text=this.ClassName; } else { HyperLink3.Text=this.SpecialName; } } // 在此处放置用户代码以初始化页面 //MoreLink.Visible=false; } private void BindtoDataGrid() { DataSet ds=new DataSet(); base.showNews=new ShowNews(); if(selectSql!="") { ds=base.showNews.GetNewsbySelectSql(selectSql); } else { if(classID!=0) { ds=base.showNews.GetNewsbyClassID(classID); } if(specialID!=0) { ds=base.showNews.GetNewsbySpecialID(specialID); } } if(ds.Tables.Count!=0) { DataGrid1.DataSource=ds; DataGrid1.DataBind(); } } public void MakeNewsList() { DataGrid1.PageSize=8; DataGrid1.PagerStyle.Visible=false; //MoreLink.Visible=true; } public void SetPageSize(int pageSize) { DataGrid1.PageSize=pageSize; } public void SetItemBackColor(string color) { DataGrid1.ItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml(color); } public void SetAItemBackColor(string color) { DataGrid1.AlternatingItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml(color); } public void SetPagerBackColor(string color) { DataGrid1.PagerStyle.BackColor=System.Drawing.ColorTranslator.FromHtml(color); } //设置HyperLink1,2,3不可见 public void SetPanelUnvisible() { Panel2.Visible=false; } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() { this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged); this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex=e.NewPageIndex; BindtoDataGrid(); } private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { //set ItemImg if(e.Item.ItemIndex>-1) { System.Web.UI.WebControls.Image NowItemImg=(System.Web.UI.WebControls.Image)e.Item.Cells[2].FindControl("ItemImg"); TimeSpan ts=new TimeSpan(); DateTime updateTime=Convert.ToDateTime(e.Item.Cells[3].Text); ts=DateTime.Now.Subtract(updateTime); int diff=ts.Days; if (diff<=3) { NowItemImg.ImageUrl=base.WebUrl+"images/new.gif"; } else { NowItemImg.ImageUrl=base.WebUrl+"images/item-img.gif"; //有ItemImgUrl可以用 } //set TitleLink System.Web.UI.WebControls.HyperLink Title=(HyperLink)e.Item.Cells[2].FindControl("TitleLink"); string text=e.Item.Cells[4].Text; if(titleWidth>0&&titleWidth<text.Length) { text=e.Item.Cells[4].Text.Substring(0,titleWidth)+"..."; } //text=@"<img src="+base.WebUrl+@"images/item-img.gif>"+text; if(e.Item.Cells[7].Text==" ") { Title.Text=text; } else { Title.Text=text+"(图)"; } Title.ForeColor=System.Drawing.ColorTranslator.FromHtml(e.Item.Cells[6].Text); Title.NavigateUrl=base.WebUrl+"Content.aspx?NewsID=" + e.Item.Cells[0].Text; Title.ToolTip=e.Item.Cells[4].Text + ";人气:"+e.Item.Cells[5].Text; try { foreach(System.Web.UI.Control c in e.Item.Cells[2].Controls) { if(c.GetType().ToString()=="System.Web.UI.LiteralControl") { //e.Item.Cells[2].Controls.Remove(c); c.Visible=false; } } } catch {} } } }} 参考以下几篇:http://www.csdn.net/develop/Read_Article.asp?Id=22259http://www.csdn.net/develop/Read_Article.asp?Id=22297http://www.csdn.net/develop/Read_Article.asp?Id=21875http://www.csdn.net/develop/Read_Article.asp?Id=18627http://www.csdn.net/develop/Read_Article.asp?Id=18760 有谁不会分页的,我建议大家去看看这个帖子http://expert.csdn.net/Expert/topic/2436/2436657.xml?temp=.1623194看过后什么页都会分了。 按一下页的代码中还要对datagrid重新进行ban定!就是重新与数据集联接 怎么在后台page_load函数中得到前台浏览器的信息 asp.net防盗链如何做? 好穷,没有分给了,呜~~~~~ 请问!Cookies不能保存进我的Cookies文件夹里?为什么! 关于DataGrid中删除列的提示问题 关于asp.net里面数据验证 如何得知系统支持并行的用户数量 水晶报表的表头可以动态生成吗? 求助,菜鸟问题? dropdownlist控件下拉框中的内容从数据库中帮定数据,当数据库中查询的结果为空的时候,需要判断.我用下面的方法提示出错@!哀 不定条件组合查询问题?? 请问怎么在javascript里调用RGB()函数
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; /// <summary>
/// NewsGrid 的摘要说明。
/// </summary>
public class NewsGrid : ControlBase
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private int classID=0;
private int specialID=0;
private string className="";
private string specialName="";
private int categoryID=0;
private string categoryName="";
private string selectSql="";
private int titleWidth=0;
private int gridWidth=0;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.HyperLink HyperLink3;
protected System.Web.UI.WebControls.Panel Panel2;
private string itemImgUrl="";
public int ClassID
{
set
{
classID=value;
}
get
{
return classID;
}
}
public int SpecialID
{
set
{
specialID=value;
}
get
{
return specialID;
}
}
public string ClassName
{
get
{
if(classID!=0)
{
base.showSubClass=new ShowSubClass(classID);
className=base.showSubClass.ClassName;
}
return className;
}
}
public string SpecialName
{
get
{
if(specialID!=0)
{
base.showSpecial=new ShowSpecial(specialID);
specialName=base.showSpecial.SpecialName;
}
return specialName;
}
}
public int CategoryID
{
get
{
if(classID!=0)
{
base.showSubClass=new ShowSubClass(classID);
categoryID=base.showSubClass.CategoryID;
}
else
{
base.showSpecial=new ShowSpecial(specialID);
categoryID=base.showSpecial.CategoryID;
}
return categoryID;
}
}
public string CategoryName
{
get
{
base.showCategory=new ShowCategory(this.CategoryID);
categoryName=base.showCategory.CategoryName;
return categoryName;
}
}
public string SelectSql
{
set
{
selectSql=value;
}
}
public string ItemImgUrl
{
set
{
itemImgUrl=value;
}
}
public int TitleWidth
{
set
{
titleWidth=value;
}
}
public int GridWidth
{
set
{
gridWidth=value;
}
}
public DataGrid NewsDataGrid
{
get
{
return DataGrid1;
}
} private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
if(gridWidth!=0)
DataGrid1.Width=new Unit(gridWidth);
else
DataGrid1.Width=new Unit(510);
BindtoDataGrid();
HyperLink1.Text="首页";
HyperLink1.NavigateUrl=base.WebUrl+"index.aspx";
HyperLink2.Text=this.CategoryName;
HyperLink2.NavigateUrl="";
if(classID!=0)
{
HyperLink3.Text=this.ClassName;
}
else
{
HyperLink3.Text=this.SpecialName;
}
}
// 在此处放置用户代码以初始化页面
//MoreLink.Visible=false;
}
private void BindtoDataGrid()
{
DataSet ds=new DataSet();
base.showNews=new ShowNews();
if(selectSql!="")
{
ds=base.showNews.GetNewsbySelectSql(selectSql);
}
else
{
if(classID!=0)
{
ds=base.showNews.GetNewsbyClassID(classID);
}
if(specialID!=0)
{
ds=base.showNews.GetNewsbySpecialID(specialID);
}
}
if(ds.Tables.Count!=0)
{
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
}
public void MakeNewsList()
{
DataGrid1.PageSize=8;
DataGrid1.PagerStyle.Visible=false;
//MoreLink.Visible=true;
}
public void SetPageSize(int pageSize)
{
DataGrid1.PageSize=pageSize;
}
public void SetItemBackColor(string color)
{
DataGrid1.ItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml(color);
}
public void SetAItemBackColor(string color)
{
DataGrid1.AlternatingItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml(color);
}
public void SetPagerBackColor(string color)
{
DataGrid1.PagerStyle.BackColor=System.Drawing.ColorTranslator.FromHtml(color);
}
//设置HyperLink1,2,3不可见
public void SetPanelUnvisible()
{
Panel2.Visible=false;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindtoDataGrid();
} private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//set ItemImg
if(e.Item.ItemIndex>-1)
{
System.Web.UI.WebControls.Image NowItemImg=(System.Web.UI.WebControls.Image)e.Item.Cells[2].FindControl("ItemImg");
TimeSpan ts=new TimeSpan();
DateTime updateTime=Convert.ToDateTime(e.Item.Cells[3].Text);
ts=DateTime.Now.Subtract(updateTime);
int diff=ts.Days;
if (diff<=3)
{
NowItemImg.ImageUrl=base.WebUrl+"images/new.gif";
}
else
{
NowItemImg.ImageUrl=base.WebUrl+"images/item-img.gif";
//有ItemImgUrl可以用
} //set TitleLink
System.Web.UI.WebControls.HyperLink Title=(HyperLink)e.Item.Cells[2].FindControl("TitleLink");
string text=e.Item.Cells[4].Text;
if(titleWidth>0&&titleWidth<text.Length)
{
text=e.Item.Cells[4].Text.Substring(0,titleWidth)+"...";
}
//text=@"<img src="+base.WebUrl+@"images/item-img.gif>"+text;
if(e.Item.Cells[7].Text==" ")
{
Title.Text=text;
}
else
{
Title.Text=text+"(图)";
}
Title.ForeColor=System.Drawing.ColorTranslator.FromHtml(e.Item.Cells[6].Text);
Title.NavigateUrl=base.WebUrl+"Content.aspx?NewsID=" + e.Item.Cells[0].Text;
Title.ToolTip=e.Item.Cells[4].Text + ";人气:"+e.Item.Cells[5].Text;
try
{
foreach(System.Web.UI.Control c in e.Item.Cells[2].Controls)
{
if(c.GetType().ToString()=="System.Web.UI.LiteralControl")
{
//e.Item.Cells[2].Controls.Remove(c);
c.Visible=false;
}
}
}
catch
{}
}
}
}
}
http://www.csdn.net/develop/Read_Article.asp?Id=22259
http://www.csdn.net/develop/Read_Article.asp?Id=22297
http://www.csdn.net/develop/Read_Article.asp?Id=21875
http://www.csdn.net/develop/Read_Article.asp?Id=18627
http://www.csdn.net/develop/Read_Article.asp?Id=18760
http://expert.csdn.net/Expert/topic/2436/2436657.xml?temp=.1623194
看过后什么页都会分了。