前台:
<asp:GridView ID="gvUserList" runat="server" CellPadding="0" BorderWidth="1px" SkinID="gvSkin"
Width="100%" BorderStyle="Solid" BorderColor="#7A97BF" EmptyDataText="目前尚未配置人员"
AutoGenerateColumns="False" HorizontalAlign="Center" EnableModelValidation="True"
PageSize="8" AllowPaging="true" OnPageIndexChanging="gvUserList_PageIndexChanging"
OnRowDataBound="gvUserList_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSelectUser" runat="server" />
<asp:HiddenField ID="hidUserID" runat="server" Value='<%#Eval("UserID")%>' />
</ItemTemplate>
<ItemStyle ForeColor="#225C62" BorderColor="#7A97BF" BorderStyle="Solid" BorderWidth="1px" />
</asp:TemplateField>
<asp:BoundField HeaderText="账号" HeaderStyle-ForeColor="White" ItemStyle-Width="100px" DataField="UserID" SortExpression="UserID">
<ItemStyle ForeColor="#000000" BorderColor="#7A97BF" BorderStyle="Solid" BorderWidth="1px"
Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="全名" HeaderStyle-ForeColor="White" ItemStyle-Width="120px" DataField="FullName" SortExpression="FullName">
<ItemStyle ForeColor="#225C62" BorderColor="#7A97BF" BorderStyle="Solid" BorderWidth="1px"
Width="70px" />
</asp:BoundField>
<asp:BoundField HeaderText="邮件" HeaderStyle-ForeColor="White" ItemStyle-Width="100px" DataField="EmailAddress" SortExpression="EmailAddress">
<ItemStyle Wrap="True" ForeColor="#225C62" BorderColor="#7A97BF" BorderStyle="Solid"
BorderWidth="1px" Width="80px" />
</asp:BoundField>
<asp:BoundField HeaderText="部门" HeaderStyle-ForeColor="White" ItemStyle-Width="160px" DataField="DepartmentName" SortExpression="DepartmentName">
<ItemStyle Wrap="True" ForeColor="#225C62" BorderColor="#7A97BF" BorderStyle="Solid"
BorderWidth="1px" />
</asp:BoundField>
<asp:BoundField HeaderText="职位" HeaderStyle-ForeColor="White" ItemStyle-Width="160px" DataField="PositionName" SortExpression="PositionName">
<ItemStyle Wrap="True" ForeColor="#225C62" BorderColor="#7A97BF" BorderStyle="Solid"
BorderWidth="1px" />
</asp:BoundField>
</Columns>
<PagerTemplate>
<table align="center" style="font-size: 10pt; width: auto">
<tr>
<td>
<asp:Label ID="LabelCurrentPage" runat="server" Font-Size="10pt">当前页:<%# ((GridView)Container.NamingContainer).PageIndex + 1 %></asp:Label>
</td>
<td>
<asp:Label ID="LabelPageCount" runat="server" Font-Size="10pt">总页数:<%# ((GridView)Container.NamingContainer).PageCount %></asp:Label>
</td>
<td>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CausesValidation="False"
CommandArgument="First" CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"
Font-Size="10pt">首页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CausesValidation="False"
CommandArgument="Prev" CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"
Font-Size="10pt">上一页</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CausesValidation="False" CommandArgument="Next"
CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>"
Font-Size="10pt">下一页</asp:LinkButton>
</td>
<td style="width: 29px">
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CausesValidation="False" CommandArgument="Last"
CommandName="Page" Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>"
Font-Size="10pt">尾页</asp:LinkButton>
</td>
<td>
<asp:Label ID="dd" runat="server" Font-Size="10pt" Height="18px" Text="跳到:"></asp:Label>
<asp:DropDownList ID="pagetiao" runat="server" AutoPostBack="True" EnableViewState="True"
Font-Size="10pt" OnInit="pagetiao_Init" OnSelectedIndexChanged="pagetiao_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
</table>
</PagerTemplate>
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle Font-Size="9pt" HorizontalAlign="Center" BorderColor="#7A97BF" BorderStyle="Solid"
BorderWidth="1px" Height="20px" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<EmptyDataRowStyle HorizontalAlign="Center" BackColor="White" />
<SelectedRowStyle BackColor="#F8F5D2" ForeColor="#184584" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
后台:
/// <summary>
/// 设置Grid的配置信息
/// </summary>
/// <创建标识>王瑶</创建标识>
/// <创建时间>2011年9月14日</创建时间>
private void InitAddedUserGrid(DataTable dt)
{
this.gvAddedUserList.DataSource = dt;
this.gvAddedUserList.DataBind();
}
protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)//绑定控件gvUserList
{
if (e.NewPageIndex >= 0)
{
this.gvUserList.PageIndex = e.NewPageIndex;
}
InitGrid();
}
protected void pagetiao_Init(object sender, EventArgs e)//pagetiao就是分页中的下拉列表框
{
DropDownList list = (DropDownList)sender;
for (int i = 1, j = gvUserList.PageCount; i <= j; i++)
{
list.Items.Add(new ListItem(i.ToString()));
}
list.SelectedIndex = gvUserList.PageIndex;
}
protected void pagetiao_SelectedIndexChanged(object sender, EventArgs e)//pagetiao就是分页中的下拉列表框
{
DropDownList list = (DropDownList)sender;
gvUserList.PageIndex = list.SelectedIndex;
InitGrid();
}
protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)//判断是否为数据绑定行
{
//鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#FFCC66'");
//鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
//选择变色
//e.Row.Attributes.Add("onclick", "this.style.backgroundColor='red'");
}
}