简单的asp.net gridview模板
来源:互联网 发布:php是做什么用的 编辑:程序博客网 时间:2024/05/21 08:55
在项目中,经常会用到表格展示数据的地方,包含分页。
在asp.net 中我们经常用的表格就是GridView控件。以下介绍此控件的一般用法。
代码如下:首先是HTML 前台代码。
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>
这样可以运用自带的分页,但当页面比较大是,性能会下降很大,所以可以在取数据的时间就只取一页数据,绑定到此控件。因只有一页数据,这样自带分页就会无法分页。
所以我们可以通过计算并传递分页信息到分页模板(PagerTemplate),来生成分页。
如如下:即传入PageIndex和PageCount,此变量为后台代码中的public变量。基中pagecount 可以
public int PageCount
{
get
{
int pageMode = DataItemCount % PageSize;
if (pageMode == 0)
{
pageCount = DataItemCount / PageSize;
}
else
{
pageCount = DataItemCount / PageSize + 1;
}
return pageCount;
}
}保存在后台。
<PagerTemplate>
页 <asp:Label ID="lblPageIndex" runat="server" Text='<%# PageIndex + 1 %>'></asp:Label>
/ <asp:Label ID="lblPageCount" runat="server" Text='<%# PageCount %>'></asp:Label>
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" Enabled="<%# PageIndex!=0 %>"
CommandName="Page" Text="最前" CommandArgument="first" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" Enabled="<%# PageIndex!=0 %>"
CommandName="Page" Text="上一页" CommandArgument="prev" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" Enabled="<%# PageIndex!=PageCount-1 %>"
CommandName="Page" Text="下一页" CommandArgument="next" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" Enabled="<%# PageIndex!=PageCount-1 %>"
CommandName="Page" Text="最后" CommandArgument="last" OnClick="BtnChangePage_Click">
</asp:LinkButton>
<asp:TextBox ID="txtNewPageIndex" runat="server" Text='<%# PageIndex + 1 %>'
Width="40px" 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="至" OnClick="BtnChangePage_Click"></asp:LinkButton>
</PagerTemplate>
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();
}
//选中用户
private void SetCheckedItem(GridView gv)
{
Dictionary<string, string> diction = null;
if (ViewState["CheckedItem"] == null)
{
diction = new Dictionary<string, string>();
//ViewState["CheckedItem"] = diction;
}
else
{
diction = (Dictionary<string, string>)ViewState["CheckedItem"];
}
for (int index = 0; index < gv.Rows.Count; index++)
{
System.Web.UI.HtmlControls.HtmlInputCheckBox chkBox = gv.Rows[index].FindControl("chkSelect") as System.Web.UI.HtmlControls.HtmlInputCheckBox;
if (chkBox != null)
{
string checkedId = gv.DataKeys[index].Value.ToString();
if (chkBox.Checked)
{
if (!diction.ContainsKey(checkedId))
{
diction.Add(checkedId, checkedId);
}
}
else
{
if (diction.ContainsKey(checkedId))
{
diction.Remove(checkedId);
}
}
}
}
ViewState["CheckedItem"] = diction;
}
//注意 要导出gridview, 在vs2010版中要加上这样的代码
public override void VerifyRenderingInServerForm(Control control){
}
- 简单的asp.net gridview模板
- 【asp.net】GridView中模板的使用
- ASP.NET 2.0 中动态添加 GridView 模板列的...
- ASP.net datalist,gridview 编辑模板里面控件的使用;
- ASP.net中模板的简单使用
- asp.net 完整GridView 分页 模板
- asp c# .net GridView模板列
- asp.net GridView排序简单实现
- asp.net linq 简单 绑定到 gridview
- ASP.NET中GridView使用简单攻略
- asp.net 第五天 html简单的模板
- asp.net模板引擎:C#中Nvelocity的简单使用
- ASP.NET 2.0的 GridView中模板列绑定DropDownList的数据处理
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- C# 中的 Infinity 和 NaN
- IT职场人生系列之五:怎样面试
- .net站点配置完后常见报错及解决措施
- php session实现原理
- 如何提高自己的编程水平
- 简单的asp.net gridview模板
- strategy, formula
- [转]Android编译环境(6) - Android JNI实例
- poj 2892 || hdu 1540 Tunnel Warfare
- 大牛地址收藏
- MFC集合类2
- 不要一辈子靠技术生存
- 时间片调度在单片机中的运用
- PHP+MYSQL+SESSION+ThinkPHP实现简单购物车功能