无法摆脱填充列表框中的重复值

时间:2015-01-13 13:07:02

标签: c# asp.net listbox

我试过看其他解决方案,但我似乎无法找到答案。我目前正在使用列表框来使用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;

        }
    }

2 个答案:

答案 0 :(得分: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;
        }
    }
}

答案 1 :(得分:0)

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

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