HTML:
<div style="overflow-x:scroll; overflow-y:auto;">
<asp:GridView ID="gvList" runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
onrowcancelingedit="gvList_RowCancelingEdit"
onrowediting="gvList_RowEditing" onrowupdated="gvList_RowUpdated"
onrowupdating="gvList_RowUpdating" >
<Columns>
<asp:TemplateField HeaderStyle-CssClass="chkCell" ItemStyle-CssClass="chkCell">
<HeaderTemplate>
<input type="checkbox" id="chkBoxAll" runat="server" onclick="OnSelectAll(this);" title="全選|全清" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chkSelect" runat="server" class="chkBox" onclick="ChkItemSelect(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="FieldName" DataField="" ReadOnly="true"/>
<asp:CommandField ButtonType="Link" EditText="edit" ShowEditButton="true"
CancelText="cancel" UpdateText="update" />
</Columns>
<PagerTemplate>
Page <asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'></asp:Label>
of <asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label>
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>"
CommandName="Page" Text="First" CommandArgument="first" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>"
CommandName="Page" Text="Previous" CommandArgument="prev" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>"
CommandName="Page" Text="Next" CommandArgument="next" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>"
CommandName="Page" Text="Last" CommandArgument="last" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:TextBox ID="txtNewPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'
Width="20px" MaxLength="10" onkeypress="CheckKeyPress(event)" onpaste="ForbiddenPaste(event);" oncontextmenu="ForbiddenContextMenu(event);"></asp:TextBox>
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="go"
CommandName="Page" Text="GO" OnClick="BtnChangePage_Click"></asp:LinkButton>
</PagerTemplate>
</asp:GridView>
</div>
JS: CheckBox全選功能。
/*check box selected and unselected Start.*/
//select all checkbox by default when page loads.
$(document).ready(function () {
var chkAll = $("[id$=chkBoxAll]");
chkAll.click();
OnSelectAll(chkAll);
});
//click Select All check box.
function OnSelectAll(obj) {
var chkFlag = $(obj).attr("checked");
$(".chkBox").attr("checked", chkFlag);
}
//click item check box.
function ChkItemSelect(obj) {
var chkFlag = $(obj).attr("checked");
if (chkFlag) {
if ($(".chkBox:checked").length == $(".chkBox").length) {
$("[id$=chkBoxAll]").attr("checked", true);
}
} else {
$("[id$=chkBoxAll]").attr("checked", false);
}
}
/*check box selected and unselected End.*/
//check keyPress for input dagit
function CheckKeyPress(evt) {
var currentKey = evt.charCode || event.keyCode;
if (currentKey < 48 || currentKey > 57) {
if (window.event) {
window.event.returnValue = false;
} else {
if (currentKey != 8) {
evt.preventDefault();
}
}
}
}
//forbidden right key menu.
function ForbiddenContextMenu() {
if (window.event) {
window.event.returnValue = false;
} else {
arguments[0].preventDefault();
}
}
//forbidden paste.
function ForbiddenPaste() {
if (window.event) {
window.event.returnValue = false;
} else {
arguments[0].preventDefault();
arguments[0].stopPropagation();
}
}
.CS
//binding
private void BindGridviewList()
{
//bind gridview.
Object obj = new Object();
List<ObjectEntity> List =Object.GetList();
this.gvList.DataSource = List;
this.gvList.DataKeyNames = new string[1] { "Id" };
this.gvList.DataBind();
}
//paging
public void GridviewPaging(object sender, GridView gv)
{
switch (((LinkButton)sender).CommandArgument.ToString())
{
case "first":
gv.PageIndex = 0;
break;
case "last":
gv.PageIndex = gv.PageCount - 1;
break;
case "prev":
if (gv.PageIndex != 0)
{ gv.PageIndex = gv.PageIndex - 1; }
break;
case "next":
gv.PageIndex = gv.PageIndex + 1;
break;
case "go":
{
GridViewRow gvr = gv.BottomPagerRow;
TextBox txtNewPageIndex = (TextBox)gvr.FindControl("txtNewPageIndex");
if (!string.IsNullOrEmpty(txtNewPageIndex.Text.Trim()))
{
int res = Convert.ToInt32(txtNewPageIndex.Text.ToString());
if (res < 1)
{
res = 1;
}
else if (res > gv.PageCount)
{
res = gv.PageCount;
}
else
{
gv.PageIndex = res - 1;
}
}
}
break;
}
}
protected void BtnChangePage_Click(object sender, EventArgs e)
{
GridviewPaging(sender, this.gvList);
BindList();
}
protected void gvList_RowEditing(object sender, GridViewEditEventArgs e)
{
this.gvList.EditIndex = e.NewEditIndex;
BindList();
}
protected void gvList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.gvList.EditIndex = -1;
BindList();
}
//注意 要匯出gridview, 在vs2010版中要加上這樣的代碼
public override void VerifyRenderingInServerForm(Control control)
{
}