【问题标题】:ListView containing CheckBoxList - Selected items not showing as checkedListView 包含 CheckBoxList - 所选项目未显示为已选中
【发布时间】:2009-07-28 09:37:19
【问题描述】:

我有一个ListView 和一个EditItemTemplate,它调用了一个方法onItemEditing

在我的ListView 中,我有一个使用LINQ 绑定的CheckBoxList

在我的onItemEditing 方法中,我尝试检查某些CheckBoxes 是否存在于将用户与部门链接的查找表中。

但是,当我加载 EditItemTemplate 时,即使我在 onItemEditing 方法中将它们设置为选中,也不会检查任何 CheckBoxes

方法如下:

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

任何想法如何解决这个问题?

【问题讨论】:

  • 您在哪里创建了控件?加载时,初始化时?

标签: c# asp.net linq listview checkboxlist


【解决方案1】:

问题是在checkboxlist绑定后listView又被绑定了。

我删除了绑定,它起作用了!

【讨论】:

    【解决方案2】:

    我希望我的回答不会太晚;)

    我在 ListView 中有一个 CheckBoxList,它应该像其他控件一样 DataBind。数据库中的值是从这个枚举中计算出来的值:

    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 是通过另一个 DataSource 填充的,该 DataSource 返回一个 Dictionary 对象,其中包含来自枚举的数据。在后面的代码中,我使用 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;
        }
    }
    

    等等!

    【讨论】:

      猜你喜欢
      • 2010-11-12
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多