GridView中动态交换列和JS控制选择CheckBox行变颜色

来源:互联网 发布:焊锡烟雾净化器淘宝 编辑:程序博客网 时间:2024/04/28 06:01

最近看了下,在GridView中动态交换列,结合自己找到点资料,感觉这方法不错,另外简单写了个JS控制的,使用CheckBox(html)来控制选中GridView的行,就改变颜色,方法可能不是很好,只是记录下这个思路。

下面是cs代码:

  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         if (!IsPostBack)
  4.         {
  5.             ViewState["SortOrder"] = "APP_ID";
  6.             ViewState["SortDire"] = "ASC";
  7.             BindData();
  8.         }
  9.     }
  10.     protected void BindData()
  11.     {
  12.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DS_DEMOConnectionString"].ToString());
  13.         SqlCommand com = new SqlCommand("SP_APP_Select", con);
  14.         com.CommandType = CommandType.StoredProcedure;
  15.         SqlDataAdapter dataAdpa = new SqlDataAdapter(com);
  16.         DataSet ds = new DataSet();
  17.         dataAdpa.Fill(ds, "Test");
  18.         gvTest.DataSource = ds;
  19.         gvTest.DataBind();
  20.         ViewState["ds"] = ds;
  21.     }
  22.     protected void btnChange_Click(object sender, EventArgs e)
  23.     {
  24.         try
  25.         {
  26.             DataSet ds = ViewState["ds"as DataSet;
  27.             int index = int.Parse(txtIndex.Text.Trim()) - 1;
  28.             if (index < gvTest.Columns.Count)
  29.             {
  30.                 DataControlField dcf = gvTest.Columns[index];
  31.                 gvTest.Columns.RemoveAt(index);
  32.                 gvTest.Columns.Insert(0, dcf);
  33.                 gvTest.DataSource = ds;
  34.                 gvTest.DataBind();
  35.                 ViewState["ds"] = ds;
  36.             }
  37.             else
  38.             {
  39.             }
  40.         }
  41.         catch
  42.         { }
  43.         finally
  44.         { }
  45.     }

 

下面是页面代码:

  1. <html xmlns="http://www.w3.org/1999/xhtml" >
  2. <head runat="server">
  3.     <title>无标题页</title>
  4.      <script language="javascript" type="text/javascript"> 
  5.    function checkme()
  6.    {
  7.        var cbkLength = document.getElementsByName("cbkname").length;
  8.        var colors = Array(cbkLength);
  9.        for(var i = 0;i < cbkLength; ++ i)
  10.        {
  11.           if(document.getElementsByName("cbkname")[i].checked)
  12.           {
  13.               //colors[i] = document.getElementById("gvTest").rows[i+1].style.backgroundColor;
  14.               document.getElementById("gvTest").rows[i+1].style.backgroundColor='blue';
  15.           }
  16.           else
  17.           {
  18.               document.getElementById("gvTest").rows[i+1].style.backgroundColor='';//colors[i];
  19.           }
  20.        }
  21.    }
  22.    </script>
  23. </head>
  24. <body>
  25.     <form id="form1" runat="server">
  26.     <div>
  27.         <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="False" Width="100%" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnSorting="gvTest_Sorting">
  28.             <Columns>
  29.                 <asp:TemplateField HeaderText="选择">
  30.                     <ItemTemplate>
  31.                         <input id="cbk" type="checkbox" onclick="checkme(this,1);" name="cbkname"/>
  32.                     </ItemTemplate>
  33.                 </asp:TemplateField>
  34.                 <asp:BoundField DataField="APP_ID" HeaderText="ID" SortExpression="APP_ID" />
  35.                 <asp:BoundField DataField="APP_NAME" HeaderText="NAME" SortExpression="APP_NAME" />
  36.                 <asp:BoundField DataField="APP_MEMO" HeaderText="MEMO" SortExpression="APP_MEMO" />
  37.             </Columns>
  38.             <FooterStyle BackColor="White" ForeColor="#000066" />
  39.             <RowStyle ForeColor="#000066" />
  40.             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
  41.             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
  42.             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
  43.         </asp:GridView>
  44.          
  45.         <asp:Label ID="lbMsg" runat="server" Text="输入序号:"></asp:Label>
  46.         <asp:TextBox ID="txtIndex" runat="server" Width="90px"></asp:TextBox>
  47.         <asp:Button ID="btnChange" runat="server" OnClick="btnChange_Click" Text="将此行提前" /></div>
  48.     </form>
  49. </body>
  50. </html>

1、交换列:

主要在这几句代码,思路就是先删除,再添加:

  1. DataControlField dcf = gvTest.Columns[index];
  2. gvTest.Columns.RemoveAt(index);
  3. gvTest.Columns.Insert(0, dcf);

2、JS控制的选中CheckBox就改变行的颜色,主要就是调用一个JS函数来实现的,主要知道了怎么使用JS来遍历GridView就OK了,思路很简单。

  1. function checkme()
  2.    {
  3.        var cbkLength = document.getElementsByName("cbkname").length;
  4.        var colors = Array(cbkLength);
  5.        for(var i = 0;i < cbkLength; ++ i)
  6.        {
  7.           if(document.getElementsByName("cbkname")[i].checked)
  8.           {
  9.               //colors[i] = document.getElementById("gvTest").rows[i+1].style.backgroundColor;
  10.               document.getElementById("gvTest").rows[i+1].style.backgroundColor='blue';
  11.           }
  12.           else
  13.           {
  14.               document.getElementById("gvTest").rows[i+1].style.backgroundColor='';//colors[i];
  15.           }
  16.        }
  17.    }

 

原创粉丝点击