datagrid单选多选

 关于datagrid的几点简单应用,功能包括radio的单选,checkbox的多选,鼠标移动到行上颜色变深,链接传递多参数等功能。这里radio和checkbox都使用的是HTML控件。
        有关checkbox的几个功能:全部选种、取消选种、没选中操作的检测等,这几个均使用js脚本实现。
    
        运行效果图: 
             

        testdatagrid.aspx页面代码: 
  1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="testdatagrid.aspx.cs" AutoEventWireup="false" Inherits="localhost.fenpage.testdatagrid"  %>
  2 None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
  3 None.gif < HTML >
  4 None.gif     < HEAD >
  5 None.gif         < title ></ title >
  6 None.gif         < meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
  7 None.gif         < meta  name ="CODE_LANGUAGE"  Content ="C#" >
  8 None.gif         < meta  name ="vs_defaultClientScript"  content ="JavaScript" >
  9 None.gif         < meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
 10 ExpandedBlockStart.gifContractedBlock.gif         < script  language ="javascript" > dot.gif
 11InBlock.gif        //全部选中
 12InBlock.gif        function QuanXuan_Click()
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 14ExpandedSubBlockStart.gifContractedSubBlock.gif            if (document.Form1.checkboxname.length)dot.gif{
 15ExpandedSubBlockStart.gifContractedSubBlock.gif                for (var i=0;i<document.Form1.checkboxname.length;i++)dot.gif{
 16InBlock.gif                    document.Form1.checkboxname[i].checked = true;
 17ExpandedSubBlockEnd.gif                }

 18ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
 19InBlock.gif                document.Form1.checkboxname.checked = true;
 20ExpandedSubBlockEnd.gif            }

 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif        
 23InBlock.gif        //取消选中
 24InBlock.gif        function QuXiao_Click()
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            if (document.Form1.checkboxname.length)dot.gif{
 27ExpandedSubBlockStart.gifContractedSubBlock.gif                for (var i=0;i<document.Form1.checkboxname.length;i++)dot.gif{
 28InBlock.gif                    document.Form1.checkboxname[i].checked = false;
 29ExpandedSubBlockEnd.gif                }

 30ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
 31InBlock.gif                document.Form1.checkboxname.checked = false;
 32ExpandedSubBlockEnd.gif            }

 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif        
 35InBlock.gif        ///判断没有选中的返回false
 36InBlock.gif        function slcNo_click()
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            if (document.Form1.checkboxname.length)dot.gif{
 39ExpandedSubBlockStart.gifContractedSubBlock.gif                for (var i=0;i<document.Form1.checkboxname.length;i++)dot.gif{
 40InBlock.gif                    if(document.Form1.checkboxname[i].checked)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 42InBlock.gif                        return true;
 43ExpandedSubBlockEnd.gif                    }

 44ExpandedSubBlockEnd.gif                }

 45ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
 46InBlock.gif                if(document.Form1.checkboxname.checked)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 48InBlock.gif                    return true;
 49ExpandedSubBlockEnd.gif                }

 50ExpandedSubBlockEnd.gif            }

 51InBlock.gif            alert("请选择后再操作!");
 52InBlock.gif            return false;
 53ExpandedSubBlockEnd.gif        }

 54InBlock.gif        
 55InBlock.gif        //改变行的颜色
 56InBlock.gif        if (!objbeforeItem)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            var objbeforeItem=null;
 59InBlock.gif            var objbeforeItembackgroundColor=null;
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif        function ItemOver(obj)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63InBlock.gif            objbeforeItembackgroundColor=obj.style.backgroundColor;
 64InBlock.gif            obj.style.backgroundColor="#B9D1F3";                                        
 65InBlock.gif            objbeforeItem=obj;
 66ExpandedSubBlockEnd.gif        }

 67InBlock.gif        
 68InBlock.gif        function ItemOut(obj)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
 70InBlock.gif            if(objbeforeItem)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 72InBlock.gif                objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
 73ExpandedSubBlockEnd.gif            }
    
 74ExpandedSubBlockEnd.gif        }

 75ExpandedBlockEnd.gif        
</ script >
 76 None.gif     </ HEAD >
 77 None.gif     < body  MS_POSITIONING ="GridLayout" >
 78 None.gif         < form  id ="Form1"  method ="post"  runat ="server" >
 79 None.gif             < table >
 80 None.gif                 < tr >
 81 None.gif                     < td >< FONT  face ="宋体" >< asp:datagrid  id ="MyDataGrid"  runat ="server"  Width ="550px"  AllowPaging ="True"  AutoGenerateColumns ="False"
 82 None.gif                                OnPageIndexChanged ="MyDataGrid_Page"  PageSize ="10"  CellPadding ="2"  HorizontalAlign ="Left"  BorderColor ="Gray"
 83 None.gif                                Font-Size ="14px"  Font-Names ="新宋体" >
 84 None.gif                                 < AlternatingItemStyle  BackColor ="#F2F2F2" ></ AlternatingItemStyle >
 85 None.gif                                 < ItemStyle  Wrap ="False"  HorizontalAlign ="Left"  Height ="22px"  VerticalAlign ="Middle" ></ ItemStyle >
 86 None.gif                                 < HeaderStyle  Wrap ="False"  Height ="25px"  BackColor ="#DEE6F6" ></ HeaderStyle >
 87 None.gif                                 < Columns >
 88 None.gif                                     < asp:BoundColumn  Visible ="False"  DataField ="id"  SortExpression ="id"  HeaderText ="id" ></ asp:BoundColumn >
 89 None.gif                                     < asp:TemplateColumn >
 90 None.gif                                         < HeaderTemplate >
 91 None.gif                                            选择
 92 None.gif                                         </ HeaderTemplate >
 93 None.gif                                         < ItemTemplate >
 94 None.gif                                             < input  type =radio  name ="RadioName"  value ='<%#  DataBinder.Eval(Container.DataItem, "id")% > '/>
 95 None.gif                                         </ ItemTemplate >
 96 None.gif                                     </ asp:TemplateColumn >
 97 None.gif                                     < asp:TemplateColumn >
 98 None.gif                                         < HeaderTemplate >
 99 None.gif                                            选择
100 None.gif                                         </ HeaderTemplate >
101 None.gif                                         < ItemTemplate >
102 None.gif                                             < INPUT  type ="checkbox"  name ="checkboxname"  value ='<%#  DataBinder.Eval(Container.DataItem, "id")% > '>
103 None.gif                                         </ ItemTemplate >
104 None.gif                                     </ asp:TemplateColumn >
105 None.gif                                     < asp:TemplateColumn >
106 None.gif                                         < HeaderTemplate >
107 None.gif                                            模板列
108 None.gif                                         </ HeaderTemplate >
109 None.gif                                         < ItemTemplate >
110 None.gif                                             < asp:Label  ID ="lblId"  Runat ="server"  Text ='' >
111 None.gif                                             </ asp:Label >
112 ExpandedBlockStart.gifContractedBlock.gif                                             < href ="abc.aspx?id=<%#DataBinder.Eval(Container.DataItem,  " id") % > &name = <% dot.gif #DataBinder.Eval(Container.DataItem,  "name" %> " target="_blank">连接 </ a >
113 None.gif                                         </ ItemTemplate >
114 None.gif                                     </ asp:TemplateColumn >
115 None.gif                                     < asp:HyperLinkColumn  DataNavigateUrlField ="id"  DataNavigateUrlFormatString ="aa.aspx?id={0}"  DataTextField ="name"
116 None.gif                                        SortExpression ="name"  HeaderText ="姓名"  Target ="_blank" >
117 None.gif                                     </ asp:HyperLinkColumn >
118 None.gif                                     < asp:BoundColumn  DataField ="card"  SortExpression ="card"  HeaderText ="身份证号"  DataFormatString ="{0:yyyy-MM-dd hh:mm:ss}" ></ asp:BoundColumn >
119 None.gif                                     < asp:BoundColumn  DataField ="price"  SortExpression ="price"  HeaderText ="价格" ></ asp:BoundColumn >
120 None.gif                                     < asp:BoundColumn  DataField ="shijian"  SortExpression ="shijian"  HeaderText ="shijian"  DataFormatString ="{0:yyyy年MM月dd日}" ></ asp:BoundColumn >
121 None.gif                                 </ Columns >
122 None.gif                                 < PagerStyle  Mode ="NumericPages" ></ PagerStyle >
123 None.gif                             </ asp:datagrid ></ FONT ></ td >
124 None.gif                 </ tr >
125 None.gif                 < tr >
126 None.gif                     < td >
127 None.gif                         < asp:Button  id ="Button1"  runat ="server"  Text ="radio得到选择的行" ></ asp:Button >
128 None.gif                         < INPUT  type ="button"  value ="全部选中"  onclick ="QuanXuan_Click()" >
129 None.gif                         < INPUT  type ="button"  value ="取消选中"  onclick ="QuXiao_Click()" >
130 None.gif                         < asp:Button  id ="Button2"  runat ="server"  Text ="checkbox得到选择的行" ></ asp:Button >
131 None.gif                     </ td >
132 None.gif                 </ tr >
133 None.gif             </ table >
134 None.gif         </ form >
135 None.gif     </ body >
136 None.gif </ HTML >
137 None.gif

        testdatagrid.aspx.cs页面代码:
  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Data;
  5 None.gif using  System.Data.SqlClient;
  6 None.gif using  System.Drawing;
  7 None.gif using  System.Web;
  8 None.gif using  System.Web.SessionState;
  9 None.gif using  System.Web.UI;
 10 None.gif using  System.Web.UI.WebControls;
 11 None.gif using  System.Web.UI.HtmlControls;
 12 None.gif using  System.Configuration;
 13 None.gif
 14 None.gif namespace  localhost.fenpage
 15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 17InBlock.gif    /// testdgRadio 的摘要说明。
 18ExpandedSubBlockEnd.gif    /// </summary>

 19InBlock.gif    public class testdatagrid : System.Web.UI.Page
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 21InBlock.gif        protected System.Web.UI.WebControls.RadioButton RadioButton1;
 22InBlock.gif        protected System.Web.UI.WebControls.Button Button1;
 23InBlock.gif        protected System.Web.UI.WebControls.Button Button2;
 24InBlock.gif        protected System.Web.UI.WebControls.DataGrid MyDataGrid;
 25InBlock.gif    
 26InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
 28InBlock.gif            if (!IsPostBack)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 30InBlock.gif                BindGrid();
 31ExpandedSubBlockEnd.gif            }

 32InBlock.gif
 33InBlock.gif            Button2.Attributes.Add("OnClick","return slcNo_click();");
 34ExpandedSubBlockEnd.gif        }

 35InBlock.gif
 36InBlock.gif        public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38InBlock.gif            MyDataGrid.CurrentPageIndex = e.NewPageIndex;
 39InBlock.gif            BindGrid();
 40ExpandedSubBlockEnd.gif        }

 41InBlock.gif
 42InBlock.gif        private void BindGrid()
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            string strSql="";
 45InBlock.gif            DataSet ds  = new DataSet();
 46InBlock.gif            
 47InBlock.gif            strSql="Select * from testDg";
 48InBlock.gif            SqlConnection conn = new  SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);  
 49InBlock.gif            if (conn.State.ToString() == "Closed")
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 51InBlock.gif                conn.Open();
 52ExpandedSubBlockEnd.gif            }

 53InBlock.gif            
 54InBlock.gif            SqlDataAdapter Da  = new SqlDataAdapter(strSql, conn);
 55InBlock.gif            Da.Fill(ds, "testdg");
 56InBlock.gif            MyDataGrid.DataSource = ds.Tables["testdg"].DefaultView;
 57InBlock.gif            MyDataGrid.DataBind();
 58InBlock.gif            
 59InBlock.gif            Da.Dispose();
 60InBlock.gif            if(conn.State.ToString()=="Open")
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 62InBlock.gif                conn.Close();
 63ExpandedSubBlockEnd.gif            }

 64InBlock.gif            conn.Dispose();
 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif
 67InBlock.gif        private void MyDataGrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            //配合前台脚本实现鼠标移动到每行上变颜色#region    //配合前台脚本实现鼠标移动到每行上变颜色
 70InBlock.gif            if(e.Item.ItemIndex>=0)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 72ExpandedSubBlockStart.gifContractedSubBlock.gif                e.Item.Attributes.Add("onmouseover","ItemOver(this)");/**///在每行上增加脚本处理 onmouseover
 73ExpandedSubBlockStart.gifContractedSubBlock.gif                e.Item.Attributes.Add("onmouseout","ItemOut(this)");/**///在每行上增加脚本处理 onmouseout
 74ExpandedSubBlockEnd.gif            }

 75ExpandedSubBlockEnd.gif            #endregion

 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 79InBlock.gif        override protected void OnInit(EventArgs e)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            //
 82InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 83InBlock.gif            //
 84InBlock.gif            InitializeComponent();
 85InBlock.gif            base.OnInit(e);
 86ExpandedSubBlockEnd.gif        }

 87InBlock.gif        
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 89InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 90InBlock.gif        /// 此方法的内容。
 91ExpandedSubBlockEnd.gif        /// </summary>

 92InBlock.gif        private void InitializeComponent()
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 94InBlock.gif            this.MyDataGrid.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.MyDataGrid_ItemCreated);
 95InBlock.gif            this.Button1.Click += new System.EventHandler(this.Button1_Click);
 96InBlock.gif            this.Button2.Click += new System.EventHandler(this.Button2_Click);
 97InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 98InBlock.gif
 99ExpandedSubBlockEnd.gif        }

100ExpandedSubBlockEnd.gif        #endregion

101InBlock.gif
102InBlock.gif        private void Button1_Click(object sender, System.EventArgs e)
103ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
104InBlock.gif            Page.Response.Write(Page.Request.Form.Get("RadioName"));
105ExpandedSubBlockEnd.gif        }

106InBlock.gif
107InBlock.gif        private void Button2_Click(object sender, System.EventArgs e)
108ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
109InBlock.gif            string str="";
110InBlock.gif            string []ckb=null;
111InBlock.gif
112InBlock.gif            str=Page.Request.Form.Get("checkboxname");
113ExpandedSubBlockStart.gifContractedSubBlock.gif            ckb=str.Split(new char[]dot.gif{','});
114InBlock.gif
115InBlock.gif            Page.Response.Write("直接在页面中得到的值为:"+str+"<br>");
116InBlock.gif
117InBlock.gif            Page.Response.Write("处理后存放在数组中,如下:<br>");
118InBlock.gif            for(int i=0;i<ckb.Length;i++)
119ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
120InBlock.gif                Page.Response.Write("ckb["+i+"]的值为:"+ckb[i]+"<br>");
121ExpandedSubBlockEnd.gif            }

122ExpandedSubBlockEnd.gif        }

123ExpandedSubBlockEnd.gif    }

124ExpandedBlockEnd.gif}

125 None.gif

        生成数据库表用到的sql语句:
None.gif CREATE   TABLE   [ dbo ] . [ testDg ]  (
None.gif    
[ id ]   [ decimal ] ( 18 0 IDENTITY  ( 1 1 NOT   NULL  ,
None.gif    
[ name ]   [ varchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
None.gif    
[ card ]   [ varchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
None.gif    
[ price ]   [ numeric ] ( 18 2 NULL  ,
None.gif    
[ shijian ]   [ datetime ]   NULL  
None.gif
ON   [ PRIMARY ]
None.gif
GO
None.gif
None.gif
ALTER   TABLE   [ dbo ] . [ testDg ]   ADD  
None.gif    
CONSTRAINT   [ DF_testDg_shijian ]   DEFAULT  ( getdate ())  FOR   [ shijian ] ,
None.gif    
CONSTRAINT   [ PK_testDg ]   PRIMARY   KEY    CLUSTERED  
None.gif    (
None.gif        
[ id ]
None.gif    )  
ON   [ PRIMARY ]  
None.gif
GO

转载于:https://www.cnblogs.com/hzuIT/articles/472795.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值