页面1:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="AuthorCode" HeaderText="AuthorCode" 
                    SortExpression="AuthorCode" />
                <asp:BoundField DataField="AuthorName" HeaderText="AuthorName" 
                    SortExpression="AuthorName" />
                <asp:BoundField DataField="AuthorLevel" HeaderText="AuthorLevel" 
                    SortExpression="AuthorLevel" />
                <asp:HyperLinkField AccessibleHeaderText="编辑" 
                    DataNavigateUrlFields="ForbidOpers" 
                    DataNavigateUrlFormatString="Default3.aspx?id={0}" 
                    HeaderText="编辑" Text="编辑" InsertVisible="False" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DCProjectDbConnectionString%>" 
            SelectCommand="SELECT [AuthorCode], [AuthorName], [AuthorLevel], [ForbidOpers] FROM [CF_PM_AuthorType]">
        </asp:SqlDataSource>页面2: <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="OperName" 
            DataValueField="OperName">
        </asp:CheckBoxList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DCProjectDbConnectionString %>" 
            SelectCommand="SELECT [OperName], [OperCode] FROM [CF_PM_OperType]">
        </asp:SqlDataSource>
数据表1:CF_PM_AuthorType 和 数据表2:CF_PM_OperType 之间存在关系(CF_PM_AuthorType中字段ForbidOpers值为多个,其中用逗号分隔,例如:32783,32784 ;CF_PM_OperType中字段 OperCode值为一个,分别为32783,32784)
要实现的功能:页面2读取出CF_PM_OperType数据表2中的所有记录,并以复选框显示,当页面1中传递数据表1中ForbidOpers字段多个值到页面2时,页面2中读取出来的复选框磨人勾选

解决方案 »

  1.   

    循环所有的checkbox,如果value在ForbidOpers中就设置checked 为true
      

  2.   

    我循环了,可是有问题,能否给写个例子啊
    下面是我写的
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.Configuration;
    using System.Data;
    public partial class Default3 : System.Web.UI.Page
    {
        public string strArrays = null;
        public int j;
        protected void Page_Load(object sender, EventArgs e)
        {
            String connectionString = ConfigurationManager.ConnectionStrings["DCProjectDbConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            String id = Request.QueryString["id"];
            String[] array=id.Split(',');
            string sql = "";
            for (j = 0; j < array.Length; j++)
            { 
                sql = "select OperName from CF_PM_OperType where OperCode ='" + array[j] + "'";
            }        SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string strTerm = dr["OperCode"].ToString();
                string[] strTerms = strTerm.Split(',');
                foreach (ListItem lItem in CheckBoxList1.Items)
                {
                    for (int i = 0; i < strTerms.Length; i++)
                    {
                        if (lItem.Value == strTerms[i])
                        {
                            lItem.Selected = true;
                            break;//加上这个
                        }
                    }
                }
            }
            dr.Close();
            con.Close();
        }
    }
      

  3.   

    strin str=Request.QueryString[""].ToString();
    foreach (ListItem li in CheckBoxList1.Items)
    {
     foreach(string s in str.Split(','))
     { if (li.Value.Equals(s))
            li.Selected = true;
     }
    }
    或使用List<string>判断是否contains(li.Value)
      

  4.   

    这段代码是写在while (dr.Read()){}里面吗
      

  5.   

    既然是复选框,就可能多选咯、判断选中一个后为什么break了你的问题是什么