想完成一个效果,www.xinnuo.net/common/company.aspx,请各位高手帮忙

effydeng 2002-10-22 10:19:05
结构显示如:www.xinnuo.net/common/company.aspx

分页我都已经做好了,数据绑定则是想让一行显示‘两列’,这样我还不知道如何去实现。

给各位看看我的完整代码吧,请注意被注释的地方。

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Configuration" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

int Page,Count,intCount,StartIndex,LastIndex;
string Sql,strCount,strarr;
int PageSize,RecordCount,PageCount,CurrentPage,PageNum;

/*PageSize每页显示行数
RecordCount总记录数
PageCount总页数
CurrentPage当前页数
PageNum每页显示总记录数
PageNum是由每页的行数(PageSize)乘以每行的列数(RepeatColumns)所得*/

SqlConnection MyConn;
public void Page_Load(Object sender,EventArgs e){
String Provider,ConnStr,Sql1,DataBase;

PageSize = 5;
PageNum = PageSize*DataList1.RepeatColumns;//RepeatColumns每行显示列数

MyConn=new SqlConnection();
MyConn.ConnectionString=ConfigurationSettings.AppSettings["DSN_pubs"];
MyConn.Open();

if (!IsPostBack)
{
CurrentPage=0;
ViewState["PageIndex"]=0;
RecordCount = CalculateRecord();
string[] strcom = new CharRecord();//这里想将字段数组值取出
BindList();
lblRecordCount.Text = RecordCount.ToString();//记录数
PageCount = RecordCount/PageNum;
if (RecordCount % PageNum !=0){
PageCount = (PageCount + 1);
}
lblPageCount.Text = PageCount.ToString();//总页数
ViewState["PageCount"] = PageCount;
}
MyConn.Close();
}
void BindList(){

DataList1.DataSource = CreatSource();
DataList1.DataBind();

lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage == (PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage == 0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();

}

ICollection CreatSource(){

StartIndex = CurrentPage*PageNum;//起始导入
LastIndex = StartIndex + PageNum;//最终导入
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

for (int i = StartIndex; i < LastIndex ; i++)
{
dr = dt.NewRow();
dr[0] = strcom[i]//根据I的不同取出数组中相应的字段值
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

}

public string[] CharRecord(){//申明数组函数提取所有记录中字段COMNAME的值
ArrayList arr=new ArrayList();
Sql="select * from company order by adate desc";
SqlCommand MyComm = new SqlCommand(Sql,MyConn);
SqlDataReader MyReader=MyComm.ExecuteReader();
int index = 0;
while (MyReader.Read()){
strarr=MyReader["comname"].ToString();
CharRecord[index]=strarr; //想这样填充CharRecord[]数组
index++;
}
MyReader.Close();
}

public int CalculateRecord(){//计算出记录数
strCount = "select count(*) as Count from company ";
SqlCommand MyComm = new SqlCommand(strCount,MyConn);
SqlDataReader dr = MyComm.ExecuteReader();
if(dr.Read()){
intCount = Int32.Parse(dr["Count"].ToString());
}else{
intCount = 0;
}
dr.Close();
return intCount;
}
public void Page_OnClick(Object sender,CommandEventArgs e){//判断翻页方向
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];

string Cmd = e.CommandName;
switch(Cmd){
case "next":
if (CurrentPage<PageCount-1) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
BindList();
}
</script>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<p>
<form action="titlelist.aspx" mathod="post" runat=server>
共有<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="DataList1" runat="server"
width="90%"
BorderWidth="1"
GridLines="Both"
BorderColor="black"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"
RepeatColumns="3"//这里是定义了每行显示的列数
RepeatDirection="Horizontal"
>

<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>

<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>

<HeaderTemplate>

公司名称

</HeaderTemplate>

<ItemTemplate>

<%# DataBinder.Eval(Container.DataItem, "StringValue") %>

</ItemTemplate>

<AlternatingItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>

</AlternatingItemTemplate>

</asp:DataList>
<asp:LinkButton Text="上一页" id="lbnPrevPage" CommandName="prev" OnCommand="Page_OnClick" runat=server/>
<asp:LinkButton Text="下一页" id="lbnNextPage" CommandName="next" OnCommand="Page_OnClick" runat=server/>
</form>
</body>
</html>

看完了吗?现在不正确的就是申明数组函数public string[] CharRecord()有误,不知该如何将所有记录的comname这个字段值提取成数组,又如何根据数组变量I,在ICollection CreatSource()中进行显示。
...全文
96 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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