3

メソッドを呼び出すListViewwithがあります。EditItemTemplateonItemEditing

私の中で私はを使用してバインドをListView持っています。CheckBoxListLINQ

私のonItemEditing方法では、CheckBoxesユーザーとセクターをリンクするルックアップテーブルにそれらが存在するかどうかを確認しようとしています。

ただし、ロードすると、メソッドで選択されたものとして設定したにもかかわらず、チェックされていEditItemTemplateないものがあります。CheckBoxesonItemEditing

方法は次のとおりです。

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;
            }
        }
    }
}

これを回避する方法はありますか?

4

2 に答える 2

1

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

バインディングを削除しましたが、機能します。

于 2009-07-31T10:06:37.170 に答える
0

私は私の答えに遅すぎないことを願っています;)

他のコントロールのように DataBind する必要がある ListView に CheckBoxList があります。データベースの値は、次の列挙から計算された値です。

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

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

値をデータバインドするために、ListView の 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 は、列挙からのデータを含む Dictionary オブジェクトを返す別の DataSource を介して入力されます。コード ビハインドでは、選択のために 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;
    }
}

ほら!

于 2014-09-11T13:24:10.650 に答える