ListView containing CheckBoxList - selected items that do not appear as checked

I have ListViewwith EditItemTemplatethat is the method onItemEditing.

In mine ListViewI have a binding CheckBoxListusing LINQ.

In my method, onItemEditingI try to check for specific ones CheckBoxesif they are present in the lookup table, which associates users with sectors.

However, when I download EditItemTemplate, none of CheckBoxesthem are checked, even if I set them as selected in the method onItemEditing.

Here's the Method:

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

Any ideas how to get around this?

+3
source share
2

, listView checkboxlist.

, !

+1

, ;)

CheckBoxList ListView, DataBind, . :

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

24, (8 + 16).

HiddenField EditItem ListView :

<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 DataSource, Dictionary . OnDataBinding HiddenField :

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

Et voilà!

0

Source: https://habr.com/ru/post/1713745/


All Articles