我在带我自己的selectedvalue属性的detailsview内部有列表框。
当我设置Appenddatabounditems="true"
时,Listbox的值将重复,否则,当我设置"AppendDatabounditems="false"
时,Listbox selectedvalue无法工作。
请告诉我,如何避免在Listbox中重复。
我的代码段如下。
[ASPX页面]
<asp:DetailsView ID="dvProfile" runat="server" AutoGenerateRows="False" DataSourceID="odsProfileData">
<Fields>
<asp:BoundField DataField="FullName" HeaderText="Full Name" HeaderStyle-Width="100px" />
<asp:TemplateField HeaderText="Products">
<ItemTemplate>
<iac:MyMultiSelectionDropDown ID="ListBox1" OnDataBound="Databound" runat="server"
DataSourceID="odsProducts" DataTextField="FullName" DataValueField="ID" AppendDataBoundItems="true"
SelectionMode="Multiple" SelectedValuesCSV='<%# Bind("Products") %>'>
</iac:MyMultiSelectionDropDown>
</ItemTemplate>
<EditItemTemplate>
<iac:MyMultiSelectionDropDown ID="ListBox1" OnDataBound="Databound" runat="server"
DataSourceID="odsProducts" DataTextField="FullName" DataValueField="ID" AppendDataBoundItems="true"
SelectionMode="Multiple" SelectedValuesCSV='<%# Bind("Products") %>'>
</iac:MyMultiSelectionDropDown>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="button" ShowEditButton="true" EditText="Edit User Profile" />
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="odsProfileData" runat="server" SelectMethod="GetProfileData"
TypeName="MultiSelectionTest.App_Code.TestData" />
<asp:ObjectDataSource ID="odsProducts" runat="server" SelectMethod="GetProducts"
TypeName="MultiSelectionTest.App_Code.TestData" />
[TestData.CS]
//Using this class file i filled the ListBox.
public class TestData
{
public List<ProfileData> GetProfileData()
{
List<ProfileData> ret = new List<ProfileData>();
ret.Add(new ProfileData("Test1", "1,2"));
return ret;
}
public List<Product> GetProducts()
{
List<Product> ret = new List<Product>();
ret.Add(new Product(1, "Product1"));
ret.Add(new Product(2, "Product2"));
ret.Add(new Product(3, "Product3"));
ret.Add(new Product(4, "Product4"));
return ret;
}
public class ProfileData
{
public ProfileData(string fullName, string products)
{
FullName = fullName;
Products = products;
}
public string FullName { get; set; }
public string Products { get; set; }
}
public class Product
{
public Product(int id, string fullName)
{
ID = id;
FullName = fullName;
}
public int ID { get; set; }
public string FullName { get; set; }
}
}
[MyMultiselectionDropDown.cs]
public string SelectedValuesCSV
{
get
{
string ret = string.Empty;
foreach (System.Web.UI.WebControls.ListItem li in this.Items)
{
if (li.Selected)
{
ret += "," + li.Value.ToString();
}
}
return ret.TrimStart(',');
}
set
{
if (!_noUpdate)
{
_noUpdate = true;
this.DataBind();
}
_noUpdate = false;
IdContainer values = new IdContainer(value);
foreach (string val in values)
{
System.Web.UI.WebControls.ListItem li = this.Items.FindByValue(val);
if (li != null)
li.Selected = true;
}
//MultiSelectionTest.App_Code.TestData nn = new TestData();
//object bb = nn.GetProfileData();
}
}
使用此类文件,我得到SelectedValues。
请告诉我。
最佳答案
AppenddataboundItems =“ true”表示
您要添加所有产品或(来自数据库的数据)+“选择”
在列表框中