C# 包含复选框列表的ListView-未显示为选中的选定项
我有一个C# 包含复选框列表的ListView-未显示为选中的选定项,c#,asp.net,linq,listview,checkboxlist,C#,Asp.net,Linq,Listview,Checkboxlist,我有一个ListView和一个EditItemTemplate调用方法onItemEditing 在我的列表视图中有一个使用LINQ绑定的复选框列表 在我的onItemEditing方法中,我试图选中某些复选框,如果它们出现在链接用户和扇区的查找表中 但是,当我加载EditItemTemplate时,没有选中任何复选框,即使我已将它们设置为在onItemEditing方法中选中 方法如下: protected void onItemEditing(object sender, ListViewE
ListView
和一个EditItemTemplate
调用方法onItemEditing
在我的列表视图中
有一个使用LINQ
绑定的复选框列表
在我的onItemEditing
方法中,我试图选中某些复选框,如果它们出现在链接用户和扇区的查找表中
但是,当我加载EditItemTemplate
时,没有选中任何复选框
,即使我已将它们设置为在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;
}
}
}
}
有什么办法解决这个问题吗?问题是在绑定复选框列表之后,listView又被绑定了
我卸下了装订,它就工作了 我希望我的回答不会太晚;)
我在ListView中有一个复选框列表,它应该像其他控件一样进行数据绑定。数据库中的值是根据此枚举计算的值:
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>
瞧 你在哪里创建控件的?加载,初始化?
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;
}
}