web页面
我在datalist控件中绑定了checkboxlist
第一次点击时需要点击两次才能选中,
以后再点击就好了;
怎么解决?

解决方案 »

  1.   

    贴出代码看看
    if(!IsPostBack)
    {//绑定数据}
      

  2.   

    代码如下,没有特别的
     <asp:DataList ID="DataList1" runat="server" Width="100%" RepeatDirection="vertical"  
                            OnItemDataBound="DataList1_ItemDataBound" GridLines="Horizontal" RepeatLayout="table" >
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <asp:CheckBox ID="chkDepart" OnCheckedChanged="chkDep_CheckedChanged" AutoPostBack="true"
                                            runat="server" Text="" /></td>
                                </tr>
                                <tr>
                                    <td style="text-align: left">
                                        <asp:CheckBoxList RepeatDirection="horizontal"   OnSelectedIndexChanged="cbl_SelectedIndexChanged" AutoPostBack="true"   EnableViewState="true" ID="cblPeople"
                                            runat="server" /></td>
                                </tr>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:DataList>
    c#代码:
     protected void Page_Load(object sender, EventArgs e)
        {
            selectID = Request["ids"];
            filedids = Request["id"].Trim();
            if (!IsPostBack)
            {
                dep = new Antu.Business.Admin.Department();
                DataTable dt = dep.GetDepartment("18");
                user = new Antu.Business.Admin.User();
                DataTable dtUser = user.GetUintUser("18");
                dvUser = new DataView(dtUser);
                DataList1.DataSource = dt;
                DataList1.DataBind();            // setcblPeople();
            }
           // Response.Cache.SetNoStore();
        }    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            CheckBox chkDep = e.Item.FindControl("chkDepart") as CheckBox;
            chkDep.ID = "Dep";
            chkDep.CheckedChanged += new EventHandler(chkDep_CheckedChanged);
            CheckBoxList cbl = e.Item.FindControl("cblPeople") as CheckBoxList;
        
            if (cbl != null)
            {
                
                DataTable dt = user.GetDepartmentUser_FWAQ(drv["DepartID"].ToString());
                cbl.DataSource = dt;
                cbl.DataTextField = "User_Name";
                cbl.DataValueField = "UserId";
                cbl.DataBind();
                cbl.ID = drv["DepartID"].ToString();
                e.Item.Attributes.Add("DepID",drv["DepartID"].ToString());
                
               
            }
            if (chkDep != null)
            { 
                chkDep.Text = drv["Depart_Name"].ToString();
             //   chkDep.Attributes.Add("onclick", string.Format("javascript:onDepartSelect(this,{0})", drv["DepartID"].ToString()));
            }    }   protected  void chkDep_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (DataListItem item in DataList1.Items)
            {
                if (cb == item.FindControl("chkDepart") as CheckBox)
                {
                    CheckBoxList cbl = item.FindControl("cblPeople") as CheckBoxList;
                    if (cbl != null)
                    {
                        foreach (ListItem li in cbl.Items)
                        {
                            li.Selected = cb.Checked;
                           
                        }
                    }
                }
            }
        }
      

  3.   

    每次打开页面时,
    第一次选中
    如果 CheckBoxList 的 autopostback=false 
    结果c#获取不到,checkboxlist还是未选的;
    而autopostback=true的话,需要点两次;
    以后就好了
      

  4.   

    设置CheckBox autopostback=fasle
      

  5.   

    是否要根据chkDepart是否选择判断
     <asp:CheckBoxList RepeatDirection="horizontal"  ID="cblPeople" runat="server" />
      

  6.   

    chkDepart只是为了实现点击让CheckBoxList 全选,
    跟问题本身无关
      

  7.   

    //chkDep.ID = "Dep";
    //chkDep.CheckedChanged += new EventHandler(chkDep_CheckedChanged);//cbl.ID = drv["DepartID"].ToString();
    //e.Item.Attributes.Add("DepID",drv["DepartID"].ToString());将这四句去掉,换成在前台绑定。
      

  8.   

    .net 很多类似事情,比如点击两次下一页,点击两次删除,
    总之,第一次无效!!
    真杯具了
      

  9.   

    因为你在databind事件中重设了ID。如果你将这些重设的过程去掉,具体见16楼,第一次就会触发了。
      

  10.   

    那个checkbox的AutoPostBack是否设置为true了 ??
      

  11.   

    再来一次。肯定行的。
    //chkDep.ID = "Dep";
    //cbl.ID = drv["DepartID"].ToString();
    将这两句必须去掉。
    因为DataList1_ItemDataBound事件只会执行一次。以后返回服务端都不会再执行。你在页面加载的时候使用上面两句重设了控件的id,而在页面回发后,控件进行的重建,这个过程并没有再次执行,控件id变成系统默认前台的id(肯定和第一次加载时候不一样,不信的话,你可以查看源代码)。当控件的id改变后,系统认为是不同的控件,原来的DataList1_ItemDataBound事件要重新注册才能生效。当以后再次回发后,控件id就一直保持是系统默认的前台id,那么DataList1_ItemDataBound就一直有效。这就是你需要点击两次的原因,所以将上面两行去掉后,一切回复正常。
      

  12.   

    你自己复制了测试吧,我这里正常,只需要点击一次。<asp:DataList ID="DataList1" runat="server" Width="100%" RepeatDirection="vertical"  
            OnItemDataBound="DataList1_ItemDataBound" GridLines="Horizontal" RepeatLayout="table" >
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:CheckBox ID="chkDepart" OnCheckedChanged="chkDep_CheckedChanged" AutoPostBack="true"
                            runat="server" Text="" /></td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        <asp:CheckBoxList RepeatDirection="horizontal"    AutoPostBack="true"   EnableViewState="true" ID="cblPeople"
                            runat="server" /></td>
                </tr>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Left" />
        </asp:DataList>
    static DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            dt.Columns.Add("title");
            dt.Columns.Add("id");
            dt.Rows.Add("sdafsa", "1");
            dt.Rows.Add("sdafsa", "2");
            dt.Rows.Add("sdafsa", "3");
            dt.Rows.Add("sdafsa", "4");
            DataList1.DataSource = dt;
            DataList1.DataBind();        // setcblPeople();
        }
        // Response.Cache.SetNoStore();
    }protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        
        DataRowView drv = e.Item.DataItem as DataRowView;
        CheckBox chkDep = e.Item.FindControl("chkDepart") as CheckBox;
        //chkDep.ID = "chkDepart";//这里要保持前后台id一致,否则去掉
        chkDep.CheckedChanged += new EventHandler(chkDep_CheckedChanged);
        CheckBoxList cbl = e.Item.FindControl("cblPeople") as CheckBoxList;    if (cbl != null)
        {        cbl.DataSource = dt;
            cbl.DataTextField = "title";
            cbl.DataValueField = "id";
            cbl.DataBind();
            //cbl.ID = dt.Rows[e.Item.ItemIndex]["id"].ToString();//这里要保持前后台id一致,否则去掉
            e.Item.Attributes.Add("DepID", dt.Rows[e.Item.ItemIndex]["id"].ToString());
        }
        if (chkDep != null)
        {
            chkDep.Text = dt.Rows[e.Item.ItemIndex]["title"].ToString();
            //   chkDep.Attributes.Add("onclick", string.Format("javascript:onDepartSelect(this,{0})", drv["DepartID"].ToString()));
        }}protected void chkDep_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = sender as CheckBox;
        foreach (DataListItem item in DataList1.Items)
        {
            if (cb == item.FindControl("chkDepart") as CheckBox)
            {
                CheckBoxList cbl = item.FindControl("cblPeople") as CheckBoxList;
                if (cbl != null)
                {
                    foreach (ListItem li in cbl.Items)
                    {
                        li.Selected = cb.Checked;                }
                }
            }
        }
    }
      

  13.   

    哥们,我也遇到过,好像在属性里改一下什么的true或false
      

  14.   

    mngzilin朋友 ,你调试一下看有没有选中
    第一次点击选中了,但是后台并没有获取到选中
      

  15.   

    参考:http://blog.csdn.net/mngzilin/archive/2010/01/17/5202516.aspx
      

  16.   


    看来你真是不懂啊,我那只是个测试用的。前台我把那句去掉了,后台难道会执行??
    <asp:CheckBoxList RepeatDirection="horizontal"    AutoPostBack="true"  EnableViewState="true" ID="cblPeople" runat="server" OnSelectedIndexChanged="cbl_SelectedIndexChanged"/> 加上红色那句,看看能不能获取到?话我就不多说了。