遍歷頁面上的CheckBox,CheckBoxList


方法一

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無標題頁</title>
    <script runat="server">  
  protected void Button1_Click(object sender, EventArgs e)
  {
    FindBox(this.Controls);
  }

  private void FindBox(ControlCollection cc)
  {

      string CheckString = "";
      foreach (Control c in cc)
      {
          if (c.HasControls())
          {
              FindBox(c.Controls);
          }
          if (c is CheckBox)
          {
              CheckBox cb = c as CheckBox;
              if (cb.Checked)
              {
                  CheckString += cb.Text.Replace("CheckBox", "").Trim() + ",";
              }
          }
      }
      Response.Write(""+CheckString+"");

  }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:CheckBox ID="CheckBox7" runat="server" name="xxxxx" Text=" xxxxx" />
  <asp:CheckBox ID="CheckBox1" runat="server" name="Admin_Power" Text=" 投票1" />
  <asp:CheckBox ID="CheckBox2" runat="server" name="Admin_Power" Text=" 投票2" />
  <asp:CheckBox ID="CheckBox3" runat="server" name="Admin_Power" Text=" 投票3" />
  <asp:CheckBox ID="CheckBox4" runat="server" name="Admin_Power" Text=" 投票4" />
  <asp:CheckBox ID="CheckBox5" runat="server" name="Admin_Power" Text=" 投票5" />
  <asp:CheckBox ID="CheckBox6" runat="server" name="Admin_Power" Text=" 投票6" />
  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="得到" />

    </div>
    </form>
</body>
</html>

 方法二

protected void Button1_Click(object sender, EventArgs e)
    {
        string CheckString = "";
        foreach (Control ct in form1.Controls)
        {
            if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
            {
                CheckBox cb = (CheckBox)ct;
                if (cb.Checked)
                {
                    CheckString += cb.Text.Replace("CheckBox", "").Trim() + ",";
                }
            }

        }

        this.Label3.Text = CheckString;
    }

你的checkbox在form里,你只能在form中循環遍歷。

所以你必須先得到form這個對象。

如果是母版頁,你可以先這樣來得到form

HtmlForm f = this.FindControl("form的id") as HtmlForm;
或者也許是this.Master.FindControl("form的id") as HtmlForm;

遍歷頁面上的CheckBoxList:

 string CheckString = "";
      foreach (Control c in this.form1.Controls) //母版頁:foreach (Control c in this.Page.Master.FindControl("ContentPlaceHolder1").Controls
)
      {
          if (c is CheckBoxList)
          {
              foreach (ListItem item in (c as CheckBoxList).Items)
              {
                  if (item.Selected == true)
                  {
                      CheckString += item.Value + ",";

                  }
              }
          }
      }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM