gridview implements both server side and client side option sharing

  • 2020-11-20 06:03:12
  • OfStack


<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  //  Computational data, can be obtained completely from the data 
  ICollection CreateDataSource()
  {
    System.Data.DataTable dt =new System.Data.DataTable();
    System.Data.DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn(" The serial number ", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn(" The student's name ", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn(" Chinese language and literature ", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn(" mathematics ", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn(" English ", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn(" The computer ", typeof(System.Decimal)));
    for (int i =0; i <8; i++)
    {
      System.Random rd =new System.Random(Environment.TickCount * i); ;
      dr = dt.NewRow();
      dr[0] = i.ToString();
      dr[1] =" "Mencius" "+ i.ToString();
      dr[2] = System.Math.Round(rd.NextDouble() *100, 2);
      dr[3] = System.Math.Round(rd.NextDouble() *100, 2);
      dr[4] = System.Math.Round(rd.NextDouble() *100, 2);
      dr[5] = System.Math.Round(rd.NextDouble() *100, 2);
      dt.Rows.Add(dr);
    }
    System.Data.DataView dv =new System.Data.DataView(dt);
    return dv;
  }
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      GridView2.DataSource = GridView1.DataSource = CreateDataSource();
      GridView2.DataBind();
      GridView1.DataBind();
    }
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    Ret1.Text ="";
    foreach (GridViewRow gvr in GridView1.Rows)
    {
      CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
      if (ch.Checked)
      {
        Ret1.Text +="<li>GridView1  What you have chosen is (key value) : "+ GridView1.DataKeys[gvr.DataItemIndex].Value.ToString();
      }
    }
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
    Ret2.Text ="";
    foreach (GridViewRow gvr in GridView2.Rows)
    {
      CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
      if (ch.Checked)
      {
        Ret2.Text +="<li>GridView2  What you have chosen is (key value) : "+ GridView2.DataKeys[gvr.DataItemIndex].Value.ToString();
      }
    }
  }
  protected void CheckAll(object sender, EventArgs e)
  {
    CheckBox cbx = (CheckBox)sender;
    foreach (GridViewRow gvr in GridView1.Rows)
    {
      CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
      ch.Checked = cbx.Checked;
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>GridView  Two ways to implement both server-side and client-side selection </title>
  <script type="text/javascript">
  //<![CDATA[
  function CheckAll(oCheckbox)
  {
   var GridView2 = document.getElementById("<%=GridView2.ClientID %>");
   for(i =1;i < GridView2.rows.length; i++)
   {
    GridView2.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = oCheckbox.checked;
   }
  }

  //]]>
  </script>
</head>
<body>
  <form id="Form1" runat="server">
    <table style="width:800px;font-size:12px;">
      <tr valign="top">
        <td>
          <asp:GridView ID="GridView1" runat="server" Font-Size="12px" BackColor="#FFFFFF"
            GridLines="Both" CellPadding="4" DataKeyNames=" The serial number " AutoGenerateColumns="false">
            <HeaderStyle BackColor="#EDEDED" Height="26px"/>
            <Columns>
              <asp:TemplateField>
                <HeaderTemplate>
                  <asp:CheckBox ID="CheckBox1" runat="server" Text=" Future generations " AutoPostBack="true" OnCheckedChanged="CheckAll"/>
                </HeaderTemplate>
                <ItemTemplate>
                  <asp:CheckBox ID="ItemCheckBox" runat="server"/>
                </ItemTemplate>
              </asp:TemplateField>
              <asp:BoundField DataField=" The student's name " HeaderText=" The student's name "/>
              <asp:BoundField DataField=" Chinese language and literature " HeaderText=" Chinese language and literature "/>
              <asp:BoundField DataField=" mathematics " HeaderText=" mathematics "/>
              <asp:BoundField DataField=" English " HeaderText=" English "/>
              <asp:BoundField DataField=" The computer " HeaderText=" The computer "/>
            </Columns>
          </asp:GridView>
          <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" Gets the selected row value "/>
        </td>
        <td align="right">
          <asp:GridView ID="GridView2" runat="server" Font-Size="12px" BackColor="#FFFFFF"
            GridLines="Both" CellPadding="4" DataKeyNames=" The serial number " AutoGenerateColumns="false">
            <HeaderStyle BackColor="#EDEDED" Height="26px"/>
            <Columns>
              <asp:TemplateField>
                <HeaderTemplate>
                  <input id="Checkbox2" type="checkbox" onclick="CheckAll(this)" runat="server"/><label> Future generations </label>
                </HeaderTemplate>
                <ItemTemplate>
                  <asp:CheckBox ID="ItemCheckBox" runat="server"/>
                </ItemTemplate>
              </asp:TemplateField>
              <asp:BoundField DataField=" The student's name " HeaderText=" The student's name "/>
              <asp:BoundField DataField=" Chinese language and literature " HeaderText=" Chinese language and literature "/>
              <asp:BoundField DataField=" mathematics " HeaderText=" mathematics "/>
              <asp:BoundField DataField=" English " HeaderText=" English "/>
              <asp:BoundField DataField=" The computer " HeaderText=" The computer "/>
            </Columns>
          </asp:GridView>
          <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text=" Gets the selected row value "/>
        </td>
      </tr>
      <tr valign="top">
        <td>
          <asp:Literal ID="Ret1" runat="server"></asp:Literal>
        </td>
        <td align="right">
          <asp:Literal ID="Ret2" runat="server"></asp:Literal>
        </td>
      </tr>
    </table>
  </form>
</body>
</html>


Related articles: