【发布时间】: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=1和FormSection='Section A';FormSectionID=2两者是不同的。 -
@TimSchmelter 是的,我只是通过 sql 进行了测试以确保,对于我正在使用的测试用例,即使没有不同的 formsection 也是一样的。