GridView绝技和CheckBox结合、鼠标到某一行改变背景色(2种方法)、删除时弹出确认对话框

5.GridView和CheckBox结合:

效果图:

后台代码:
1 using  System;
 2
using  System.Data;
 3
using  System.Configuration;
 4
using  System.Web;
 5
using  System.Web.Security;
 6
using  System.Web.UI;
 7
using  System.Web.UI.WebControls;
 8
using  System.Web.UI.WebControls.WebParts;
 9
using  System.Web.UI.HtmlControls;
10
using  System.Data.SqlClient;
11
12
public   partial   class  Default5 : System.Web.UI.Page
13 {
14 //清清月儿http://blog.csdn.net/21aspnet 
15    SqlConnection sqlcon;
16    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17    protected void Page_Load(object sender, EventArgs e)
18    {
19        if (!IsPostBack)
20        {
21            bind();
22        }

23    }

24    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
25    {
26        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
27        {
28            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
29            if (CheckBox2.Checked == true)
30            {
31                cbox.Checked = true;
32            }

33            else
34            {
35                cbox.Checked = false;
36            }

37        }

38    }

39    protected void Button2_Click(object sender, EventArgs e)
40    {
41        sqlcon = new SqlConnection(strCon);
42        SqlCommand sqlcom;
43        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
44        {
45            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
46            if (cbox.Checked == true)
47            {
48
49                string sqlstr = "delete from 飞狐工作室 where 身份证号码='" + GridView1.DataKeys[i].Value + "'";
50                sqlcom = new SqlCommand(sqlstr, sqlcon);
51                sqlcon.Open();
52                sqlcom.ExecuteNonQuery();
53                sqlcon.Close();
54            }

55        }

56        bind();
57    }

58    protected void Button1_Click(object sender, EventArgs e)
59    {
60        CheckBox2.Checked = false;
61        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
62        {
63            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
64            cbox.Checked = false;
65        }

66    }

67    public void bind()
68    {
69        string sqlstr = "select top 5 * from 飞狐工作室";
70        sqlcon = new SqlConnection(strCon);
71        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
72        DataSet myds = new DataSet();
73        sqlcon.Open();
74        myda.Fill(myds, "tb_Member");
75        GridView1.DataSource = myds;
76        GridView1.DataKeyNames = new string[] "身份证号码" };
77        GridView1.DataBind();
78        sqlcon.Close();
79    }

80}

前台主要代码:
1 < asp:GridView ID = " GridView1 "  runat = " server "  AllowSorting = " True "  AutoGenerateColumns = " False "
 
2     CellPadding = " 3 "  Font - Size = " 9pt "   BackColor = " White "  BorderColor = " #CCCCCC "  BorderStyle = " None "  BorderWidth = " 1px " >
 
3      < FooterStyle BackColor = " White "  ForeColor = " #000066 "   />
 
4      < Columns >
 
5           < asp:TemplateField >
 
6              < ItemTemplate >
 
7                  < asp:CheckBox ID = " CheckBox1 "  runat = " server "   />
 
8              </ ItemTemplate >
 
9          </ asp:TemplateField >
10           < asp:BoundField DataField = " 身份证号码 "  HeaderText = " 用户ID "  SortExpression = " 身份证号码 "   />
11          < asp:BoundField DataField = " 姓名 "  HeaderText = " 用户姓名 "  SortExpression = " 姓名 " />
12         
13          < asp:BoundField DataField = " 家庭住址 "  HeaderText = " 家庭住址 "  SortExpression = " 家庭住址 " />
14             
15      </ Columns >
16      < RowStyle ForeColor = " #000066 "   />
17      < SelectedRowStyle BackColor = " #669999 "  Font - Bold = " True "  ForeColor = " White "   />
18      < PagerStyle BackColor = " White "  ForeColor = " #000066 "  HorizontalAlign = " Left "   />
19      < HeaderStyle BackColor = " #006699 "  Font - Bold = " True "  ForeColor = " White "   />
20 </ asp:GridView >
21   < asp:CheckBox ID = " CheckBox2 "  runat = " server "  AutoPostBack = " True "  Font - Size = " 9pt "  OnCheckedChanged = " CheckBox2_CheckedChanged "
22     Text = " 全选 "   />
23 < asp:Button ID = " Button1 "  runat = " server "  Font - Size = " 9pt "  Text = " 取消 "  OnClick = " Button1_Click "   />
24 < asp:Button ID = " Button2 "  runat = " server "  Font - Size = " 9pt "  Text = " 删除 "  OnClick = " Button2_Click "   />
25
26

 

6.鼠标移到GridView某一行时改变该行的背景色方法一:

效果图:

做法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:

 

1 protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
 
2 {
 
3    int i;
 
4    //执行循环,保证每条数据都可以更新
 5    for (i = 0; i < GridView1.Rows.Count; i++)
 
6    {
 
7        //首先判断是否是数据行
 8        if (e.Row.RowType == DataControlRowType.DataRow)
 
9        {
10            //当鼠标停留时更改背景色
11            e.Row.Attributes.Add("onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
12            //当鼠标移开时还原背景色
13            e.Row.Attributes.Add("onmouseout""this.style.backgroundColor=c");
14        }

15    }

16
17}

 

前台代码:

 

  1 < html xmlns = " http://www.w3.org/1999/xhtml "   >
 
2 < head runat = " server " >
 
3      < title > 实现鼠标划过改变GridView的行背景色 清清月儿http: // blog.csdn.net/21aspnet </title>
  4 </ head >
 
5 < body >
 
6      < form id = " form1 "  runat = " server " >
 
7      < div >
 
8          < asp:GridView ID = " GridView1 "  runat = " server "  AutoGenerateColumns = " False "  DataKeyNames = " 身份证号码 "
 
9             DataSourceID = " SqlDataSource1 "  AllowSorting = " True "  BackColor = " White "  BorderColor = " #CCCCCC "  BorderStyle = " None "  BorderWidth = " 1px "  CellPadding = " 3 "  Font - Size = " 12px "  OnRowDataBound = " GridView1_RowDataBound " >
10              < Columns >
11                  < asp:BoundField DataField = " 身份证号码 "  HeaderText = " 身份证号码 "  ReadOnly = " True "  SortExpression = " 身份证号码 "   />
12                  < asp:BoundField DataField = " 姓名 "  HeaderText = " 姓名 "  SortExpression = " 姓名 "   />
13                  < asp:BoundField DataField = " 家庭住址 "  HeaderText = " 家庭住址 "  SortExpression = " 家庭住址 "   />
14                  < asp:BoundField DataField = " 邮政编码 "  HeaderText = " 邮政编码 "  SortExpression = " 邮政编码 "   />
15              </ Columns >
16              < FooterStyle BackColor = " White "  ForeColor = " #000066 "   />
17              < RowStyle ForeColor = " #000066 "   />
18              < SelectedRowStyle BackColor = " #669999 "  Font - Bold = " True "  ForeColor = " White "   />
19              < PagerStyle BackColor = " White "  ForeColor = " #000066 "  HorizontalAlign = " Left "   />
20              < HeaderStyle BackColor = " #006699 "  Font - Bold = " True "  ForeColor = " White "   />
21          </ asp:GridView >
22          < asp:SqlDataSource ID = " SqlDataSource1 "  runat = " server "  ConnectionString = " <%$ ConnectionStrings:北风贸易ConnectionString1 %> "
23             SelectCommand = " SELECT top 5 [身份证号码], [姓名], [员工性别], [家庭住址], [邮政编码] FROM [飞狐工作室] "  DataSourceMode = " DataReader " ></ asp:SqlDataSource >
24     
25      </ div >
26      </ form >
27 </ body >
28 </ html >

 

7.鼠标移到GridView某一行时改变该行的背景色方法二:

效果图:

做法:和上面的一样就是代码不同
 1 protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
 
2 {
 
3    //int i;
 4    /**/////执行循环,保证每条数据都可以更新
 5    //for (i = 0; i < GridView1.Rows.Count; i++)
 6    //{
 7    //    //首先判断是否是数据行
 8    //    if (e.Row.RowType == DataControlRowType.DataRow)
 9    //    {
10    //        //当鼠标停留时更改背景色
11    //        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
12    //        //当鼠标移开时还原背景色
13    //        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
14    //    }
15    //}
16    //如果是绑定数据行 
17    if (e.Row.RowType == DataControlRowType.DataRow)
18    {
19        //鼠标经过时,行背景色变 
20        e.Row.Attributes.Add("onmouseover""this.style.backgroundColor='#E6F5FA'");
21        //鼠标移出时,行背景色变 
22        e.Row.Attributes.Add("onmouseout""this.style.backgroundColor='#FFFFFF'");
23    }

24}

8.GridView实现删除时弹出确认对话框:

效果图:

实现方法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:

 

 1 protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
 
2 {
 
3    //如果是绑定数据行 
 4    if (e.Row.RowType == DataControlRowType.DataRow)
 
5    {
 
6         if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
 
7        {
 
8            ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick""javascript:return confirm('你确认要删除:"" + e.Row.Cells[1].Text + ""吗?')");
 
9        }

10    }
 
11}

12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值