Simple Asp.net gridview Template

Source: Internet
Author: User

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 = "select all | clear all"/>
</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 & nbsp; <asp: Label id = "lblpageindex" runat = "server" text = '<% # (gridview) container. parent. parent ). pageindex + 1% & gt; '& gt; </ASP: Label>
& Nbsp; of & nbsp; <asp: Label id = "lblpagecount" runat = "server" text = '<% # (gridview) container. parent. parent ). pagecount %> '> </ASP: Label> & nbsp;
<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: select all functions of the 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 ();
}

// Note that you need to export the gridview and add this code in vs2010.

Public override void verifyrenderinginserverform (Control)
{
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.