首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Panel1动态绑定CheckBox控件,选中以后取不到值。搞崩溃了,求帮助

2013-11-06 
Panel1动态绑定CheckBox控件,选中之后取不到值。。。搞崩溃了,求帮助using Systemusing System.Collections.

Panel1动态绑定CheckBox控件,选中之后取不到值。。。搞崩溃了,求帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using FrameWork.Components;

namespace FrameWork.web.gongneng
{
    public partial class quanxianshezhi : System.Web.UI.Page
    {
        SqlDataReader reader1; SqlDataReader reader2; SqlDataReader reader3;
        sys_UserTable ut = BusinessFacade.sys_UserDisp(Common.Get_UserID);
        FrameWork.Data.SqlDataProvider sa = new FrameWork.Data.SqlDataProvider();
        protected void Page_Load(object sender, EventArgs e)
        {         
            if (!IsPostBack)
            {
            
                Panel1.GroupingText = "站点区";
                reader1 = sa.DataBase3("select [UserID] ,[U_LoginName] from sys_User");
                reader2 = sa.DataBase3("SELECT [id] ,[name] from [AreaTable]");

                while (reader1.Read())
                {
                    DropDownList1.Items.Add(new ListItem(reader1["U_LoginName"].ToString(), reader1["UserID"].ToString()));
                }
                while (reader2.Read())
                {
                    DropDownList2.Items.Add(new ListItem(reader2["name"].ToString(), reader2["id"].ToString()));
                }

                reader1.Close();
                reader2.Close();
                list3();
                //COMLIST();      
            }
            COMLIST(); 
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            list3(); COMLIST();
        }
        public void list3()
        {
            DropDownList3.Items.Clear();//清空DropDownList3里面集合数据
            reader3 = sa.DataBase3("select id,name from StreetTable where A_id='" + DropDownList2.SelectedValue + "'");
            while (reader3.Read())
            {
                DropDownList3.Items.Add(new ListItem(reader3["name"].ToString(), reader3["id"].ToString()));
            }
            reader3.Close();        
           
        }
        public void COMLIST()
        {
            this.Panel1.Controls.Clear();
            string sql1="SELECT  [Site_ID] ,[Site_Name]  from SiteTable where site_jiedao='"+DropDownList3.SelectedValue+"'";
            string sql2="SELECT  [Site_ID] ,[Site_Name]  from SiteTable where site_fenqu='"+DropDownList2.SelectedValue+"'";


            SqlDataReader table;
            if (DropDownList3.Text == "")
            {
                table = sa.DataBase3(sql2);
            }
            else
            {
                table = sa.DataBase3(sql1);
            }
           while(table.Read())
           {
                CheckBox c2 = new CheckBox();
                c2.Text = table["Site_Name"].ToString()+ "    &nbsp     &nbsp";
                c2.ID = table["Site_ID"].ToString();
                SqlDataReader readerwihle = sa.DataBase3("select site_id from quanxian where name_id=" + DropDownList1.SelectedValue + "");

                while (readerwihle.Read())
                {
                    if (c2.ID == readerwihle["site_id"].ToString())
                    {
                        c2.Checked = true;
                    }
                }
                Panel1.Controls.Add(c2);
                readerwihle.Close();
            }
           table.Close();
           
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            COMLIST();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            COMLIST();
            foreach (Control c in Panel1.Controls)//遍历Panel11内的所有控件
            {
                if (c is CheckBox)//只遍历CheckBox控件 
                {
                    ((CheckBox)c).Checked = true;
                    
                }
            }
          
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            COMLIST();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            COMLIST();
            foreach (Control c in Panel1.Controls)//遍历Panel11内的所有控件
            {
                if (c is CheckBox)//只遍历CheckBox控件 


                {
                    ((CheckBox)c).Checked = false;
                }
            }
    
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            COMLIST();
            foreach (Control c in Panel1.Controls)//遍历Panel11内的所有控件
            {
                if (c is CheckBox)//只遍历CheckBox控件 
                {
                    if (((CheckBox)c).Checked == true)
                    {
                        string sql = "insert into quanxian values(" + DropDownList1.SelectedValue + "," + ((CheckBox)c).ID.ToString() + ")";
                        sa.DataBase2(sql);
                    }
                }
            }
            
        }

        
    }
}




Panel1动态绑定CheckBox控件,选中以后取不到值。搞崩溃了,求帮助
DropDownList1是绑定的账号。。DropDownList2是绑定的大区。。DropDownList3是绑定的分区。。。
点击全选时候,当前生成的CheckBox能全选,取消也能取消。。可是点击授权按钮就得不到CheckBox的ID值了。。。手动选中也不行。。。为毛啊。。。
[解决办法]

????????protected?void?Button3_Click(object?sender,?EventArgs?e)
????????{
????????????COMLIST();
????????????foreach?(Control?c?in?Panel1.Controls)//遍历Panel11内的所有控件
????????????{
????????????????if?(c?is?CheckBox)//只遍历CheckBox控件?
????????????????{
????????????????????if?(((CheckBox)c).Checked?==?true)
????????????????????{
????????????????????????string?sql?=?"insert?into?quanxian?values("?+?DropDownList1.SelectedValue?+?","?+?((CheckBox)c).ID.ToString()?+?")";
????????????????????????sa.DataBase2(sql);
????????????????????}
????????????????}
????????????}
?????????????
????????}去掉这句
[解决办法]
你所有的按钮都有 COMLIST();重新绑定了,每次都是新的值了,之前所有的操作都没有用了,
点取消也不用这句,全选择也不用,这叫浪费资源。多读取一次数据库。

热点排行