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

        testdatagrid.aspx页面代码: 
  1datagrid单选多选_html_02datagrid单选多选_html_03<%datagrid单选多选_sql_04@ Page language="c#" Codebehind="testdatagrid.aspx.cs" AutoEventWireup="false" Inherits="localhost.fenpage.testdatagrid" %>
  2datagrid单选多选_鼠标移动_05<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
  3datagrid单选多选_鼠标移动_05<HTML>
  4datagrid单选多选_鼠标移动_05    <HEAD>
  5datagrid单选多选_鼠标移动_05        <title></title>
  6datagrid单选多选_鼠标移动_05        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  7datagrid单选多选_鼠标移动_05        <meta name="CODE_LANGUAGE" Content="C#">
  8datagrid单选多选_鼠标移动_05        <meta name="vs_defaultClientScript" content="JavaScript">
  9datagrid单选多选_鼠标移动_05        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 10datagrid单选多选_html_02datagrid单选多选_html_03        <script language="javascript">datagrid单选多选_sql_04
 11datagrid单选多选_sql_16        //全部选中
 12datagrid单选多选_sql_16        function QuanXuan_Click()
 13datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19        datagrid单选多选_sql_04{
 14datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19            if (document.Form1.checkboxname.length)datagrid单选多选_sql_04{
 15datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19                for (var i=0;i<document.Form1.checkboxname.length;i++)datagrid单选多选_sql_04{
 16datagrid单选多选_sql_16                    document.Form1.checkboxname[i].checked = true;
 17datagrid单选多选_i++_28                }

 18datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19            }
elsedatagrid单选多选_sql_04{
 19datagrid单选多选_sql_16                document.Form1.checkboxname.checked = true;
 20datagrid单选多选_i++_28            }

 21datagrid单选多选_i++_28        }

 22datagrid单选多选_sql_16        
 23datagrid单选多选_sql_16        //取消选中
 24datagrid单选多选_sql_16        function QuXiao_Click()
 25datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19        datagrid单选多选_sql_04{
 26datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19            if (document.Form1.checkboxname.length)datagrid单选多选_sql_04{
 27datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19                for (var i=0;i<document.Form1.checkboxname.length;i++)datagrid单选多选_sql_04{
 28datagrid单选多选_sql_16                    document.Form1.checkboxname[i].checked = false;
 29datagrid单选多选_i++_28                }

 30datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19            }
elsedatagrid单选多选_sql_04{
 31datagrid单选多选_sql_16                document.Form1.checkboxname.checked = false;
 32datagrid单选多选_i++_28            }

 33datagrid单选多选_i++_28        }

 34datagrid单选多选_sql_16        
 35datagrid单选多选_sql_16        ///////////////判断没有选中的返回false
 36datagrid单选多选_sql_16        function slcNo_click()
 37datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19        datagrid单选多选_sql_04{
 38datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19            if (document.Form1.checkboxname.length)datagrid单选多选_sql_04{
 39datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19                for (var i=0;i<document.Form1.checkboxname.length;i++)datagrid单选多选_sql_04{
 40datagrid单选多选_sql_16                    if(document.Form1.checkboxname[i].checked)
 41datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19                    datagrid单选多选_sql_04{
 42datagrid单选多选_sql_16                        return true;
 43datagrid单选多选_i++_28                    }

 44datagrid单选多选_i++_28                }

 45datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19            }
elsedatagrid单选多选_sql_04{
 46datagrid单选多选_sql_16                if(document.Form1.checkboxname.checked)
 47datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19                datagrid单选多选_sql_04{
 48datagrid单选多选_sql_16                    return true;
 49datagrid单选多选_i++_28                }

 50datagrid单选多选_i++_28            }

 51datagrid单选多选_sql_16            alert("请选择后再操作!");
 52datagrid单选多选_sql_16            return false;
 53datagrid单选多选_i++_28        }

 54datagrid单选多选_sql_16        
 55datagrid单选多选_sql_16        //////////////////////////////////////改变行的颜色
 56datagrid单选多选_sql_16        if (!objbeforeItem)
 57datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19        datagrid单选多选_sql_04{
 58datagrid单选多选_sql_16            var objbeforeItem=null;
 59datagrid单选多选_sql_16            var objbeforeItembackgroundColor=null;
 60datagrid单选多选_i++_28        }

 61datagrid单选多选_sql_16        function ItemOver(obj)
 62datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19        datagrid单选多选_sql_04{
 63datagrid单选多选_sql_16            objbeforeItembackgroundColor=obj.style.backgroundColor;
 64datagrid单选多选_sql_16            obj.style.backgroundColor="#B9D1F3";                                        
 65datagrid单选多选_sql_16            objbeforeItem=obj;
 66datagrid单选多选_i++_28        }

 67datagrid单选多选_sql_16        
 68datagrid单选多选_sql_16        function ItemOut(obj)
 69datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19        datagrid单选多选_sql_04{            
 70datagrid单选多选_sql_16            if(objbeforeItem)
 71datagrid单选多选_窗体设计器_18datagrid单选多选_sql_19            datagrid单选多选_sql_04{
 72datagrid单选多选_sql_16                objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
 73datagrid单选多选_i++_28            }
    
 74datagrid单选多选_i++_28        }

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

        testdatagrid.aspx.cs页面代码:
  1datagrid单选多选_鼠标移动_181using System;
  2datagrid单选多选_鼠标移动_181using System.Collections;
  3datagrid单选多选_鼠标移动_181using System.ComponentModel;
  4datagrid单选多选_鼠标移动_181using System.Data;
  5datagrid单选多选_鼠标移动_181using System.Data.SqlClient;
  6datagrid单选多选_鼠标移动_181using System.Drawing;
  7datagrid单选多选_鼠标移动_181using System.Web;
  8datagrid单选多选_鼠标移动_181using System.Web.SessionState;
  9datagrid单选多选_鼠标移动_181using System.Web.UI;
 10datagrid单选多选_鼠标移动_181using System.Web.UI.WebControls;
 11datagrid单选多选_鼠标移动_181using System.Web.UI.HtmlControls;
 12datagrid单选多选_鼠标移动_181using System.Configuration;
 13datagrid单选多选_鼠标移动_181
 14datagrid单选多选_鼠标移动_181namespace localhost.fenpage
 15datagrid单选多选_sql_195datagrid单选多选_i++_196datagrid单选多选_sql_197{
 16datagrid单选多选_html_198datagrid单选多选_窗体设计器_199    /**//// <summary>
 17datagrid单选多选_i++_200    /// testdgRadio 的摘要说明。
 18datagrid单选多选_html_201    /// </summary>

 19datagrid单选多选_i++_200    public class testdatagrid : System.Web.UI.Page
 20datagrid单选多选_html_198datagrid单选多选_窗体设计器_199    datagrid单选多选_sql_197{
 21datagrid单选多选_i++_200        protected System.Web.UI.WebControls.RadioButton RadioButton1;
 22datagrid单选多选_i++_200        protected System.Web.UI.WebControls.Button Button1;
 23datagrid单选多选_i++_200        protected System.Web.UI.WebControls.Button Button2;
 24datagrid单选多选_i++_200        protected System.Web.UI.WebControls.DataGrid MyDataGrid;
 25datagrid单选多选_i++_200    
 26datagrid单选多选_i++_200        private void Page_Load(object sender, System.EventArgs e)
 27datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{            
 28datagrid单选多选_i++_200            if (!IsPostBack)
 29datagrid单选多选_html_198datagrid单选多选_窗体设计器_199            datagrid单选多选_sql_197{
 30datagrid单选多选_i++_200                BindGrid();
 31datagrid单选多选_html_201            }

 32datagrid单选多选_i++_200
 33datagrid单选多选_i++_200            Button2.Attributes.Add("OnClick","return slcNo_click();");
 34datagrid单选多选_html_201        }

 35datagrid单选多选_i++_200
 36datagrid单选多选_i++_200        public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
 37datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{
 38datagrid单选多选_i++_200            MyDataGrid.CurrentPageIndex = e.NewPageIndex;
 39datagrid单选多选_i++_200            BindGrid();
 40datagrid单选多选_html_201        }

 41datagrid单选多选_i++_200
 42datagrid单选多选_i++_200        private void BindGrid()
 43datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{
 44datagrid单选多选_i++_200            string strSql="";
 45datagrid单选多选_i++_200            DataSet ds  = new DataSet();
 46datagrid单选多选_i++_200            
 47datagrid单选多选_i++_200            strSql="Select * from testDg";
 48datagrid单选多选_i++_200            SqlConnection conn = new  SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);  
 49datagrid单选多选_i++_200            if (conn.State.ToString() == "Closed")
 50datagrid单选多选_html_198datagrid单选多选_窗体设计器_199            datagrid单选多选_sql_197{
 51datagrid单选多选_i++_200                conn.Open();
 52datagrid单选多选_html_201            }

 53datagrid单选多选_i++_200            
 54datagrid单选多选_i++_200            SqlDataAdapter Da  = new SqlDataAdapter(strSql, conn);
 55datagrid单选多选_i++_200            Da.Fill(ds, "testdg");
 56datagrid单选多选_i++_200            MyDataGrid.DataSource = ds.Tables["testdg"].DefaultView;
 57datagrid单选多选_i++_200            MyDataGrid.DataBind();
 58datagrid单选多选_i++_200            
 59datagrid单选多选_i++_200            Da.Dispose();
 60datagrid单选多选_i++_200            if(conn.State.ToString()=="Open")
 61datagrid单选多选_html_198datagrid单选多选_窗体设计器_199            datagrid单选多选_sql_197{
 62datagrid单选多选_i++_200                conn.Close();
 63datagrid单选多选_html_201            }

 64datagrid单选多选_i++_200            conn.Dispose();
 65datagrid单选多选_html_201        }

 66datagrid单选多选_i++_200
 67datagrid单选多选_i++_200        private void MyDataGrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 68datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{
 69datagrid单选多选_html_198datagrid单选多选_窗体设计器_199            //配合前台脚本实现鼠标移动到每行上变颜色#region    //配合前台脚本实现鼠标移动到每行上变颜色
 70datagrid单选多选_i++_200            if(e.Item.ItemIndex>=0)
 71datagrid单选多选_html_198datagrid单选多选_窗体设计器_199            datagrid单选多选_sql_197{
 72datagrid单选多选_html_198datagrid单选多选_窗体设计器_199                e.Item.Attributes.Add("onmouseover","ItemOver(this)");/**///////在每行上增加脚本处理 onmouseover
 73datagrid单选多选_html_198datagrid单选多选_窗体设计器_199                e.Item.Attributes.Add("onmouseout","ItemOut(this)");/**///////在每行上增加脚本处理 onmouseout
 74datagrid单选多选_html_201            }

 75datagrid单选多选_html_201            #endregion

 76datagrid单选多选_html_201        }

 77datagrid单选多选_i++_200
 78datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 79datagrid单选多选_i++_200        override protected void OnInit(EventArgs e)
 80datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{
 81datagrid单选多选_i++_200            //
 82datagrid单选多选_i++_200            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 83datagrid单选多选_i++_200            //
 84datagrid单选多选_i++_200            InitializeComponent();
 85datagrid单选多选_i++_200            base.OnInit(e);
 86datagrid单选多选_html_201        }

 87datagrid单选多选_i++_200        
 88datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        /**//// <summary>
 89datagrid单选多选_i++_200        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 90datagrid单选多选_i++_200        /// 此方法的内容。
 91datagrid单选多选_html_201        /// </summary>

 92datagrid单选多选_i++_200        private void InitializeComponent()
 93datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{    
 94datagrid单选多选_i++_200            this.MyDataGrid.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.MyDataGrid_ItemCreated);
 95datagrid单选多选_i++_200            this.Button1.Click += new System.EventHandler(this.Button1_Click);
 96datagrid单选多选_i++_200            this.Button2.Click += new System.EventHandler(this.Button2_Click);
 97datagrid单选多选_i++_200            this.Load += new System.EventHandler(this.Page_Load);
 98datagrid单选多选_i++_200
 99datagrid单选多选_html_201        }

100datagrid单选多选_html_201        #endregion

101datagrid单选多选_i++_200
102datagrid单选多选_i++_200        private void Button1_Click(object sender, System.EventArgs e)
103datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{
104datagrid单选多选_i++_200            Page.Response.Write(Page.Request.Form.Get("RadioName"));
105datagrid单选多选_html_201        }

106datagrid单选多选_i++_200
107datagrid单选多选_i++_200        private void Button2_Click(object sender, System.EventArgs e)
108datagrid单选多选_html_198datagrid单选多选_窗体设计器_199        datagrid单选多选_sql_197{
109datagrid单选多选_i++_200            string str="";
110datagrid单选多选_i++_200            string []ckb=null;
111datagrid单选多选_i++_200
112datagrid单选多选_i++_200            str=Page.Request.Form.Get("checkboxname");
113datagrid单选多选_html_198datagrid单选多选_窗体设计器_199            ckb=str.Split(new char[]datagrid单选多选_sql_197{','});
114datagrid单选多选_i++_200
115datagrid单选多选_i++_200            Page.Response.Write("直接在页面中得到的值为:"+str+"<br>");
116datagrid单选多选_i++_200
117datagrid单选多选_i++_200            Page.Response.Write("处理后存放在数组中,如下:<br>");
118datagrid单选多选_i++_200            for(int i=0;i<ckb.Length;i++)
119datagrid单选多选_html_198datagrid单选多选_窗体设计器_199            datagrid单选多选_sql_197{
120datagrid单选多选_i++_200                Page.Response.Write("ckb["+i+"]的值为:"+ckb[i]+"<br>");
121datagrid单选多选_html_201            }

122datagrid单选多选_html_201        }

123datagrid单选多选_html_201    }

124datagrid单选多选_i++_342}

125datagrid单选多选_鼠标移动_181

        生成数据库表用到的sql语句:
datagrid单选多选_鼠标移动_05CREATE TABLE [dbo].[testDg] (
datagrid单选多选_鼠标移动_05    
[id] [decimal](180IDENTITY (11NOT NULL ,
datagrid单选多选_鼠标移动_05    
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
datagrid单选多选_鼠标移动_05    
[card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
datagrid单选多选_鼠标移动_05    
[price] [numeric](182NULL ,
datagrid单选多选_鼠标移动_05    
[shijian] [datetime] NULL 
datagrid单选多选_鼠标移动_05
ON [PRIMARY]
datagrid单选多选_鼠标移动_05
GO
datagrid单选多选_鼠标移动_05
datagrid单选多选_鼠标移动_05
ALTER TABLE [dbo].[testDg] ADD 
datagrid单选多选_鼠标移动_05    
CONSTRAINT [DF_testDg_shijian] DEFAULT (getdate()) FOR [shijian],
datagrid单选多选_鼠标移动_05    
CONSTRAINT [PK_testDg] PRIMARY KEY  CLUSTERED 
datagrid单选多选_鼠标移动_05    (
datagrid单选多选_鼠标移动_05        
[id]
datagrid单选多选_鼠标移动_05    )  
ON [PRIMARY] 
datagrid单选多选_鼠标移动_05
GO