当前位置:编程学习 > C#/ASP.NET >>

如何选中datalist的控件,在线等,非常谢谢!

要更新datalist选中的checkbox行信息,通过断点,发现总是找不checkbox 
dlEmotion 是datalist的ID,cbChenk是checkbox的ID,大家有什么好办法可以找到datalist里的checkbox呢?非常谢谢!!
 protected void Button4_Click(object sender, EventArgs e)
    {
       CheckBox CB = (CheckBox)((DataListItem)dlEmotion.Controls[dlEmotion.Controls.Count - 1]).FindControl("cbChenk");
            if (CB!=null&&CB.Checked)
            {
                HiddenField hf = dlEmotion.Items[1].FindControl("hfID") as HiddenField;
                if (hf != null)
                {
                    string id = hf.Value;
                    int ids = int.Parse(id);

                    string sql = string.Format("update MenuType set [Menuname]='{0}' where [MenuId]='{1}'", this.TextBox2.Text,ids);

                    if (sd.CRUD(sql) > 0)
                    {
                        Response.Write("<script>alert('更改成功')</script>");
                      
                    }
                    else { Response.Redirect("<script>alert('更改失败')</script>"); }
                }

           
            }
        DataBinds();
    } --------------------编程问答-------------------- 你没循环当然找不到了,必须循环这个DataList的所有Row,循环所有行的时候再去FindControl才行,不然就只找了第一行之后就不会继续往下找了! --------------------编程问答--------------------
引用 1 楼 zzq495791432 的回复:
你没循环当然找不到了,必须循环这个DataList的所有Row,循环所有行的时候再去FindControl才行,不然就只找了第一行之后就不会继续往下找了!
 
你是说这样吗?还是没找到
  for(int i=0; i <  dlEmotion.Items.Count;i++)
       {
            CheckBox CB = (CheckBox) dlEmotion .Items[i].FindControl("cbChenk");

            if (CB!=null&&CB.Checked)
            {
                HiddenField hf = dlEmotion.Items[1].FindControl("hfID") as HiddenField;
                if (hf != null)
                {
                    string id = hf.Value;
                    int ids = int.Parse(id);

                    string sql = string.Format("update MenuType set [Menuname]='{0}' where [MenuId]='{1}'", this.TextBox2.Text,ids);

                    if (sd.CRUD(sql) > 0)
                    {
                        Response.Write("<script>alert('更改成功')</script>");
                      
                    }
                    else { Response.Redirect("<script>alert('更改失败')</script>"); }
                }

            }
       
            }
        DataBinds(); --------------------编程问答-------------------- datalist的item得用循环遍历行去找 --------------------编程问答-------------------- 你的循环写错了! --------------------编程问答--------------------
引用 4 楼 zzq495791432 的回复:
你的循环写错了!
写错了 麻烦能指导下吗?我学这个还不是很久 谢谢  --------------------编程问答-------------------- 不对不对,你那个判断条件,CB.Checked == true --------------------编程问答-------------------- 你调试过没,那个判断进没? --------------------编程问答-------------------- if的那个  --------------------编程问答-------------------- if (CB!=null&&CB.Checked) 这个  --------------------编程问答-------------------- if (CB!=null&&CB.Checked)
你调试了没,这个if判断有没有进去?
FindControl语句总是觉得有点问题 ,我好久没用过DataList这样找控件了,你从这两个地方去找,差不多就在这两个地方出了错误。下班了,要先闪了    --------------------编程问答-------------------- 哇 都走了啊~~哎··· --------------------编程问答-------------------- 参考 --------------------编程问答-------------------- 断点调试 

看看 在哪里的错误爆出来的
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,