asp.net2.0 GridView的整理使用

<% @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >

< head  runat ="server" >
    
< title > 无标题页 </ title >
    
< meta  http-equiv ="Cache-Control" content ="no-cache, must-revalidate" />  
    
< link  href ="StyleSheet.css"  rel ="stylesheet"  type ="text/css"   />
    
< script  language ="javascript"  type ="text/javascript" >
    
function ButtonSelect()
    
{      
       
var chkall= document.all["chkAll"];
       
if(chkall.checked==false)
       
{
        chkall.setAttribute(
"checked","true");
      
       }

       
else
       
{
         chkall.checked 
= false;
         
       }

       AllSelect();
    }

    
function AllSelect()
    
{
      
var len=document.form1.elements.length;
    
var i;
    
var chkall= document.all["chkAll"];
     
if (chkall.checked == true)
     
{
            
for (i=0;i<len;i++)
            
{
                    
if (document.form1.elements[i].type=="checkbox")
                    
{  
                       
                      document.form1.elements[i].checked
=true;        
                    }

            }

        
     }

     
else
     

            
            
for (i=0;i<len;i++)
            
{
                    
if (document.form1.elements[i].type=="checkbox")
                    
{
                       document.form1.elements[i].checked
=false;        
                    }

            }

            
        
     }

    }

         
var k = <%=num%>
   
function checkgou()
   
{
       
var chkall= document.all["chkAll"];
        
var len=document.form1.elements.length;
        
var i;
        
var j=0;
      
            
for (i=0;i<len;i++)
            
{
                    
if (document.form1.elements[i].type=="checkbox")
                    
{   
                       
if (document.form1.elements[i].checked == true
                       
{
                          j
++;
                       }

                             
                    }

            }

            
if (j==k) {
              chkall.checked 
= true;
            }

            
//window.alert(len);
   }

    
</ script >

</ head >
< body  onload ="checkgou()" >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:GridView  ID ="GridView1"  runat ="server"  BackColor ="White"
            BorderColor
="#3366CC"  BorderStyle ="None"  BorderWidth ="1px"  CellPadding ="4"  Height ="200px"
            Width
="700px"  CaptionAlign ="Left"  AllowPaging ="True"  AllowSorting ="True"  OnSorting ="GridView1_Sorting"  HeaderStyle-Font-Underline ="true"  OnRowCreated ="GridView1_RowCreated" >
            
< FooterStyle  BackColor ="#99CCCC"  ForeColor ="#003399"   />
            
< RowStyle  BackColor ="White"  ForeColor ="#003399"  Font-Size ="Small"  HorizontalAlign ="Center"   />
            
< SelectedRowStyle  BackColor ="#009999"  Font-Bold ="True"  ForeColor ="#CCFF99"   />
            
< PagerStyle  BackColor ="#99CCCC"  ForeColor ="#003399"  HorizontalAlign ="Left"   />
            
< HeaderStyle  BackColor ="#003399"  Font-Bold ="True"  ForeColor ="#CCCCFF"  Font-Size ="Small"  Height ="20px"  Font-Underline ="True"  HorizontalAlign ="Center"  VerticalAlign ="Middle"   />
            
< EditRowStyle  Font-Size ="Small"  Height ="20px"  HorizontalAlign ="Center"   />
            
< AlternatingRowStyle  Font-Size ="Small"  ForeColor ="#00C000"  HorizontalAlign ="Center"   />
            
< PagerSettings  Visible ="False"   />
            
< Columns >
                
< asp:TemplateField >
                    
< HeaderTemplate >
                        
< input  id ="chkAll"  onclick ="AllSelect()"  type ="checkbox"   />
                    
</ HeaderTemplate >
                    
< ItemTemplate >
                        
< asp:CheckBox  ID ="CHK"  runat ="server"   />
                    
</ ItemTemplate >
                
</ asp:TemplateField >
            
</ Columns >
        
</ asp:GridView >
        
< asp:Button  ID ="Button1"  runat ="server"  BackColor ="#E0E0E0"  BorderStyle ="Groove"
            BorderWidth
="1px"  CommandArgument ="first"  ForeColor ="Red"  Text ="首页"  OnClick ="PageChange_Click"   />
        
< asp:Button  ID ="Button2"  runat ="server"  BackColor ="#E0E0E0"  BorderStyle ="Groove"
            BorderWidth
="1px"  CommandArgument ="prv"  ForeColor ="Red"  Text ="上一页"  OnClick ="PageChange_Click"   />
        
&nbsp;   &nbsp;
        
< asp:Button  ID ="Button3"  runat ="server"  BackColor ="#E0E0E0"  BorderStyle ="Groove"
            BorderWidth
="1px"  CommandArgument ="next"  ForeColor ="Red"  Text ="下一页"  OnClick ="PageChange_Click"   />
        
< asp:Button  ID ="Button4"  runat ="server"  BackColor ="#E0E0E0"  BorderStyle ="Groove"
            BorderWidth
="1px"  CommandArgument ="last"  ForeColor ="Red"  Text ="尾页"  OnClick ="PageChange_Click"   />
        
< asp:Label  ID ="Label1"  runat ="server"  Text ="Label"  Width ="350px" ></ asp:Label >
        
< asp:TextBox  ID ="sorttext"  runat ="server"  Visible ="False" ></ asp:TextBox >< br  />
        
< input  id ="Button6"  type ="button"  onclick ="ButtonSelect()"  value ="全选"   />
        
< asp:Button  ID ="Button5"  runat ="server"  BackColor ="#FFC0FF"  BorderColor ="#00C000"
            BorderStyle
="Solid"  BorderWidth ="1px"  OnClick ="Button5_Click"  Text ="删  除"   />
        
< asp:DropDownList  ID ="PageCountdrop"  runat ="server"  AutoPostBack ="True"  OnSelectedIndexChanged ="PageCountdrop_SelectedIndexChanged" >
        
</ asp:DropDownList ></ div >
    
</ form >
</ body >
</ html >

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  _Default : System.Web.UI.Page 
{
    
public int num;
    DataBase db 
= new DataBase();

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!this.IsPostBack)
        
{
            Button5.Attributes.Add(
"onclick""return confirm('确定要删除?')");
            Session[
"strSqlpara"= "";
           
            GVBind();
        }

        num 
= GridView1.PageSize;
    }

    
//GridView Binding
    private void GVBind()
    
{
        
string err="";
        
string sql = "SELECT MONITOR_ID as 设备号, MONITOR_SIM_ID as SIM卡号, TEMP_SELF as 导线温度, TEMP_CONNECTOR as 节点温度, E_CURRENT as 电流, RECEIVE_TIME as 采样时间, STATE as 状态 FROM CA_ALARM_RECORD ORDER BY RECEIVE_TIME";
        DataTable dt 
= db.GetTableSource(sql,ref err);
        
//GridView1.AllowPaging = true;
       
// GridView1.PageSize = 10;
        DataView dv = new DataView(dt);

        dv.Sort 
= sorttext.Text;

        GridView1.DataSource 
= dv;
      
        GridView1.DataBind();
       
        
int page =GridView1.PageIndex+1;
        
int pagecount=GridView1.PageCount;
        Label1.Text 
= "当前第"+page.ToString()+"页,总共 "+pagecount.ToString()+"";
        
if (GridView1.PageIndex == 0)
        
{
            Button1.Enabled 
= false;
            Button2.Enabled 
= false;
        }

        
else
        
{
            Button1.Enabled 
= true;
            Button2.Enabled 
= true;
        }

        
if (GridView1.PageIndex == GridView1.PageCount - 1)
        
{
            Button3.Enabled 
= false;
            Button4.Enabled 
= false;
        }

        
else
        
{
            Button3.Enabled 
= true;
            Button4.Enabled 
= true;
        }

        PageCountdrop.Items.Clear();
        DropDownBind();
    }

    
////dropdown的绑定显示控制页面跳转
    private void DropDownBind()
    
{
     
        
for (int i = 1; i <= this.GridView1.PageCount; i++
        

        
this.PageCountdrop.Items.Add(i.ToString()); 
        }
 
        
this.PageCountdrop.SelectedIndex = this.GridView1.PageIndex; 
      

    }

    
//分页函数
    protected void PageChange_Click(object source , EventArgs e)
    
{
        
/*保存checked*/
        
/********************************************************************************/
        
string strSqlpara;
        strSqlpara 
= Convert.ToString(Session["strSqlpara"]);


        
for (int i = 0; i < GridView1.Rows.Count;i++ )
        
{
            CheckBox chk 
= (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CHK");
            
if (strSqlpara.IndexOf(GridView1.Rows[i].Cells[6].Text) != -1)
            
{
                
if (chk.Checked == false)
                
{
                    strSqlpara 
= strSqlpara.Replace("|'" + GridView1.Rows[i].Cells[6].Text + "'""");
                   
                }

            }

            
else
            
{
                
if (chk.Checked == true)
                
{
                    strSqlpara 
= strSqlpara + "|'" + GridView1.Rows[i].Cells[6].Text + "'";
                   
                }

            }

        }

        Session[
"strSqlpara"= strSqlpara;
       
/********************************************************************************/
        
string Arg = ((Button)source).CommandArgument.ToString();
        
switch(Arg)
        
{
            
case "first":
                GridView1.PageIndex 
= 0;
                
break;
            
case "prv":
                GridView1.PageIndex 
= GridView1.PageIndex - 1;
                
break;
            
case "next":
                GridView1.PageIndex 
= GridView1.PageIndex + 1;
                
break;
            
case "last":
                GridView1.PageIndex 
= GridView1.PageCount - 1;
                
break;
            
default:
                  
break;
        }

        GVBind();
        
/********************************************************************************/
        
/*换页后的循环判断*/
        
for (int p = 0; p < GridView1.Rows.Count;p++ )
        
{

            CheckBox chk 
= (System.Web.UI.WebControls.CheckBox)GridView1.Rows[p].Cells[0].FindControl("CHK");
            
if (strSqlpara.IndexOf(GridView1.Rows[p].Cells[6].Text) != -1)
                chk.Checked 
= true;
        }

 
    }



    
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    
{
        
string expre = e.SortExpression;
        
if (sorttext.Text.IndexOf("desc"== -1)
        
{
            sorttext.Text 
= e.SortExpression + " desc";
        }

        
else
        
{
            sorttext.Text 
= e.SortExpression + " asc";
        }

        GVBind();
    }

    
protected void Button5_Click(object sender, EventArgs e)  //删除选中的
    {
        
bool kk=false;
        
for (int i = 0; i < GridView1.Rows.Count; i++)
        
{
            CheckBox chk 
= (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CHK");
            
            
if (chk.Checked == true)
            
{
                
string err ="";
                
string id = GridView1.Rows[i].Cells[6].Text;   //这里这个是日期,我是根据日期进行del
                string sql = "delete from CA_ALARM_RECORD where RECEIVE_TIME=to_date('"+id+"','yyyy-mm-dd hh24:mi:ss')";
               kk 
= db.ExceSql(sql, ref err);
              
            }

        }

        GVBind();
        
if (kk)
        
{
            
string sScript = "<Script>alert('删除成功!')</Script>";
            Response.Write(sScript);
        }

    }

    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) //鼠标动作
    {
        


    }

    
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Attributes.Add(
"onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#e6e6fa';");
            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=c;");
        }

    }

    
protected void PageCountdrop_SelectedIndexChanged(object sender, EventArgs e)
    
{
        GridView1.PageIndex 
= PageCountdrop.SelectedIndex;
        GVBind();
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT流渊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值