熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

GridView實現服務器端和客戶端全選的兩種方法

2022-06-13   來源: .NET編程 

  代碼很簡單這裡就不累述了看代碼如下

  C#

  <%@ Page Language=C# AutoEventWireup=true %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN
transitionaldtd>
<script runat=server>
  // 計算數據完全可以從數據看取得
  ICollection CreateDataSource()
  {
    SystemDataDataTable dt = new SystemDataDataTable();
    SystemDataDataRow dr;
    dtColumnsAdd(new SystemDataDataColumn(序號 typeof(SystemString)));
    dtColumnsAdd(new SystemDataDataColumn(學生姓名 typeof(SystemString)));
    dtColumnsAdd(new SystemDataDataColumn(語文 typeof(SystemDecimal)));
    dtColumnsAdd(new SystemDataDataColumn(數學 typeof(SystemDecimal)));
    dtColumnsAdd(new SystemDataDataColumn(英語 typeof(SystemDecimal)));
    dtColumnsAdd(new SystemDataDataColumn(計算機 typeof(SystemDecimal)));

  for (int i = ; i < ; i++)
    {
      SystemRandom rd = new SystemRandom(EnvironmentTickCount * i); ;
      dr = dtNewRow();
      dr[] = iToString();
      dr[] = 【孟子E章】 + iToString();
      dr[] = SystemMathRound(rdNextDouble() * );
      dr[] = SystemMathRound(rdNextDouble() * );
      dr[] = SystemMathRound(rdNextDouble() * );
      dr[] = SystemMathRound(rdNextDouble() * );
      dtRowsAdd(dr);
    }
    SystemDataDataView dv = new SystemDataDataView(dt);
    return dv;
  }

  protected void Page_Load(object sender EventArgs e)
  {
    if (!IsPostBack)
    {

  GridViewDataSource = GridViewDataSource = CreateDataSource();
      GridViewDataBind();
      GridViewDataBind();
    }
  }

  protected void Button_Click(object sender EventArgs e)
  {
    RetText = ;
    foreach (GridViewRow gvr in GridViewRows)
    {
      CheckBox ch = (CheckBox)gvrFindControl(ItemCheckBox);
      if (chChecked)
      {
        RetText += <li>GridView 您選擇的是(鍵值) + GridViewDataKeys[gvrDataItemIndex]ValueToString();
      }
    }
  }

  protected void Button_Click(object sender EventArgs e)
  {
    RetText = ;
    foreach (GridViewRow gvr in GridViewRows)
    {
      CheckBox ch = (CheckBox)gvrFindControl(ItemCheckBox);
      if (chChecked)
      {
        RetText += <li>GridView 您選擇的是(鍵值) + GridViewDataKeys[gvrDataItemIndex]ValueToString();
      }
    }
  }

  protected void CheckAll(object sender EventArgs e)
  {
    CheckBox cbx = (CheckBox)sender;
    foreach (GridViewRow gvr in GridViewRows)
    {
      CheckBox ch = (CheckBox)gvrFindControl(ItemCheckBox);
      chChecked = cbxChecked;
    }
  }
</script>
<html xmlns=>
<head>
<title>GridView 實現服務器端和客戶端全選的兩種方法</title>
<script type=text/javascript>
  //<![CDATA[
  function CheckAll(oCheckbox)
  {
   var GridView = documentgetElementById(<%=GridViewClientID %>);
   for(i = ;i < GridViewrowslength; i++)
   {
    GridViewrows[i]cells[]getElementsByTagName(INPUT)[]checked = oCheckboxchecked;
   }
  }
 
  //]]>
  </script>
</head>
<body>
<form id=Form runat=server>
<table >
<tr valign=top>
<td>
<asp:GridView ID=GridView runat=server FontSize=px BackColor=#FFFFFF
GridLines=Both CellPadding= DataKeyNames=序號 AutoGenerateColumns=false>
<HeaderStyle BackColor=#EDEDED Height=px />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID=CheckBox runat=server Text=全選 AutoPostBack=true OnCheckedChanged=CheckAll />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=ItemCheckBox runat=server />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=學生姓名 HeaderText=學生姓名 />
<asp:BoundField DataField=語文 HeaderText=語文 />
<asp:BoundField DataField=數學 HeaderText=數學 />
<asp:BoundField DataField=英語 HeaderText=英語 />
<asp:BoundField DataField=計算機 HeaderText=計算機 />
</Columns>
</asp:GridView>
<asp:Button ID=Button runat=server OnClick=Button_Click Text=得到選擇的行值 />
</td>
<td align=right>
<asp:GridView ID=GridView runat=server FontSize=px BackColor=#FFFFFF
GridLines=Both CellPadding= DataKeyNames=序號 AutoGenerateColumns=false>
<HeaderStyle BackColor=#EDEDED Height=px />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id=Checkbox type=checkbox onclick=CheckAll(this) runat=server /><label>全選</label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=ItemCheckBox runat=server />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=學生姓名 HeaderText=學生姓名 />
<asp:BoundField DataField=語文 HeaderText=語文 />
<asp:BoundField DataField=數學 HeaderText=數學 />
<asp:BoundField DataField=英語 HeaderText=英語 />
<asp:BoundField DataField=計算機 HeaderText=計算機 />
</Columns>
</asp:GridView>
<asp:Button ID=Button runat=server OnClick=Button_Click Text=得到選擇的行值 />

  </td>
</tr>
<tr valign=top>
<td>
<asp:Literal ID=Ret runat=server></asp:Literal>
</td>
<td align=right>
<asp:Literal ID=Ret runat=server></asp:Literal>
</td>
</tr>
</table>
</form>
</body>
</html>


From:http://tw.wingwit.com/Article/program/net/201311/13455.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.