会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
暮江独钓
知人者智,自知者明; 胜人者有力,自胜者强; 知足者富,强行者有志; 不失其所者久;死而不亡者寿.
博客园
首页
新随笔
联系
管理
订阅
[GridView控件]自定义分页
<
tr
>
<
td align
=
"
center
"
valign
=
"
middle
"
><
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AllowPaging
=
"
True
"
AutoGenerateColumns
=
"
False
"
Width
=
"
600px
"
PageSize
=
"
10
"
OnRowDataBound
=
"
GridView1_RowDataBound
"
>
<
Columns
>
<
asp:TemplateField HeaderText
=
"
序号
"
>
<
ControlStyle Width
=
"
10%
"
/>
<
ItemStyle Width
=
"
10%
"
/>
<
ItemTemplate
>
<%
#(Container.DataItemIndex
+
1
).ToString()
%>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:BoundField DataField
=
"
ID1
"
HeaderText
=
"
ID
"
Visible
=
"
false
"
/>
<
asp:BoundField DataField
=
"
NewsTitle1
"
HeaderText
=
"
新闻标题
"
SortExpression
=
"
NewsTitle1
"
>
<
ControlStyle Font
-
Underline
=
"
True
"
/>
<
ItemStyle HorizontalAlign
=
"
Left
"
Width
=
"
56%
"
/>
<
FooterStyle Font
-
Strikeout
=
"
False
"
HorizontalAlign
=
"
Left
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
NewsDateTime1
"
HeaderText
=
"
时间
"
SortExpression
=
"
NewsDateTime1
"
>
<
ItemStyle Width
=
"
12%
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
ItemID1
"
HeaderText
=
"
ItemID1
"
Visible
=
"
false
"
/>
</
Columns
>
<
EmptyDataTemplate
>
该栏目暂时没有数据
</
EmptyDataTemplate
>
<
PagerStyle BackColor
=
"
Transparent
"
BorderColor
=
"
White
"
ForeColor
=
"
DarkGray
"
/>
<
PagerSettings Visible
=
"
false
"
/>
</
asp:GridView
>
<
table cellpadding
=
"
0
"
cellspacing
=
"
0
"
border
=
"
0
"
>
<
tr
>
<
td align
=
"
center
"
style
=
"
height: 25px; width: 569px;
"
>
<
asp:LinkButton ID
=
"
btnFirst
"
CommandArgument
=
"
first
"
OnClick
=
"
PagerButtonClick
"
runat
=
"
server
"
>
首 页
</
asp:LinkButton
>&
nbsp;
<
asp:LinkButton ID
=
"
btnPrev
"
CommandArgument
=
"
prev
"
OnClick
=
"
PagerButtonClick
"
runat
=
"
server
"
>
上一页
</
asp:LinkButton
>
<
asp:LinkButton ID
=
"
btnNext
"
CommandArgument
=
"
next
"
OnClick
=
"
PagerButtonClick
"
runat
=
"
server
"
>
下一页
</
asp:LinkButton
>
<
asp:LinkButton ID
=
"
btnLast
"
CommandArgument
=
"
last
"
OnClick
=
"
PagerButtonClick
"
runat
=
"
server
"
>
尾 页
</
asp:LinkButton
>
<
asp:Label ID
=
"
LblCurrentIndex
"
runat
=
"
server
"
></
asp:Label
>
<
asp:Label ID
=
"
LblPageCount
"
runat
=
"
server
"
></
asp:Label
>
<
asp:Label ID
=
"
LblRecordCount
"
runat
=
"
server
"
></
asp:Label
>
跳转到第
<
asp:DropDownList ID
=
"
ddlCurrentPage
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
OnSelectedIndexChanged
=
"
DropDownList1_SelectedIndexChanged
"
>
</
asp:DropDownList
>
页
</
td
>
<
tr
></
table
>
</
td
>
</
tr
>
private
void
BindGrid()
{
//
绑定GRIDVIEW
string
sql
=
""
;
News2006.Conn1 con
=
new
News2006.Conn1();
System.Data.SqlClient.SqlConnection con1
=
new
System.Data.SqlClient.SqlConnection(con.sqlconnstr());
if
(ItemBool(
int
.Parse(Session[
"
item
"
].ToString())))
{
sql
=
""
;
}
else
{
sql
=
""
;
}
System.Data.SqlClient.SqlCommand mycommand1
=
new
System.Data.SqlClient.SqlCommand(sql, con1);
mycommand1.Parameters.Add(
"
@par1
"
, System.Data.SqlDbType.Int,
4
).Value
=
int
.Parse(item)
System.Data.SqlClient.SqlDataAdapter ada
=
new
System.Data.SqlClient.SqlDataAdapter(mycommand1);
con1.Open();
System.Data.DataSet ds
=
new
DataSet();
ada.Fill(ds,
"
News1
"
);
this
.GridView1.DataSource
=
ds.Tables[
"
News1
"
].DefaultView;
this
.GridView1.DataBind();
LblCurrentIndex.Text
=
"
第
"
+
(GridView1.PageIndex
+
1
).ToString()
+
"
页
"
;
LblPageCount.Text
=
"
共
"
+
GridView1.PageCount.ToString()
+
"
页
"
;
LblRecordCount.Text
=
"
总共
"
+
ds.Tables[
0
].Rows.Count.ToString()
+
"
条
"
;
if
(ds.Tables[
0
].Rows.Count
==
0
)
{
btnFirst.Visible
=
false
;
btnPrev.Visible
=
false
;
btnNext.Visible
=
false
;
btnLast.Visible
=
false
;
LblCurrentIndex.Visible
=
false
;
LblPageCount.Visible
=
false
;
LblRecordCount.Visible
=
false
;
}
else
if
(GridView1.PageCount
==
1
)
{
btnFirst.Visible
=
false
;
btnPrev.Visible
=
false
;
btnNext.Visible
=
false
;
btnLast.Visible
=
false
;
}
//
计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
btnFirst.CommandName
=
"
1
"
;
btnPrev.CommandName
=
(GridView1.PageIndex
==
0
?
"
1
"
: GridView1.PageIndex.ToString());
btnNext.CommandName
=
(GridView1.PageCount
==
1
?
GridView1.PageCount.ToString() : (GridView1.PageIndex
+
2
).ToString());
btnLast.CommandName
=
GridView1.PageCount.ToString();
this
.ddlCurrentPage.Items.Clear();
for
(
int
i
=
1
; i
<=
this
.GridView1.PageCount; i
++
)
{
this
.ddlCurrentPage.Items.Add(i.ToString());
}
this
.ddlCurrentPage.SelectedIndex
=
this
.GridView1.PageIndex;
con1.Close();
}
protected
void
PagerButtonClick(
object
sender, EventArgs e)
{
GridView1.PageIndex
=
Convert.ToInt32(((LinkButton)sender).CommandName)
-
1
;
BindGrid();
}
该分页除了首页,上一页,下一页,未页外还有一个Dropdownlist.......代码都帖出来了.....自己细细的看
posted @
2007-07-23 15:52
暮江独钓
阅读(
366
) 评论(
1
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告