由于前面一篇帖子问题还没搞清楚。
所以再来问问
http://topic.csdn.net/u/20090723/14/150fdc5f-f289-44d4-a4b8-96fdcff16af8.html就是想实现上面那个图的功能,
点那个dropdownlist选择不同的角色。
然而下面那个gridview里license字段的打勾或者不打勾是根据用户角色来的。
如果这个用户有权限呢,就打上,没有的话就不打。
请问下如何实现啊?
不知道在后台如何操控打勾不打勾的问题。。

解决方案 »

  1.   

    把那个项存入数据库,循环查啊,查数据库里对应的是哪一个,如果哪一项为true就选中啦。
      

  2.   

    我现在就差一个思路,就是不知道如何在后台里面写那段代码。
    就像checked= <%#Eval("FieldName").ToString()=="1"?true:false%>这个代码不知道写在.cs文件里。
      

  3.   

    你上一个问题别人不是已经答得很清楚了吗?<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("权限").ToString()=="有权限"?true:false %>' />
      

  4.   


    不是写在后台,是直接写在html里面。
      

  5.   

    这个代码太多, 其实 不复杂 ,先把权限读到datatable里。 然后根据当前行的id 遍历权限 true 就让checibox 选中
      

  6.   

    我想把
    <asp:CheckBox ID="CheckBox1" runat="server" Checked=' <%# Eval("权限").ToString()=="有权限"?true:false %>' />这句话放在.cs文件里如何写呢?
    没头绪
    因为是这样的gridview中的那个license字段,默认我不是想让它去数据库里判断是否有权限,就是说默认不让它打勾的, 
    而是根据上面那个dropdownlist的角色来判断,如果这个人有这个权限的话就打上,,是这样的。
    而不是一开始gridview里就判断了
      

  7.   

    我知道那个是写在html里,
    但是我想的是写在.cs里。
      

  8.   


    那就用上面这位的方法,找到那个checkbox所在行对应的权限,就可以判断true or false了
      

  9.   

    你找到gd里面的那个checkbox在那个下拉菜单选择时间里面通过那个下拉菜单的值来查找到这个权限,如果这个有就判断让这个checkboxcheck
      

  10.   


    在下拉框事件里foreach(DataGridItem item in DataGrid1.Items)
    {
         CheckBox cb = item.FindControl["你那个checkbox的id"] as CheckBox;
         //拿到权限的那个字段,假设为 aa,
         if(aa=="有权限")
        {     
            cb.Checked = true;
        }
    }
      

  11.   

    你用的什么数据库? 是access么?有两个解决办法,一个是数据库做操作然后直接绑定,另一个是用C#来实现
      

  12.   

    是ACCESS,
    用C#代码实现的能不能给个代码看下。
      

  13.   

    为什么大家都不贴个代码看看呀,
    想要的效果不是放在html里写绑定代码,
    而是想在.cs文件里写呀
      

  14.   

    前台代码
    <asp:CheckBox ID="CheckBox1" runat="server" Checked=' <%# GetXXX(Eval("权限"))%>' />后台protected bool GetXXX(object s)
    {
    return s.ToString()=="有权限"?true:false;
    }
      

  15.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownListDemo.aspx.cs" Inherits="csdn_DropDownListDemo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <table>
        <tr>
        <td>用户名:</td><td>
        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
        <asp:ListItem Text="请选择用户角色" Selected="True"  Value="-1"></asp:ListItem>
        <asp:ListItem Text="主管" Value="0"></asp:ListItem>
        <asp:ListItem Text="经理" Value="1"></asp:ListItem>
        <asp:ListItem Text="职员" Value="2"></asp:ListItem>
        </asp:DropDownList>
        </td>
        </tr>
        <tr>
        <td></td>
        <td>
            <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_RowDataBound">
            <Columns>
            <asp:BoundField HeaderText="权限名" DataField="right" />
            <asp:BoundField HeaderText="URL" DataField="url" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="ckb" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            </asp:GridView>
        </td>
        </tr>
        </table>
        </form>
    </body>
    </html>
      

  16.   


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class csdn_DropDownListDemo : System.Web.UI.Page
    {
        string right = string.Empty;
        string[] tempArray;    ICollection CreateDataSource()
        {        DataTable dt = new DataTable();        DataRow dr;        dt.Columns.Add(new DataColumn("right", typeof(string)));        dt.Columns.Add(new DataColumn("url", typeof(string)));
                   
            dr = dt.NewRow();        dr[0] = "789";        dr[1] = "~/aspx/manageuser.aspx";        dt.Rows.Add(dr);        dr = dt.NewRow();        dr[0] = "889";        dr[1] = "~/aspx/temp.aspx";        dt.Rows.Add(dr);        dr = dt.NewRow();        dr[0] = "321";        dr[1] = "~/aspx/managedepartment.aspx";        dt.Rows.Add(dr);        dr = dt.NewRow();        dr[0] = "123";        dr[1] = "~/aspx/test.aspx";        dt.Rows.Add(dr);        return dt.DefaultView;    }    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gv.DataSource = CreateDataSource();
                gv.DataBind();
            }
        }
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {        
            //模拟数据库查询操作
            switch (ddl.SelectedValue)
            { 
                case "-1":                
                    break;
                case "0":
                    right = "789,889,321,123";
                    break;
                case "1":
                    right = "789,321,123";
                    break;
                case "2":
                    right = "889,321";
                    break;
                default:
                    break;
            }
            gv.DataSource = CreateDataSource();
            gv.DataBind();    }
        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType== DataControlRowType.Header)
            {
                tempArray = right.Split(',');
            }        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (drv != null && tempArray != null)
                {
                    if (Array.IndexOf(tempArray, drv["right"]) != -1)
                    {
                        CheckBox ckb = e.Row.FindControl("ckb") as CheckBox;
                        if (ckb != null)
                        {
                            ckb.Checked = true;
                        }
                    }
                }
            }
        }
    }
      

  17.   


    <%@ Page Title="" Language="C#" MasterPageFile="~/UserPower.Master" AutoEventWireup="true"
        CodeBehind="ManagerRolePower.aspx.cs" Inherits="XFS.Aspx.SystemSetting.ManagerRolePower" %><asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder_Main" runat="server">
        <table class="MainTable">
            <tr>
                <td class="KeyTd">
                    <asp:Label ID="Label_RoleName" runat="server" Text="角色名:"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList_Role" runat="server" DataSourceID="SqlDataSource_Role"
                        DataTextField="RoleName" DataValueField="RoleId" AutoPostBack="True" 
                        AppendDataBoundItems="True" 
                        onselectedindexchanged="DropDownList_Role_SelectedIndexChanged">
                        <asp:ListItem Value="0">请选择用户角色</asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource_Role" runat="server" ConnectionString="<%$ ConnectionStrings:XFS_Master %>"
                        ProviderName="<%$ ConnectionStrings:XFS_Master.ProviderName %>" SelectCommand="SELECT [RoleId], [RoleName] FROM [UserRole]">
                    </asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:GridView ID="GridView_RolePower" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                        CellPadding="4" DataSourceID="SqlDataSource_RolePower" ForeColor="#333333" 
                        GridLines="None">
                        <RowStyle BackColor="#EFF3FB" />
                        <Columns>
                            <asp:TemplateField HeaderText="权限名" SortExpression="PowerName">
                                <ItemTemplate>
                                    <asp:Label ID="Label_PowerName" runat="server" Text='<%# Bind("PowerName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="URL" SortExpression="PowerUrl">
                                <ItemTemplate>
                                    <asp:Label ID="Label_Url" runat="server" Text='<%# Bind("PowerUrl") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="License" SortExpression="License">
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox_License" runat="server" Checked='<%# Eval("License").ToString()=="True"?true:false %>' 
                                         />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#2461BF" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource_RolePower" runat="server" ConnectionString="<%$ ConnectionStrings:XFS_Master %>"
                        ProviderName="<%$ ConnectionStrings:XFS_Master.ProviderName %>" SelectCommand="SELECT A.[PowerName], A.[PowerUrl], B.[License] FROM ([PowerInfo] AS A LEFT JOIN [RolePower] AS B ON A.[PowerId]=B.[PowerId])"></asp:SqlDataSource>
                </td>
            </tr>
            <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="Button_OK" runat="server" Text="确定" onclick="Button_OK_Click" />
            </td>
            </tr>
        </table>
    </asp:Content>
      

  18.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace XFS.Aspx.SystemSetting
    {
        public partial class ManagerRolePower : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                        }        protected void DropDownList_Role_SelectedIndexChanged(object sender, EventArgs e)
            {
                string RoleId = DropDownList_Role.SelectedValue;
                SqlDataSource_RolePower.SelectParameters.Clear();
                SqlDataSource_RolePower.SelectCommand = "SELECT A.PowerName, A.PowerUrl, B.License FROM (PowerInfo AS A LEFT JOIN RolePower AS B ON A.PowerId=B.PowerId) LEFT JOIN UserRole AS C ON B.RoleId=C.RoleId WHERE C.RoleId=[@RoleId]";
                SqlDataSource_RolePower.SelectParameters.Add("@RoleId", RoleId);
                
                GridView_RolePower.DataBind();
                        }
            protected void Button_OK_Click(object sender, EventArgs e)
            {
                for (int i = 0; i != GridView_RolePower.Rows.Count; i++)
                {
                    if (((CheckBox)GridView_RolePower.Rows[i].Cells[2].FindControl("aa")).Checked)
                    {
                        //panduan shifoucunzai   bucunzai tianjia  cunzai gengxin 
                    }
                    else
                    { 
                        //panduanshifoucunzai cunzai shanchu
                    }
                }
            }
        }
    }
      

  19.   

    数据库的设计是这样的。 UserRole(角色表)  PowerInfo(权限表)  RolePower(角色权限表---)  
      

  20.   

    现在的问题是这样的。
    默认的话就是像下面这张图一样,license是没有打上勾的而当我dropdownlist选择过之后license就会根据上面的那个角色来判断他有没有权限,有的话就打上勾,没有的话就不打,
    就是想实现那个打勾,请问下如何实现?
    要结合我上面贴出来的代码接着下去实现。
    不太明白。
      

  21.   

            dt.Columns.Add(new DataColumn("right", typeof(string)));        dt.Columns.Add(new DataColumn("url", typeof(string)));
    我这里加两列的原因 是想告诉你 你不用关联两个表 来绑定 License字段的不是说两个解决办法么
    首先说数据库如何解决你首次绑定的时候,sql 语句这样写 select [PowerId],[PowerName],[PowerUrl],'false' [License] from [PowerInfo]当选择DropDownList的时候 sql语句这样写select [PowerId],[PowerName],[PowerUrl],(select [License] from [RolePower] r where r.[RoleId]='DropDownList.SelectedValue' and r.[PowerId]=[PowerId])
    from [PowerInfo]
      

  22.   


    select [PowerId],[PowerName],[PowerUrl],(select [License] from [RolePower] r where r.[RoleId]='DropDownList.SelectedValue'
     and r.[PowerId]=[PowerId]) from [PowerInfo]
      

  23.   


    select [PowerId],[PowerName],[PowerUrl],
    (select [License] from [RolePower] r where r.[RoleId]='DropDownList.SelectedValue' 
    and r.[PowerId]=[PowerId]) from [PowerInfo]
      

  24.   

    囧select [PowerId],[PowerName],[PowerUrl],'false' [License] from [PowerInfo]
    powerinfo表里没 'false' [license] 这两个字段
      

  25.   

    那是一种sql 语句的用法,在sql 中是没问题的,你在access里试下,应该也可以
      

  26.   

            在后台的DropDownList_Role_SelectedIndexChanged()事件中,根据选中的角色,查出此角色对应的[PowerId],[License] ,根据License是否验证,再循环遍历其License字段的数组值,为true时就勾上,为false时就不勾,反正你gridview里面的][PowerName],[PowerUrl]绑定是死的,只是License字段的状态改变是随着role改变的
      

  27.   

    肯定不行的吧
    license这个字段都没在powerinfo里也没连接两个表,怎么会选的出来
      

  28.   

    那个license 是别名的意思 不是字段,你先试试再说
      

  29.   

    还有就是功能我都没有些明白。
    原来自己一直那样想的做的其实是错的。
    刚刚同事过来说了我下
    现在让我知道功能其实是另一种。
    我把这功能说下。
    就是默认gridview把rolepower里面所有数据取出来。
    就像上面图那样。
    默认license不要打勾
    然后根据上面那个dropdownlist选择的角色来判断这个角色是否有权限再把那个licesnse打上勾。。
    但是gridview的数据还是向默认取出来那么多,而不是根据角色来取出他有的权限,是要全部权限显示出来,
    选择的那个角色没有权限的话也可以自己把license打上勾,再更新回数据库。
      

  30.   

    我晕那个有权限就设true OK了
      

  31.   

    你先试试 这句 access 能运行不select [PowerId],[PowerName],[PowerUrl],'false' [License] from [PowerInfo]
      

  32.   

    语法错误 (操作符丢失) 在查询表达式 ''false' [License]' 中。 
      

  33.   

     就是默认gridview把rolepower里面所有数据取出来。你的powerInfo 是全部权限吧~
      

  34.   

    gridview其实是两张表里取出数据来的
    一张powerinfo
    就是放了[权限名][URL]
    还有一个license是从rolepower取出来的。这个就是来判断有没权限的。
    rolepower里面数据就是放了userrole角色表的id, powerinfo权限表的id,还有个就是license(是/否类型的字段)
      

  35.   

    应该是6点,不过5点有培训看来只够说说思路了
    你要查询的主表是 powerinfo 
    一开始 是不用考虑 license字段的,因为都是未选中
    你只需将powerinfo  中的所有记录绑给gridview 就可以了所以 我一开始写select [PowerId],[PowerName],[PowerUrl],'false' [License] from [PowerInfo]意思就是 绑定的时候 只考虑 PowerInfo 表 将false 这个值 最为附件字段 给结果集
    可惜的是 可能access 不支持这种写法
    不过也没关系你绑定的语句 就写select [PowerId],[PowerName],[PowerUrl] from [PowerInfo]就可以了然后当选择下拉列表框时 
    根据对应的RoleId 去查询 RolePower 表select * from [RolePower] where [RoleId]='xxx'
      

  36.   

    这个查询的结果集 在 girdview 的 RowDataBound 事件中 会 用到在 RowDataBound 事件中 循环判断 当前的 PowerId 是否相等, 然后取对应的License 的值
    赋给 对应的 checkbox
    这段代码 你可以参考我在 20楼写的代码
      

  37.   

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
             //获取控件和主键ID,查询数据库是否有该权限,设置checkbox 选中
               CheckBox ckb= (CheckBox)e.Row.FindControl("checkbox1");
             // 通过e.Row.Cells[1].Text.ToString().Trim()或主键查询是否有权限
            }
        }
      

  38.   

    程序判断里用FindControl应该就可以了!
      

  39.   

    在每个CheckBox中加入代码判断
    <asp:CheckBox onclick="onCheck(this);"/><script>
    function onCheck(obj)
    {
        if(document.getElementById('<%= 下拉框.ClientID %>').selectedIndex != 0)
        {
             return false;
        }
    }
    </script>
      

  40.   

    DataRow[] rows = 取出数据;
    for (int i = 0; i < this.ckboxRole.Items.Count; i++)
                    {
                        foreach (DataRow row in rows)
                        {
                            if (this.ckboxRole.Items[i].Value.ToString()==row["RoleId"].ToString())
                            {
                                this.ckboxRole.Items[i].Selected = true;                        }
                        }
                    }
      

  41.   

    access 的表结构和 你提供的是一样的,这里就不贴了
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" 
    Inherits="Blog_AccessDemo_Main" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <table>
        <tr>
        <td width="30%">角色名:</td><td>
        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">    
        </asp:DropDownList>
        </td>
        </tr>
        <tr>
        <td></td>
        <td>
            <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" >
            <Columns>
            <asp:BoundField HeaderText="权限名" DataField="PowerName" />
            <asp:BoundField HeaderText="URL" DataField="PowerUrl" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="ckb" runat="server" onclick="this.parentNode.children[1].value=!this.checked;" Checked='<%#bool.Parse(Eval("License").ToString()) %>' />
                    <asp:HiddenField ID="hid" runat="server" Value='<%# Eval("License")%>' />
                    <asp:HiddenField ID="hidID" runat="server" Value='<%# Eval("PowerID")%>' />
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            </asp:GridView>
            <asp:Button ID="btnSave" Text="保存" runat="server" OnClick="btnSave_Click" />
        </td>
        </tr>
        </table>
        </form>    
    </body>
    </html>
      

  42.   

    哎,看来看去,
    看到那86楼的好像和自己想像中的有些像了,
    感觉可以做了
    可是动起手来就不知道怎么下手了
    不知道sandy945原来刚学的时候有没有这样的痛苦啊
    这感觉真不舒服 。。
      

  43.   

    access 的数据库名 叫 Database1.accdb 是用 access 2007创建的using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using System.Data.OleDb;
    using System.Text;
    using System.Collections.Generic;public partial class Blog_AccessDemo_Main : System.Web.UI.Page
    {
        private const string conStr="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents and Settings\\xxx\\My Documents\\Database1.accdb;Persist Security Info=False;";    private void InitControl(string connectionStr)
        {
            using (OleDbConnection ocon = new OleDbConnection(connectionStr))
            {
                OleDbDataAdapter oda = new OleDbDataAdapter("SELECT [PowerId], [PowerName], [PowerUrl], 'False' AS [License] FROM [PowerInfo]",ocon);
                DataSet ds = new DataSet();
                oda.Fill(ds, "PowerInfo");
                if (ds.Tables.Contains("PowerInfo"))
                {
                    gv.DataSource = ds.Tables["PowerInfo"];
                    gv.DataBind();
                }
                oda = new OleDbDataAdapter("SELECT [RoleID],[RoleName] FROM [UserRole]", ocon);
                oda.Fill(ds, "UserRole");
                if (ds.Tables.Contains("UserRole"))
                {
                    ddl.DataSource = ds.Tables["UserRole"];
                    ddl.DataTextField = "RoleName";
                    ddl.DataValueField = "RoleID";
                    ddl.DataBind();
                }
                ddl.Items.Insert(0, new ListItem("请选择用户角色", "-1"));
            } 
        }    protected void Page_Load(object sender, EventArgs e)
        {        
            if (!IsPostBack)
            {
                InitControl(conStr);
            }
            
        }
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            string RoleID = ddl.SelectedValue;
            if (!string.IsNullOrEmpty(RoleID) && RoleID != "-1")
            {
                string sql = string.Format("SELECT A.*, B.License FROM (PowerInfo AS A LEFT JOIN RolePower AS B ON A.PowerId=B.PowerId)  WHERE B.RoleId={0}", RoleID);
                using (OleDbConnection ocon = new OleDbConnection(conStr))
                {
                    OleDbDataAdapter oda = new OleDbDataAdapter(sql, ocon);
                    DataSet ds = new DataSet();
                    oda.Fill(ds, "PowerInfo");
                    if (ds.Tables.Contains("PowerInfo") && ds.Tables["PowerInfo"].Rows.Count > 0)
                    {
                        IList<string> list = new List<string>();
                        for (int i = 0; i < ds.Tables["PowerInfo"].Rows.Count; i++)
                        {
                            if (ds.Tables["PowerInfo"].Rows[i]["License"].ToString().ToLower() == "true")
                            {
                                list.Add(ds.Tables["PowerInfo"].Rows[i]["PowerId"].ToString());
                            }
                        }
                        for (int i = 0; i < gv.Rows.Count; i++)
                        {
                            HiddenField hid = gv.Rows[i].FindControl("hidID") as HiddenField;
                            CheckBox ckb = gv.Rows[i].FindControl("ckb") as CheckBox;
                            if (hid != null && ckb != null)
                            {
                                if (list.Contains(hid.Value))
                                {
                                    ckb.Checked = true;
                                }
                                else
                                {
                                    ckb.Checked = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < gv.Rows.Count; i++)
                        {                        
                            CheckBox ckb = gv.Rows[i].FindControl("ckb") as CheckBox;
                            if (ckb != null)
                            {
                                ckb.Checked = false;                            
                            }
                        }
                    }
                }
            }
        }    protected void btnSave_Click(object sender, EventArgs e)
        {
            string RoleID = ddl.SelectedValue;
            if (!string.IsNullOrEmpty(RoleID) && RoleID != "-1")
            {
                CheckBox ckb;
                HiddenField hid, hidID;
                for (int i = 0; i < gv.Rows.Count; i++)
                {
                    ckb = gv.Rows[i].FindControl("ckb") as CheckBox;
                    hid = gv.Rows[i].FindControl("hid") as HiddenField;
                    if (ckb != null && hid != null)
                    {
                        if (ckb.Checked.ToString() != hid.Value)
                        {
                            hidID = gv.Rows[i].FindControl("hidID") as HiddenField;
                            if (hidID != null)
                            {
                                string sql = string.Format("update [RolePower] set[License]={0} where [RoleID]={1} and [PowerID]={2} ;", ckb.Checked, RoleID, hidID.Value);
                                using (OleDbConnection ocon = new OleDbConnection(conStr))
                                {
                                    using (OleDbCommand ocmd = new OleDbCommand(sql, ocon))
                                    {
                                        if (ocon.State != ConnectionState.Open)
                                        {
                                            ocon.Open();
                                        }// end if ocon.Open
                                        int flag = ocmd.ExecuteNonQuery();
                                        if (flag == 0)
                                        {
                                            ocmd.CommandText = string.Format("insert into [RolePower]([RoleID],[PowerID],[License]) values({0},{1},{2});", RoleID, hidID.Value, ckb.Checked);
                                            ocmd.ExecuteNonQuery();
                                        }// end if flag
                                        if (ocon.State != ConnectionState.Closed)
                                        {
                                            ocon.Close();
                                        }// end if ocon.Closed
                                    }//end using ocmd
                                }// end using ocon                            
                            }// end if hidID
                        }//end if ckb.Checked
                    }//end if ckb!=null
                }//end for            
            }//end if RoleID
        }//end event btnSave_Click
    }
      

  44.   

    onclick="this.parentNode.children[1].value=!this.checked;" 这句话什么意思?
    还有这个Checked='<%#bool.Parse(Eval("License").ToString())我想要的就是默认不管数据库里license是否打上勾,但是默认girdview是不打勾的,就是当上面那个dropdownlist角色改变之后再根据那个角色有这个权限的话再打上勾。。
      

  45.   

    我里面一些数据绑定都是用sqldatasource的
      

  46.   

    就是那个角色dropdownlist还有grdiview都是用sqldatasource绑定。。
    好像和你弄的有些不一样噢,
    看到你72,73楼的代码,感动啊
    还专门做个代码演示了一遍,
      

  47.   

    你用的accessdatasource 还是 sqldatasource
      

  48.   

    你那个checkbox这句话Checked=' <%#bool.Parse(Eval("License").ToString())写了好像不对
    因为像你给我的那个从数据库里取出来的代码是这样的。
    select powerid,powername,powerUrl from powerinfo 这个代码是放在sqldatasource的selectcommand属性里的。
    而license是在rolepower表里的,
    按照这样写的话岂不是那个 gridview里的数据是从powerinfo和rolepower两张表里取出来的?????
      

  49.   

    我用的是sqldatasource
    用这个也可以去操作access数据库的
    前面一些功能都是access数据库而用的是sqldatasource的
      

  50.   

    你的数据库是mdb 文件是么
      

  51.   

    onclick="this.parentNode.children[1].value=!this.checked;" 
    这句话里面的this.parentnode.cildren[1].value什么意思?
      

  52.   

    网上大概看了下
    parentNode.children
    好像是 treeview里的属性,可是这里面都没用到这个控件。
    写这句话是什么意思呢?
      

  53.   

    这句话是 写在 checkbox 的 onclick 事件中的,
    gridview 生成的html 代码 可能是这样的
    <table><tr><td>PowerName</td><td>PowerUrl</td><td><input type="checkbox" /><hidden /><hidden /></td>
    this 表示当前的checkbox, this.parentNode 表示checkbox所在的td ,所在的td 有三个children 
    this.parentNode.children[1].value=!this.checked; 就是设置 第2个children 的值 为checkbox的选择状态取反 
    和下面这句是相关联的 if (ckb.Checked.ToString() != hid.Value)标示 当前的权限 是否 是改动的,如果改动 就进行数据库操作 反之则不
      

  54.   

    你这方法我看的很晕。
    gridview刚开始的时候取数据是select Powername,PowerUrl from powerinfo 里面取出来的,
    没有 license这个字段,
    而你上面那个checkbox绑定了License,
    这样会报错的。 
      

  55.   

    你能不能帮我用这个方法给我个例子看看。。
    先把权限读到datatable里。 然后根据当前行的id 遍历权限 true 就让checibox 选中 
      

  56.   

    gridview 绑定的是SELECT [PowerId], [PowerName], [PowerUrl], 'False' AS [License] FROM [PowerInfo]现在就是这么做的你有运行过程序么?
      

  57.   

    好像是必须显示的写'False' AS License
      

  58.   

    sql server 中就可以省略了
      

  59.   

     HiddenField hid, hidID;
    hiddenfield是什么类型?
    没见过
    运行过你的代码了,
    效果出不来。。
      

  60.   

    哈哈,原来.net里面有hiddenField这个类型。
    看意思是隐藏字段
    果然里面不还真有个隐藏字段类型,
    真是不好意思,
    这个低级还问。。
    不过效果还是出不来,
    我再调试看看,
    问题在哪
      

  61.   

    哇哈哈,终于看到点眉目]了
    效果能正常显示出来了, 。
    现在看看更新回数据库原来就是把那个HiddenFiled 我改成了Label....