zoukankan
html css js c++ java
C# 生产新闻文章分页
有的时候我们遇到输出的内容太长,导致失去页面美观。现在我们采用给这个内容分页的方法来解决此问题:
代码:
<
div
id
="mycontent"
runat
="server"
style
="z-index: 101; left: 100px; 621px; position: absolute; top: 40px;
height: 160px"
>
</
div
>
<
div
id
="pager"
runat
="server"
align
="center"
style
="z-index: 101; left: 1px; 618px; position: absolute; top: 162px;
height: 34px"
>
</
div
>
前台页面两个DIV用来显示信息,mycontent用来显示新闻内容,pager 用来显示分页信息
protected
void
Page_Load(
object
sender, EventArgs e)
{
int
page
=
1
;
//
初始页面代码,一打开显示第一页内容
string
mypage;
mypage
=
Request.QueryString[
"
page
"
];
if
(mypage
!=
null
)
//
第一次的时候没有加载分页因此得到的结果为NULL,因此要判断一下
{
page
=
Convert.ToInt32(mypage);
}
if
(
!
Page.IsPostBack)
{
//
string strsql = "select top 1 * from pageContent where title='chen1'";
string
content
=
"
2008年4月9日(星期三)上午10时,国务院新闻办新闻发布厅举行新闻发布会,<P>请西藏自治区主席向巴平措、中共中央统战部副部长斯塔介绍近期西藏有关情况,并答记者问。<P>以下为发布会部分内容摘要。向巴平措:前一段时间,我在慰问无辜死亡人员的亲属、到医院去看望受伤人员,<P>与受到损害的商户座谈的时候一再表示,作为政府,作为自治区政府主席,没有能够对纳税人、对人民群众很好地加以保护,因此我表示深深的歉意,<P>而且明确表示,作为人民政府,对死亡人员,对受伤人员,政府会通过多种渠道妥善解决他们受到的损失和面临的困难问题。
"
;
//
以上内容可以从数据库中取得,这里为方便演示直接写上内容,用<P>来间隔要取得的内容
string
[] strContent
=
null
;
//
SqlDataReader dr = cdb.mydr(strsql); 如果从数据库里读的话
//
if (dr.Read())
//
{
strContent
=
filesplit(content);
//
调用filesplit方法取得数组形式的内容
//
}
//
dr.Close();
if
(strContent[page
-
1
]
!=
null
)
{
this
.mycontent.InnerHtml
=
strContent[page
-
1
].ToString();
//
显示内容
}
else
{
this
.mycontent.InnerHtml
=
"
无内容
"
;
}
string
adpager
=
string
.Empty;
for
(
int
i
=
0
; i
<
strContent.Length; i
++
)
{
//
循环数组取得内容分页数
if
(strContent[i]
!=
null
)
{
int
npage
=
i
+
1
;
adpager
+=
"
<a href=Default.aspx?page=
"
+
npage
+
"
>
"
+
npage
+
"
</a>
"
;
}
}
this
.pager.InnerHtml
=
adpager.ToString();
//
显示分页
}
}
public
string
[] filesplit(
string
contents)
{
int
fileindex
=
0
;
string
[] splitfile
=
new
string
[
10
];
while
(contents.Length
>
10
&&
fileindex
<
9
)
{
if
(contents.IndexOf(
"
<P>
"
,
10
)
<
0
)
break
;
splitfile[fileindex]
=
contents.Substring(
0
, contents.IndexOf(
"
<P>
"
,
10
));
//
这里注意这里的10是字数,我是为了测试而采用10,你可以根据你的新闻页面再设置,我想最少也得200字吧。呵呵。。。。。。
contents
=
contents.Remove(
0
, splitfile[fileindex].Length);
fileindex
++
;
}
splitfile[fileindex]
=
contents;
return
splitfile;
}
此文方法引荐自
http://blog.csdn.net/chenguang79/archive/2008/03/28/2224977.aspx
查看全文
相关阅读:
Oracle之sqlplus显示中文出现乱码
如何让谷歌取消自动重定向
装饰器模式
代理模式
适配器模式
protobuf接口调用报错:java.nio.charset.MalformedInputException: Input length = 1
本地tomcat调用远程接口报错:java.lang.reflect.InvocationTargetException
windows下安装weblogic
windows下使用linux命令搜文件
单例模式
原文地址:https://www.cnblogs.com/zjba2/p/1146374.html
最新文章
你的B计划在哪里?
有没有最安全的职业?
CentOS设置程序开机自启动的方法
GitLab版本管理
一键安装 Gitlab7 on RHEL6.4 并设置邮件发送
gitlab 6 安装备忘录
CentOS 7 安装 Gitlab
CentOS 7安装配置Redis数据库
centos/rhel下实现nginx自启动脚本实例
centos下nginx启动脚本和chkconfig管理
热门文章
./configure: error: the HTTP rewrite module requires the PCRE library解决
Linux Tools
Oracle之函数concat、lpad
Oracle之配置客户端登陆多个远程数据库
linux shell编程入门到精通
Oracle之批量生成数据
linux之mail
Oracle之标示符无效
linux之grub的使用
Oracle配置客户端
Copyright © 2011-2022 走看看