【问题标题】:Cannot get rid of duplicate values in populated listbox无法摆脱填充列表框中的重复值
【发布时间】:2015-01-13 22:26:26
【问题描述】:

我试图查看其他解决方案,但似乎找不到答案。我目前正在使用列表框来显示使用 sqldatasource 的数据库中的数据。它是在我事先从下拉列表中选择一个值后填充的。使用列表框时,我想在单击后保存所选项目,因此我将 AppendDataBoundItems 设置为 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;

        }
    }

【问题讨论】:

  • 你提到你使用DISTINCT,但是在哪里?
  • @TimSchmelter 我将展示我的 sql 数据源
  • 您的 sql 查询使用 SELECT DISTINCT FormSection, FormSectionID。你知道DISTINCT 比较两列,所以如果你有FormSection='Section A';FormSectionID=1FormSection='Section A';FormSectionID=2 两者是不同的。
  • @TimSchmelter 是的,我只是通过 sql 进行了测试以确保,对于我正在使用的测试用例,即使没有不同的 formsection 也是一样的。

标签: c# asp.net listbox


【解决方案1】:

我猜你只需要确保每次回发时都不会填写列表:

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

【讨论】:

  • 是的,我已经尝试过 !Page.IsPostBack 但是在我的应用程序中,我的第一个下拉列表填充了我的第二个下拉列表,然后它会根据第二个下拉列表中的第一个值自动填充我的列表框。当我使用 !PageIsPostBack 时,它不会在开始时填充它,因为第二个下拉列表是回发但我希望它显示。我希望这很清楚。
  • 我会尝试使用 selectedindexchanged 来看看我能到哪里
  • 我肯定还缺少其他东西,因为即使使用 !Page.IsPostBack 和 SelectedIndexChanged 它仍然显示重复项。有什么办法可以解决 AppendDataBoundItems 属性?
【解决方案2】:

我找到了解决办法。我将 AppendDataBoundItems 设置为 false。并使用 SelectedIndexChanged 属性在 Binding 后保存选中的索引。

protected void FormSectionListBoxSelectedIndexChanged(object sender, EventArgs e)
{
    var item = FormSectionListBox.SelectedIndex;
    FormSectionListBox.DataSourceID = "FormSectionDataSource";
    FormSectionListView.DataBind();
    FormSectionListBox.SelectedIndex = item;
}

【讨论】:

    最近更新 更多