我在 detailsview 中有 Listbox 和我自己的 selectedvalue 属性。
当我设置时Appenddatabounditems="true"
,Listbox 的值会被复制,否则,当我设置时"AppendDatabounditems="false"
,Listbox selectedvalue 不起作用。
请建议我,如何避免列表框中的重复。
我的代码片段如下。
[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" />
[测试数据.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。
请给我建议。