我在一个DatList里面嵌套了另一个DataList,而里面的DataList里面有CheckBox,可是我写的程序,总有一个固定的CheckBox选中不了,请大家帮我看看什么原因哈。
前台代码:
<asp:DataList ID="DLApp" runat="server">
                            <ItemTemplate>
                                <asp:Label ID="应用程序标识Label" runat="server" Visible="false" Text='<%# Eval("应用程序标识") %>' />
                                <br />
                                名称:
                                <asp:Label ID="名称Label" runat="server" Text='<%# Eval("名称") %>' />
                                <asp:DataList ID="DLRoleList" runat="server" DataSourceID="dsRoleList" RepeatColumns="5"
                                    RepeatDirection="Horizontal" onitemdatabound="DLRoleList_ItemDataBound">
                                    <ItemTemplate>
                                        &nbsp;<asp:Label ID="RoleID" runat="server" Text='<%# Eval("角色标识") %>' Visible="False" />
                                        <br />
                                        <asp:CheckBox ID="cbrole" runat="server" AutoPostBack="true" />
                                        <asp:Label ID="lblRoleName" runat="server" Text='<%# Eval("角色名称") %>'></asp:Label>
                                        (
                                        <asp:Label ID="角色描述Label" runat="server" Text='<%#(bool)(Eval("角色描述").ToString()=="")?"无描述":Eval("角色描述") %>' />
                                        )<br />
                                        <br />
                                    </ItemTemplate>
                                </asp:DataList>
                                <hr style="width: 100%" />
                                <asp:SqlDataSource ID="dsRoleList" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM &quot;系统应用程序角色表&quot; WHERE (&quot;应用程序标识&quot; = :应用程序标识)">
                                    <SelectParameters>
                                        <asp:ControlParameter ControlID="应用程序标识Label" Name="应用程序标识" PropertyName="Text" Type="String" />
                                    </SelectParameters>
                                </asp:SqlDataSource>
                            </ItemTemplate>
                        </asp:DataList>
后台代码:
 protected void DLRoleList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            string[] alroleid = { };
            alroleid = Session["roleid"] as string[];
            if (alroleid != null)
            {
                for (int i = 0; i < DLApp.Items.Count; i++)
                {
                    DataList dl = (DataList)DLApp.Items[i].FindControl("DLRoleList");
                    for (int j = 0; j < dl.Items.Count; j++)
                    {
                        CheckBox cb = (CheckBox)dl.Items[j].FindControl("cbrole");
                        Label lbid = (Label)dl.Items[j].FindControl("RoleID");
                        if (alroleid.Contains(lbid.Text))
                        {
                            cb.Checked = true;
                        }
                        else
                        {
                            cb.Checked = false;
                        }
                    }
                }
            }
        }有不清楚的说出来哈。谢谢大家了

解决方案 »

  1.   


    protected void DLRoleList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
       if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
       {
          DataList dl = (DataList)e.Item.FindControl("DLRoleList");
          for (int j = 0; j < dl.Items.Count; j++)
          {
             CheckBox cb = (CheckBox)dl.Items[j].FindControl("cbrole");
             cb.Checked=true;
          }   }
    }
      

  2.   

    控件中的控件首你要foreach后,findcontrol("id")找到该控件。接下来再看你语法。
      

  3.   

    我能找到控件中的控件,也能给控件中的checkbox赋值,就是有最后一个checkbox不能被选中,帮忙看下代码是不是哪里错了,如果代码没错,那就是我数据库里面的几张表里面的数据不同步了。。
      

  4.   

    加这一句i (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

    ...
    }
      

  5.   

    加这一句if (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
    {
    }
      

  6.   

    我加上了这个也没效果,除了最后一个checkbox,其他的都正常。我把完整代码贴出来,大家帮忙看下。谢谢了。
      

  7.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Roles.aspx.cs" MasterPageFile="~/MasterPage.master"
        Inherits="crm.Web.UserCenter.Roles" %><asp:Content ContentPlaceHolderID="head" ID="head" runat="server">
    </asp:Content>
    <asp:Content ContentPlaceHolderID="ContentPlaceHolder1" ID="FVRole" runat="server">
        <div id="body" style="text-align: center; background-color: #353C44; height: 600px;
            color: White;">
            <table style="float: left">
                <tr>
                    <td>
                        <div style="float: left; text-align: left;">
                            <fieldset>
                                <legend><span style="color: White;">用户列表</span></legend>
                                <asp:GridView CssClass="GridViewStyle" GridLines="None" ID="GVRole" runat="server"
                                    AutoGenerateColumns="false" DataKeyNames="角色标识" OnRowDataBound="GVRole_RowDataBound"
                                    OnRowCommand="GVRole_RowCommand">
                                    <RowStyle CssClass="RowStyle" />
                                    <FooterStyle CssClass="FooterStyle" />
                                    <PagerStyle CssClass="PagerStyle" />
                                    <SelectedRowStyle CssClass="SelectedRowStyle" />
                                    <HeaderStyle CssClass="HeaderStyle" />
                                    <Columns>
                                        <asp:BoundField DataField="用户标识" HeaderText="用户标识" SortExpression="用户标识" Visible="false" />
                                        <asp:BoundField DataField="角色标识" HeaderText="角色标识" SortExpression="角色标识" Visible="false" />
                                        <asp:TemplateField HeaderText="用户名" SortExpression="用户名">
                                            <EditItemTemplate>
                                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("用户名") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lbtnName" ForeColor="Control" CommandName="editRole" runat="server"
                                                    Text='<%# Eval("用户名") %>' CommandArgument='<%# Eval("用户标识") %>'></asp:LinkButton>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="角色名" HeaderText="角色名" SortExpression="角色名" Visible="true" />
                                    </Columns>
                                </asp:GridView>
                            </fieldset>
                        </div>
                    </td>
                    <td align="left">
                        <div style="float: left; padding: 0px; margin: 0px; text-align: left">
                            <asp:DataList ID="DLApp" runat="server">
                                <ItemTemplate>
                                    <asp:Label ID="应用程序标识Label" runat="server" Visible="false" Text='<%# Eval("应用程序标识") %>' />
                                    <br />
                                    名称:
                                    <asp:Label ID="名称Label" runat="server" Text='<%# Eval("名称") %>' />
                                    <asp:Label ID="lblroleid" runat="server" Visible="false" Text='<%# Eval("用户标识") %>'></asp:Label>
                                    <asp:DataList ID="DLRoleList" runat="server" DataSourceID="dsRoleList" RepeatColumns="5"
                                        RepeatDirection="Horizontal" OnItemDataBound="DLRoleList_ItemDataBound">
                                        <ItemTemplate>
                                            &nbsp;<asp:Label ID="RoleID" runat="server" Text='<%# Eval("角色标识") %>' Visible="False" />
                                            <br />
                                            <asp:CheckBox ID="cbrole" runat="server" AutoPostBack="true" />
                                            <asp:Label ID="lblRoleName" runat="server" Text='<%# Eval("角色名称") %>'></asp:Label>
                                            (
                                            <asp:Label ID="角色描述Label" runat="server" Text='<%#(bool)(Eval("角色描述").ToString()=="")?"无描述":Eval("角色描述") %>' />
                                            )<br />
                                            <br />
                                        </ItemTemplate>
                                    </asp:DataList>
                                    <hr style="width: 100%" />
                                    <asp:SqlDataSource ID="dsRoleList" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM &quot;系统应用程序角色表&quot; WHERE (&quot;应用程序标识&quot; = :应用程序标识)">
                                        <SelectParameters>
                                            <asp:ControlParameter ControlID="应用程序标识Label" Name="应用程序标识" PropertyName="Text" Type="String" />
                                        </SelectParameters>
                                    </asp:SqlDataSource>
                                </ItemTemplate>
                            </asp:DataList>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="btnsubmit" runat="server" Text="确定" OnClick="btnsubmit_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </asp:Content>
    前台
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Collections;namespace crm.Web.UserCenter
    {
        public partial class Roles : System.Web.UI.Page
        {
            public string[] str;
            public string usersid;
            public string strSQL = "";
            public string strCMD = "";
            public string rolesname = "";
            public DataSet ds = new DataSet();
            public DataTable dt = new DataTable();
            public DataRow dtr;
            public string[] sArray;
            public string roleid = "";
            public int flag = 0;
            public bool exist;
            public CRM.Model.系统应用程序角色表 AppRole = new CRM.Model.系统应用程序角色表();
            public CRM.Model.系统用户应用程序角色表 Model = new CRM.Model.系统用户应用程序角色表();
            protected void Page_Load(object sender, EventArgs e)
            {
                AdminLoad();
            }        private void AdminLoad()
            {
                strSQL = "SELECT   用户标识, TRANSLATE (LTRIM (text, '/'), '*/', '*,') 角色标识  FROM (SELECT ROW_NUMBER () OVER (PARTITION BY 用户标识 ORDER BY 用户标识, lvl DESC) rn, 用户标识, text   FROM (SELECT     用户标识, LEVEL lvl,     SYS_CONNECT_BY_PATH (角色标识,'/') text  FROM (SELECT   用户标识, 角色标识 as 角色标识,ROW_NUMBER () OVER (PARTITION BY 用户标识 ORDER BY 用户标识,角色标识) x FROM 系统用户应用程序角色表   ORDER BY 用户标识, 角色标识) a  CONNECT BY 用户标识 = PRIOR 用户标识 AND x - 1 = PRIOR x)) WHERE rn = 1 ORDER BY 用户标识";
                ds = new BLL.GetView().GetData(strSQL);
                dt.Columns.Add("用户名");
                dt.Columns.Add("角色名");
                dt.Columns.Add("用户标识");
                dt.Columns.Add("角色标识");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string roles = "";
                    roleid = "";
                    CRM.Model.系统用户表 model = new CRM.Model.系统用户表();
                    model = new CRM.BLL.系统用户表().GetModel(string.Format("用户标识='{0}'", dr["用户标识"].ToString()));
                    rolesname = model.用户名称.ToString();
                    sArray = dr["角色标识"].ToString().Split(',');
                    for (int i = 0; i < sArray.Length; i++)
                    {
                        roleid += sArray[i] + ",";
                        AppRole = new CRM.BLL.系统应用程序角色表().GetModel(string.Format("角色标识='{0}'", sArray[i].ToString()));
                        roles += AppRole.角色名称 + ",";
                    }
                    dtr = dt.NewRow();
                    dtr["用户名"] = rolesname;
                    dtr["角色名"] = roles.Substring(0, roles.Length - 1);
                    dtr["用户标识"] = dr["用户标识"].ToString();
                    dtr["角色标识"] = roleid.Substring(0, roleid.Length - 1);
                    dt.Rows.Add(dtr);
                }
                GVRole.DataSource = dt;
                GVRole.DataBind();
            }        protected void GVRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GVRole.PageIndex = e.NewPageIndex;
                GVRole.DataSource = new CRM.BLL.系统用户应用程序角色表().GetModelLists("1=1 ");
                GVRole.DataBind();
            }        protected void GVRole_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "editRole")
                {
                    int rowindex = Convert.ToInt32(((GridViewRow)((LinkButton)e.CommandSource).Parent.Parent).RowIndex);
                    roleid = GVRole.DataKeys[rowindex]["角色标识"].ToString();
                    string userid = e.CommandArgument.ToString();
                    sArray = roleid.ToString().Split(',');
                    Session["roleid"] = sArray;
                    roleid = "";
                    for (int i = 0; i < sArray.Length; i++)
                    {
                        roleid += "'" + sArray[i] + "',";
                    }
                    roleid = roleid.Substring(0, roleid.Length - 1);
                    strSQL = string.Format("select distinct(系统应用程序表.应用程序标识) as 应用程序标识 ,系统应用程序表.名称 as 名称 from 系统应用程序表 left join 系统应用程序角色表 on 系统应用程序表.应用程序标识=系统应用程序角色表.应用程序标识 where 系统应用程序角色表.角色标识 in  ({0})", roleid);
                    ds = new BLL.GetView().GetData(strSQL);
                    DataTable table = new DataTable();
                    table.Columns.Add("应用程序标识");
                    table.Columns.Add("名称");
                    table.Columns.Add("用户标识");
                    foreach (DataRow tablerow in ds.Tables[0].Rows)
                    {
                        DataRow newrow = table.NewRow();
                        newrow["应用程序标识"] = tablerow["应用程序标识"].ToString();
                        newrow["名称"] = tablerow["名称"].ToString();
                        newrow["用户标识"] = userid;
                        table.Rows.Add(newrow);
                    }
                    DLApp.DataSource = table;
                    DLApp.DataBind();
                }
            }        protected void GVRole_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#F98214'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#F98214'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }        private void BindDLRole()
            {
                strSQL = "SELECT distinct(应用程序标识),系统应用程序表.名称 FROM 系统应用程序表";
                ds = new BLL.GetView().GetData(strSQL);
                DLApp.DataSource = ds;
                DLApp.DataBind();        }        protected void DLRoleList_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    string[] alroleid = { };
                    alroleid = Session["roleid"] as string[];
                    if (alroleid != null)
                    {
                        foreach (DataListItem dli in DLApp.Items)
                        {
                            DataList dl = (DataList)dli.FindControl("DLRoleList");
                            if (dl != null)
                            {
                                foreach (DataListItem dlitem in dl.Items)
                                {
                                    CheckBox cb = (CheckBox)dlitem.FindControl("cbrole");
                                    Label lbid = (Label)dlitem.FindControl("RoleID");
                                    if (alroleid.Contains(lbid.Text))
                                    {
                                        cb.Checked = true;
                                    }
                                    else
                                    {
                                        cb.Checked = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }        protected void btnsubmit_Click(object sender, EventArgs e)
            {
                foreach (DataListItem dli in DLApp.Items)
                {
                    Label lblid = (Label)dli.FindControl("lblroleid");
                    Model.用户标识 = lblid.Text;
                    DataList dl = (DataList)dli.FindControl("DLRoleList");
                    foreach (DataListItem datali in dl.Items)
                    {
                        CheckBox cb = (CheckBox)datali.FindControl("cbrole");
                        Label lblroleid = (Label)datali.FindControl("RoleID");
                        Model.角色标识 = lblroleid.Text;
                        if (cb.Checked)
                        {
                            exist = new CRM.BLL.系统用户应用程序角色表().ModelExist(Model);
                            if (!exist)
                            {
                                flag += new CRM.BLL.系统用户应用程序角色表().Add(Model);
                            }
                        }
                        else
                        {
                            exist = new CRM.BLL.系统用户应用程序角色表().ModelExist(Model);
                            if (exist)
                            {
                                new CRM.BLL.系统用户应用程序角色表().Delete(Model);
                            }
                        }
                    }
                }
                if (flag > 0)
                {
                    ScriptManager.RegisterStartupScript(this.Page, GetType(), "alert", "alert('设置成功!')", true);
                    dt = new DataTable();
                    AdminLoad();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.Page, GetType(), "alert", "alert('设置失败!')", true);
                    dt = new DataTable();
                    AdminLoad();
                }
            }
        }
    }后台