无法删除填充列表框中的重复值
本文关键字:删除 填充 列表 | 更新日期: 2023-09-27 18:37:24
我试图寻找其他解决方案,但我似乎找不到答案。我目前正在使用列表框使用 sqldatasource 显示数据库中的数据。在我事先从下拉列表中选择一个值后,它就会填充。使用列表框时,我想在单击后保存所选项目,因此我将追加数据绑定项设置为 true。似乎当我将其设置为 false 时,我不再获得重复值,但是绑定后我无法保留列表框的选定值。我也尝试启用/禁用视图状态,但没有运气。我很肯定我在查询中使用了 DISTINCT 关键字,但仍然没有运气。
ASP.Net
<asp:DropDownList ID="ProgramDropDownList" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Program" DataValueField="ProgramID">
</asp:DropDownList>
<asp:DropDownList ID="ReportPeriodDropDownList" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="ReportLabel" DataValueField="DataCollectionPeriodID" Height="21px" Width="172px">
</asp:DropDownList>
<div style="width:400px; height:auto; overflow:auto; text-align: center; margin-left: auto; margin-right: auto; left: 0; right: 0">
<asp:ListBox ID="FormSectionListBox" DataSourceID="FormSectionDataSource" runat="server" AutoPostBack="True" DataTextField="FormSection" DataValueField="FormSectionID" AppendDataBoundItems="True" EnableViewState="False">
</asp:ListBox>
</div>
<asp:SqlDataSource ID="FormSectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SmartFormConnection %>" SelectCommand="SELECT DISTINCT FormSection, FormSectionID FROM Core.FormSection_Lkup
where formsectionid IN (select formsectionid from core.form_section_subsection_item_rel where datacollectionperiodid = @datacollectionperiodid) order by FormSection">
<SelectParameters>
<asp:ControlParameter ControlID="ReportPeriodDropDownList" Name="datacollectionperiodid" PropertyName="SelectedValue" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
_connection = DataAccess.SelfRef().GetConnection();
string eTarget = Request.Params["__EVENTTARGET"];
if (string.IsNullOrEmpty(eTarget)) return;
var list = InstructionDropDown.SelectedValue;
switch (list)
{
case "Form Section":
FormSectionListBox.DataSourceID = "FormSectionDataSource";
FormSectionListView.DataBind();
RenderView(FormSectionListView, "hidden"); // hide listview on page load
break;
}
}
我想
你只需要确保列表不会在每次回发时都填写:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// do what you need on the initial load
}
}
如果需要处理DropDownList
选择,请使用相应的事件。在本例中为SelectedIndexChanged
事件。
protected void InstructionDropDown_SelectedIndexChanged(Object sender, EventArgs e)
{
var list = InstructionDropDown.SelectedValue;
switch (list)
{
case "Form Section":
FormSectionListBox.DataSourceID = "FormSectionDataSource";
FormSectionListView.DataBind();
RenderView(FormSectionListView, "hidden"); // hide listview on page load
break;
}
}
}
我能够找到解决方法。我将 AppendDataBoundItems 设置为 false。并使用 SelectedIndexChanged 属性在绑定后保存选定的索引。
protected void FormSectionListBoxSelectedIndexChanged(object sender, EventArgs e)
{
var item = FormSectionListBox.SelectedIndex;
FormSectionListBox.DataSourceID = "FormSectionDataSource";
FormSectionListView.DataBind();
FormSectionListBox.SelectedIndex = item;
}