DropDownList重置为第一个项目

时间:2015-11-03 06:02:04

标签: c# asp.net webforms

我有两个绑定到objectdatasource下拉列表。

<asp:DropDownList ID="ddlProgram" runat="server" DataSourceID="ProgramsDS" 
     DataTextField="NAME" DataValueField="ID" AutoPostBack="True" 
     OnSelectedIndexChanged="ddlProgram_SelectedIndexChanged">
</asp:DropDownList>
<asp:ObjectDataSource ID="ProgramsDS" runat="server" 
     OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
     TypeName="WebAdminTools.DATA.DSTableAdapters.ProgramTA" 
     UpdateMethod="Update">
</asp:ObjectDataSource>
<br/>
<asp:DropDownList runat="server" ID="ddlVersion" AutoPostBack="True" 
     OnSelectedIndexChanged="ddlVersion_SelectedIndexChanged" Width="158px" 
     DataSourceID="VersionDS" DataTextField="VERSION" 
     DataValueField="PROGRAM_ID"/>
<asp:ObjectDataSource ID="VersionDS" runat="server" 
     OldValuesParameterFormatString="original_{0}" 
     SelectMethod="GetData" 
     TypeName="WebAdminTools.DATA.DSTableAdapters.UpdateTA">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlProgram" DefaultValue="0" 
             Name="P_ID" PropertyName="SelectedValue" Type="Int32"/>
    </SelectParameters>
</asp:ObjectDataSource>

我也有第二个下拉列表的onSelectionIndexChanged事件处理程序。当我尝试从第二个下拉列表中选择项目时,它将重置为第一个。我试过这个guide我做错了什么?

编辑:

protected void ddlVersion_SelectedIndexChanged(object sender, EventArgs e)
{
    GetCompaniesForUpdate();
}

private void GetCompaniesForUpdate()
{
    ProgramVersionTA ta = new ProgramVersionTA();
    var dt = new DS.ProgramsVersionsDataTable();
    ta.Fill(dt, Convert.ToInt32(ddlVersion.SelectedValue));
    var selectedVersion = Version.Parse(ddlVersion.SelectedValue);
    var companyForUpdate = dt.Where(c => Version.Parse(c.MAX_AVAILABLE_VERSION) > selectedVersion
                                         && Version.Parse(c.VERSION) < selectedVersion);
    lbCompanies.DataSource = companyForUpdate;
    lbCompanies.DataMember = "NAME";
    lbCompanies.DataBind();
}

1 个答案:

答案 0 :(得分:0)

我认为这是因为回发。如果你在form_load上设置一个dropdownlist的值,请确保你把它放在if(!Page.IsPostBack)中。