2011-12-25 10 views
1

私はそのアイテムの一部をチェックしたいチェックボックスリストを持っています。 データベースにストアをチェックしたいのですが、データベースから選択してループしていますが、最後に選択したアイテムです:((:checkboxlistの項目をチェックする方法は?

var selectedRoles = (from r in DataContext.Context.Core_PagesPermission 
        where r.PageName.Contains(pageName) select r) 
         .FirstOrDefault(); 
if(selectedRoles != null) 
{ 
    string roles = selectedRoles.Roles; 
    string[] role = roles.Split(','); 
    int countTags = role.Count(); 
    foreach (string word in role) 
    { 
     CheckBoxList1.SelectedValue = word; 
     Response.Write(word + "<br/>"); 
     countTags -= 1; 
    } 
} 

これが働いた:

var selectedRoles = (from r in DataContext.Context.Core_PagesPermission where r.PageName.Contains(pageName) select r) 
          .FirstOrDefault(); 
         dsRoles.DataBind(); 
         CheckBoxList1.DataBind(); 
         if(selectedRoles != null) 
         { 
         string roles = selectedRoles.Roles; 
         string[] role = roles.Split(','); 
         foreach (string word in role) 
         { 
          try 
          { 
           CheckBoxList1.Items.FindByValue(word).Selected = true; 
          } 
          catch (Exception exp) 
          { 
           lbError.Text= exp.ToString(); 
          } 
+0

あなたがあなたの質問をより正確にしてください可能性があります。私は前提を正しく守っています:あなたはすべての権限を表示するチェックボックスリストを持っていて、その権限をDBでチェックしたいのですか? –

+0

はい私の純粋な英語のために申し訳ありません –

答えて

2

をあなたは個々の項目を選択することになるでしょう:

CheckBoxList1.Items.FindByText(word).Selected = true; 

または

CheckBoxList1.Items.FindByValue(word).Selected = true; 

ただし、チェックボックスリストに探しているテキスト/値がないと、エラーが発生します。

+0

私はエラーが発生しました:(しかし、私はすべてのアイテムを持っています –

+0

リスト全体をループし、あなたのリストのそれぞれの単語との比較を行い、そのようにチェックボックスを選択することもできます。 i = 0; iはCheckBoxList1.Items.Count <; iは++){ \t IF(CheckBoxList1.Items [I]の.text ==ワード) \t \t {CheckBoxList1.Items [I] .Selected = TRUE;}} '.Textまたは.Valueを使用して、見つからない値、スペルの違い、またはエラーを投げている理由を比較して調べます。 –

0

DataTableのメソッドCheckBoxListの

for (int i = 0; i < dt.Rows.Count; i++) 
{ 
    chkCategories.Items.FindByValue(dt.Rows[i]["CategoryID"].ToString()).Selected = true; 
} 
関連する問題