首页 > 编程 > .NET > 正文

gridview实现服务器端和客户端全选的两种方法分享

2024-07-10 12:43:46
字体:
来源:转载
供稿:网友

代码如下:
<%@ 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">
  // 计算数据,完全可以从数据看取得
  ICollection CreateDataSource()
  {
    System.Data.DataTable dt =new System.Data.DataTable();
    System.Data.DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("序号", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("计算机", 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] ="【孟子】"+ 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 您选择的是(键值):"+ GridView1.DataKeys[gvr.DataItemIndex].Value.ToString();
      }
    }
  }

  protected void Button2_Click(object sender, EventArgs e)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表