2009-07-28 23 views
3

私はのEditItemTemplateで、方法はonItemEditingです。CheckBoxListを含むListView - チェックされていない選択項目

私のListViewには、LINQを使用してCheckBoxListがバインドされています。

私のonItemEditingの方法では、ユーザがセクタとリンクしているルックアップテーブルにある場合は、CheckBoxesを確認しようとしています。

EditItemTemplateをロードすると、CheckBoxesのいずれも、onItemEditingメソッドで選択されたものとして設定されていますが、チェックされていません。

protected void onItemEditing(object sender, ListViewEditEventArgs e) 
{ 
    ListView1.EditIndex = e.NewEditIndex; 
    ListView1.DataBind(); 

    int regId = Convert.ToInt32(((Label)ListView1.Items[e.NewEditIndex].FindControl("LblRegId")).Text); 
    CheckBoxList cbl = (CheckBoxList) ListView1.Items[e.NewEditIndex].FindControl("chkLstSectors"); 

//test to see if forcing first check box to be selected works - doesn't work 
    cbl.Items[0].Selected = true; 

    SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DaresburyConnectionString"].ToString()); 
    SqlCommand objCmd = new SqlCommand("select * from register_sectors where register_id= " + regId, objConn); 
    objConn.Open(); 

    SqlDataReader objReader = objCmd.ExecuteReader(); 

    if (objReader != null) 
    { 
     while (objReader.Read()) 
     { 
      ListItem currentCheckBox = cbl.Items.FindByValue(objReader["sector_id"].ToString()); 
      if (currentCheckBox != null) 
      { 
       currentCheckBox.Selected = true; 
      } 
     } 
    } 
} 

任意のアイデアはどのようにこの問題を回避するには:

ここでの方法は?

+0

コントロールをどこに作成しましたか?ロード時に、initで? –

答えて

1

問題は、チェックボックスリストがバインドされた後にlistViewが再度バインドされていたことです。

私はバインディングを削除して動作します!

0

私は私が私の答えで手遅れじゃない願っています。)

私は他のコントロールのようにデータバインドする必要があり、リストビューにCheckBoxListのを持っています。データベース内の値は、この列挙から算出される値である:

public enum SiteType 
{ 
    Owner = 1, 
    Reseller = 2, 
    SubReseller = 4, 
    Distributor = 8 
    Manufacturer = 16, 
    Consumer = 32 
} 

値が24である場合、これはディストリビュータとメーカー(8 + 16)を意味します。

Iは、値をデータバインディングのための私のリストビューでEditItemへのHiddenFieldを追加:

<EditItemTemplate> 
    <tr> 
     <td> 
      <asp:CheckBoxList ID="cblSiteTypes" runat="server" RepeatLayout="Flow" 
       DataSourceID="ObjectDataSource4" DataTextField="Key" DataValueField="Value" /> 
      <asp:HiddenField ID="hfSiteTypes" runat="server" Value='<%# Bind("SiteType") %>' OnDataBinding="hfSiteTypesBnd" /> 
     </td> 
    </tr> 
    <!-- other data... --> 
</EditItemTemplate> 

をCheckBoxListのが列挙からのデータを辞書オブジェクトを返す別のデータソースを介して充填されています。コードの背後では、選択のためにHiddenFieldのOnDataBindingメソッドを使用します。

protected void hfSiteTypesBnd(object sender, EventArgs e) 
{ 
    // read the value 
    HiddenField hf = (HiddenField)sender; 
    short val = Convert.ToInt16(hf.Value); 
    // find the checkboxlist 
    CheckBoxList cblSiteTypes = (CheckBoxList)hf.Parent.FindControl("cblSiteTypes"); 
    // clear the selection (may be not needed) 
    cblSiteTypes.ClearSelection(); 
    // for each item 
    foreach (ListItem li in cblSiteTypes.Items) 
    { 
     // get the value from each item and... 
     short v = Convert.ToInt16(li.Value); 
     // ...look up whether this value is matching or not 
     if ((val & v) == v) li.Selected = true; 
    } 
} 

Etvoilà!

関連する問題