gridview下获取checkboxlist的值

本来估计很简单的问题,却卡壳了。一开始用的是datalist,在确定程序没错误的情况下,居然获取不到在gridview中的值,郁闷了一下午,终于想开了,换gridview来试试,结果一下子就OK了。

起初使用datalist,在datalist中嵌套checkboxlist,后台代码我是这样写的

protected void AddRight()
    {
        int roleId = Convert.ToInt32(Request.QueryString["RoleId"]);


        for (int i = 0; i <= dlstModuleRight.Items.Count - 1; i++)
        {
            CheckBoxList chkRights = (CheckBoxList)dlstModuleRight.Items[i].FindControl("chkRoleRight");


            //此句可以攻取ModuleId
            Label lblId = (Label)dlstModuleRight.Items[i].FindControl("lblModuleId");


            int ModuleId= Convert.ToInt32(lblId.Text);


            
            for(int j=0;j            {
                if (chkRights.Items[j].Selected==true)
                {

                    Rights objRights = new Rights();
                    objRights.Add(roleId,ModuleId, chkRights.Items[j].Value);

                }

                Response.Write(chkRights.Text);
              

            }


可是,却取不到的值的郁闷,我又问了其它人,好像都没什么法子。这样不行,那只能再试着另一种写法了,我又用了以下代码去检验一下:



    protected void AddRight()
    {
        int roleId = Convert.ToInt32(Request.QueryString["RoleId"]);


        for (int i = 0; i <= dlstModuleRight.Items.Count - 1; i++)
        {

            if (this.dlstModuleRight.Items[i].ItemType == ListItemType.AlternatingItem || this.dlstModuleRight.Items[i].ItemType == ListItemType.Item)
            {


Label lblId = (Label)dlstModuleRight.Items[i].FindControl("lblModuleId");
CheckBoxList chkRights = (CheckBoxList)dlstModuleRight.Items[i].FindControl("chkRights");
CheckBox chk = (CheckBox)dlstModuleRight.Items[i].FindControl("chk");


if (lblId != null && chkRights != null)
                        {
                            int iModuleID = int.Parse(lblId.Text);
                            int iRightValue = 0;foreach(ListItem li in chkRights.Items)
                            {
                                if(li.Selected)
                                {
                                    
                                        iRightValue += int.Parse(li.Value);

                                        Response.Write(iRightValue.ToString());
                                    
                                    }

                                  
                                }    
                            }

            }


结果还是没有搞到checkboxlist里面的选择值,很是不爽,但又实在找不到错,希望各位帮忙指正一下:)

百思不得其解后,我索性把datalist撤了,换成gridview,这下爽了,一下子就搞掂了,呵呵[em13][em13]

代码也不复杂

protected void imgbtSure_Click(object sender, ImageClickEventArgs e)
    {

        int RoleId = Convert.ToInt32(Request.QueryString["RoleId"]);

        Rights objRights = new Rights();
        
        
        for (int i = 0; i < this.gdvRoleRight.Rows.Count; i++)
        {
            CheckBoxList chkRight = (CheckBoxList)this.gdvRoleRight.Rows[i].Cells[3].FindControl("chkRight");

            int ModuleId = Convert.ToInt32(this.gdvRoleRight.Rows[i].Cells[0].Text.Trim());

            foreach (ListItem li in chkRight.Items)
            {
                if (li.Selected)
                {
                    Response.Write(li.Text + "
");

                    objRights.Add(RoleId, ModuleId, li.Value);
                }
            }

        }
    }


写在这里,供大家参与,同时欢迎大家相互交流:)
[em02][em02]


上一篇: VS2005调试问题解决方案集锦
下一篇: gridview结合Zgraphy使用用法
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 24491
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭