zoukankan      html  css  js  c++  java
  • Asp.net--GridView控件--(1)高亮显示当前所在行,(2)高亮显示单击行,(3)绑定数据库数据,(4)分页,(5)导出到excel表格,(6)首列插入序号

    //本页代码中的DB类及方法在http://www.cnblogs.com/Deerjiadelu/p/7252769.html中能查询到

    (1)Asp.net--GridView控件--高亮显示当前所在行

    protected void gvquery_DataBound(object sender, GridViewRowEventArgs e)
        {
            //高亮显示光标所在行
            if (e.Row.RowType == DataControlRowType.DataRow)//判断当前行是不是数据绑定行
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6495ED'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
                
    
            }
          
        }

    (2).net--GridView控件--高亮显示单击行

     protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            foreach (GridViewRow gvr in this.gvquery.Rows)//高亮显示选择行
            {
                if (gvr.RowType != DataControlRowType.DataRow) return;
                if (gvr.RowState.HasFlag(DataControlRowState.Selected) == false)
                {
                    gvr.Attributes.Add("style", "background-color:none");
                    gvr.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvquery, "Select$" + gvr.RowIndex, true);
                }//ondblclick双击
                else
                {
                    gvr.Attributes.Add("style", "background-color:#F7CE90;");
                }
            }
            base.Render(writer);
        }

    (3)Asp.net--GridView控件--绑定数据库数据

       protected void Page_Load(object sender, EventArgs e)
        {
            gvDataInit();//gv绑定数据                
         }
    public void gvDataInit()//绑定gv数据
        {
            //获取查询条件
            string strsid = this.tbsmid.Text.Trim();
            string strsname = this.tbsmname.Text.Trim();
            string strsex = this.ddlsmsex.SelectedValue.ToString().Trim();
            if (strsex == "不限")
            {
                strsex = "";
            }
            string strage = this.tbsmage.Text.Trim();
            string strprovince = this.ddlsmprovince.SelectedValue.ToString().Trim();
            if (strprovince == "不限")
            {
                strprovince = "";
            }
            string streducation = this.ddlsmeducation.SelectedValue.ToString().Trim();
            if (streducation == "不限")
            {
                streducation = "";
            }
            string strgraduation = this.tbsmgraduation.Text.Trim();
            
    
            DB db = new DB();
            string strtitle = "sid as 工号,sname as 姓名,sex as 性别,age as 年龄,province as 省份,education as 学历,graduation as 毕业院校,lmdate as 维护日期,lmtime as 维护时间,lmuser as 维护人";
    
    //模糊条件查询--当条件为空时忽略此项属性
            string strcontion = "(sid='" + strsid + "' or '" + strsid + "' ='')and (sname=N'" + strsname + "' or '" + strsname + "'='')and(sex=N'" + strsex + "' or '" + strsex + "'='')and(age='" + strage + "' or '" + strage + "'='')and(province=N'" + strprovince + "' or '" + strprovince + "'='')and(education=N'" + streducation + "' or '" + streducation + "'='') and (graduation like N'%" + strgraduation + "%' or '" + strgraduation + "'='')and(active='Y')";
            string str = "select " + strtitle + " from staffInfo  where " + strcontion + "";
            if (db.sqlEx(str) == 1)
            {
                this.gvquery.DataSource = db.reDt(str);
                this.gvquery.DataBind();
                if (this.gvquery.Rows.Count == 0)
                    Response.Write("<script>alert('查无此人!请重新输入查询条件!');location='StaffManage.aspx'</script>");
    
            }
        }

    (4)Asp.net--GridView控件--分页

    添加三个属性:

    AllowPaging="true"

    PageSize="10"
    onpageindexchanging="gvquery_PageIndexChanging"

     protected void gvquery_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvquery.PageIndex = e.NewPageIndex;
            gvDataInit();
        }

     (5)Asp.net--GridView控件--导出到excel表格

     protected void btsmexcel_Click(object sender, EventArgs e)//导出
        {
            gvquery.AllowPaging = false;
            gvDataInit();
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=filename.xls");
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter strWt = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(strWt);
            gvquery.RenderControl(htw);
            Response.Write(strWt.ToString());
            Response.Flush();
            Response.End();
            gvquery.AllowPaging = true;
            gvDataInit();
        }
    
      public override void VerifyRenderingInServerForm(Control control)//excel 必须重载此方法
        {
            //gridwiew必须放在具有 runat=server 的窗体标记内
            //确保该控件显示在 HtmlForm 控件的开始和结束标记之间
        }

    (6)Asp.net--GridView控件--首列插入序号

     protected void gvquery_DataBound(object sender, GridViewRowEventArgs e)
        {
    
            if (e.Row.RowIndex >= 0)//序号
            {
                e.Row.Cells[0].Text = Convert.ToString(e.Row.DataItemIndex + 1);
              
            }
    
        }
  • 相关阅读:
    生产环境elk
    命令行操作es(转)
    makedown在线生成工具
    网站渗透测试工具zap2docker-stable
    ansible常用单模块
    程序员应该怎样和领导相处(转)
    Css设置毛玻璃
    tar.gz 文件解压
    Ubuntu:命令行安装可视化界面
    汇编语言开发总结
  • 原文地址:https://www.cnblogs.com/Deerjiadelu/p/7252901.html
Copyright © 2011-2022 走看看