DataGrid中没有RepeatColumns属性,怎么弄呢?DataList又不支持分页呀

nowfox 2004-08-20 02:55:49
我每条记录都比较短,要把下面的
---------------------
1
2
3
4
5
6
……
---------------------
换成
---------------------
1 2
3 4
5 6
……
---------------------
在DataList里可以用RepeatColumns=2,但他不支持分页呀,而DataGrid里又没有这个属性,怎么弄?
...全文
243 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nowfox 2004-08-20
  • 打赏
  • 举报
回复
刚看了孟老大分页显示,很简单的,谢谢各位了。结帖
mengfanpp 2004-08-20
  • 打赏
  • 举报
回复
我分页的用户控件,你拷过去,改一下sql语句,改一下前台,就能用,
CasusMore_.ascx
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="CasusMore_.ascx.cs" Inherits="huawei.myClass.CasusMore_" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<FONT face="宋体">
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="98%" border="0">
<TBODY>
<TR>
<TD>
</FONT>
<asp:DataList id="DL_Report" runat="server" RepeatColumns="2" Width="100%" Height="100%" ShowHeader="False"
ShowFooter="False">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<TABLE id="Table1" cellSpacing="0" cellPadding="0" border="0">
<TR>
<TD height="10"></TD>
</TR>
<TR>
<TD align="center"><A href="#" target="_blank"><IMG src='<%# DataBinder.Eval(Container.DataItem, "Picture") %>' border=0></A>
</TD>
</TR>
<TR>
<TD align="center"><%# DataBinder.Eval(Container.DataItem, "PicIntro") %></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:DataList></TD></TR>
<TR>
<TD align="right"><FONT face="宋体">共有记录
<asp:label id="TotalRecord" runat="server">Label</asp:label>条 共<asp:label id="TotalPage" runat="server">1</asp:label>页
第<asp:label id="PageNo" runat="server">1</asp:label>页 
<asp:linkbutton id="Back" runat="server" ForeColor="Blue" Font-Underline="True">前页</asp:linkbutton> 
<asp:linkbutton id="Next" runat="server" ForeColor="Blue" Font-Underline="True">后页</asp:linkbutton> 
跳至第
<asp:textbox id="JumpPage" runat="server" Width="24px">1</asp:textbox>页
<asp:imagebutton id="Jump" runat="server" ImageUrl="../images/go1.gif"></asp:imagebutton></FONT></TD>
</TR>
</TBODY></TABLE></FONT>

代码文件:CasusMore_.ascx.cs
namespace huawei.myClass
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;


/// <summary>
/// CasusMore_ 的摘要说明。
/// 前台显示更多案例时调用
///
/// --梦凡 2004年8月16号15:24
/// </summary>
public class CasusMore_ : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.ImageButton Jump;
protected System.Web.UI.WebControls.TextBox JumpPage;
protected System.Web.UI.WebControls.LinkButton Next;
protected System.Web.UI.WebControls.LinkButton Back;
protected System.Web.UI.WebControls.Label PageNo;
protected System.Web.UI.WebControls.Label TotalPage;
protected System.Web.UI.WebControls.Label TotalRecord;
protected System.Web.UI.WebControls.DataList DL_Report;

PagedDataSource objPds = new PagedDataSource();
string cmStr = "select ID,Picture,PicIntro "+
"from Casus "+
"order by Publictime desc";

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindDataList(0);
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Back.Click += new System.EventHandler(this.Back_Click);
this.Next.Click += new System.EventHandler(this.Next_Click);
this.Jump.Click += new System.Web.UI.ImageClickEventHandler(this.Jump_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

//数据绑定
private void BindDataList(int CurrentPageIndex)
{
DataOperate dp = new DataOperate("CnStr");
SqlDataAdapter da = new SqlDataAdapter(cmStr,dp.Cn);
DataSet ds = new DataSet();
da.Fill(ds, "Casus");
objPds.DataSource = ds.Tables["Casus"].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 6;

DL_Report.DataSource = objPds;
TotalRecord.Text = objPds.DataSourceCount.ToString();

objPds.CurrentPageIndex = CurrentPageIndex;
DL_Report.DataBind();
PageFooter();
}

//页数、第几页统计显示
private void PageFooter()
{
TotalPage.Text= objPds.PageCount.ToString();
int PageNum = objPds.CurrentPageIndex + 1;
PageNo.Text= PageNum.ToString();
JumpPage.Text = PageNum.ToString();
}

//上一页
private void Back_Click(object sender, System.EventArgs e)
{
int CurrentPageIndex = Convert.ToInt32(PageNo.Text)-1;
if( CurrentPageIndex <= 0)
Response.Write("<script>alert('已是第一页!');</script>");
else
{
CurrentPageIndex -= 1;
BindDataList(CurrentPageIndex);
int PageNum = CurrentPageIndex + 1;
this.JumpPage.Text = PageNum.ToString();
}
}

//下一页
private void Next_Click(object sender, System.EventArgs e)
{
int CurrentPageIndex = Convert.ToInt32(PageNo.Text)-1;
if( CurrentPageIndex >=(Convert.ToInt32(TotalPage.Text)))
Response.Write("<script>alert('已到最后一页!');</script>");
else
{
CurrentPageIndex += 1;
BindDataList(CurrentPageIndex);
int PageNum = CurrentPageIndex + 1;
this.JumpPage.Text = PageNum.ToString();
}
}

//转到第几页
private void Jump_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Regex digitregex = new Regex(@"^[0-9]\d*[.]?\d*$");
if (!digitregex.IsMatch(JumpPage.Text))
{
JumpPage.Text="";
Response.Write("<script>alert('只能输入数字!');</script>");
}
else
{
int CurrentPageIndex;
CurrentPageIndex = Convert.ToInt32(JumpPage.Text.ToString())-1;
if (CurrentPageIndex > (Convert.ToInt32(TotalPage.Text)-1))
{
Response.Write("<script>alert('输入的数值要在总页数范围之内!');</script>");
}
else if(CurrentPageIndex < 0)
{
Response.Write("<script>alert('输入的数值要在总页数范围之内!');</script>");
}
else
{
BindDataList(CurrentPageIndex);
}
}
}


}
}
暗石绿 2004-08-20
  • 打赏
  • 举报
回复
<HTML>
<HEAD>
<title></title>
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<Script Language="C#" Runat="Server">
/*
Modified By Blur
Support .Net Framework Beta 2
*/
SqlConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;//分页之用
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;
//连接语句
string MyConnString = "User ID=sa;Password=;initial catalog=NorthWind;data source=www";
MyConn = new SqlConnection(MyConnString);
//打开数据库连接
MyConn.Open();

//第一次请求执行
if(!Page.IsPostBack)
{
//计算总共有多少记录
RecordCount = CalculateRecord();
//计算总共有多少页
//取整
PageCount = RecordCount/PageSize;
if (RecordCount%PageSize > 0)
PageCount = PageCount + 1;
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
ViewState["PageCount"] = PageCount;
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//绑定
ListBind();
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from products";
SqlCommand MyComm = new SqlCommand(strCount,MyConn);
SqlDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}

ICollection CreateSource()
{

int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from products";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"products");
return ds.Tables["products"].DefaultView;
}
public void ListBind()
{
fenye.DataSource = CreateSource();
fenye.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(PageCount==0)
{
lblCurrentPage.Text = "0";
lbnNextPage.Enabled = false;
lbnPrevPage.Enabled = false;
}
else
{
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
}

public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}

ViewState["PageIndex"] = CurrentPage;

ListBind();

}
</Script>
</HEAD>
<body>
<form runat="server" ID="Form1">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
<asp:DataList id="fenye" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="Gainsboro" EditItemStyle-BackColor="yellow">
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"productname") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
</form>
</body>
</HTML>
Hero4444 2004-08-20
  • 打赏
  • 举报
回复
DataList的分页可以自己寫代碼啊
jiezhi 2004-08-20
  • 打赏
  • 举报
回复
http://www.cnblogs.com/mack/archive/2004/08/19/34952.aspx
jiezhi 2004-08-20
  • 打赏
  • 举报
回复
根据其邦定的数据集的纪录数来确定
wnlovezxm 2004-08-20
  • 打赏
  • 举报
回复
DataList可以写代码自己分页!

如果要是用grid实现这样的效果,不太容易!

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧