none
帮我看看 我写对CheckBoxList是否选中的判断怎么横为假? RRS feed

  • 问题

  •  

        protected void readFromAccess()//数据绑定
        {
          
            string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
            strConnection += @"Data Source=" + Server.MapPath("App_Data/City.mdb");

            OleDbConnection con = new OleDbConnection(strConnection);
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT perlike.* FROM perlike";
            OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            this.chkList.DataSource = dr;

                this.chkList.DataValueField = "id";
                this.chkList.DataTextField = "personLike";
     
            this.chkList.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)//判断选了哪项
        {
            for (int i = 0; i < this.chkList.Items.Count; i++)
            {
                if (this.chkList.ItemsIdea.Selected)
                {
                    Response.Write(this.chkList.ItemsIdea.Text);
                }
            }
        }

     

     

    也许是不能用 Response.Write(this.chkList.ItemsIdea.Text);??

    但是现在的问题是我调试时, if (this.chkList.ItemsIdea.Selected)这个判断 根本不成立呢?

    2008年12月27日 13:54

答案

  • 没有问题啊。完整测试代码

    Code Snippet

     

    <%@ Page Language="C#" EnableViewState="true" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
      System.Data.DataTable CreateDataSourceByXianhuiMeng()
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("id", 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] = "【孟子E章】" + 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);
        }
        return dt;
      }


      protected void Page_Load(object sender, EventArgs e)
      {   
        if (!Page.IsPostBack)
        {
          this.chkList.DataSource = CreateDataSourceByXianhuiMeng();
          this.chkList.DataValueField = "id";
          this.chkList.DataTextField = "学生姓名";
          this.chkList.DataBind();
        }
      }
     
      protected void Button1_Click(object sender, EventArgs e)
      {
        for (int i = 0; i < this.chkList.Items.Count; i++)
        {
          if (this.chkList.Items[i].Selected)
          {
            Response.Write(this.chkList.Items[i].Text);
          }
        }

      }
    </script>

    <body>
      <form id="form1" runat="server">
      <asp:CheckBoxList ID="chkList" runat="server"></asp:CheckBoxList>
      <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
      </form>
    </body>
    </html>

     

     

     

    2008年12月27日 14:01
    版主