下拉列表不保存回发的位置

时间:2013-07-19 14:59:45

标签: c# asp.net drop-down-menu

我有一个下拉列表,主要来自sqldatasource:

<asp:DropDownList ID="searchApplicationDropDown" runat="server" OnDataBound="searchApplicationDropDown_DataBound" DataSourceID="SqlDataSource8" DataTextField="AppName" DataValueField="PK_Application" AutoPostBack="true"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource8" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT [PK_Application], [AppName] FROM [Application] ORDER BY CASE WHEN [PK_Application] = 1 THEN 1 ELSE 2 END, [AppName]">
</asp:SqlDataSource>

此外,我在数据库中添加了一行:

//add default non selected row outside of database to drop down list
protected void searchApplicationDropDown_DataBound(object sender, EventArgs e)
{
    DropDownList list = sender as DropDownList;
    if (list != null)
    {
        list.Items.Insert(0, "--All Applications--");
    }
}

当我回发下拉列表时,不保存它的最后一个选定位置。我无法弄清楚原因。

编辑:

/* ===== PAGE LOAD ===== */
protected void Page_Load(object sender, EventArgs e)
{
    //default tab behavior if not a postback
    if (!IsPostBack)
    {
        //bind all datafields on startup
        dataBindFields();
        catagoryDropDown.DataBind();
        applicationDropDown.DataBind();
        //populate the input fields with the most recent story of the user currently logged in on startup, 
        //the user drop down menu automatically puts to the top and selects the currently logged in user when the user first goes to the page
        populateMostRecentStory(sqlQueryReturnString(getPKofUserLoggedIn).ToString());
        //preliminary behavior of panels tabs, tab settings
        Tab1.CssClass = "Clicked";
        resetTabControlValues();
        //set default date range in the search inputs
        searchDateFrom.SelectedDate = DateTime.Today.AddDays(-30);
        searchDateTo.SelectedDate = DateTime.Now;
        //set GridView SQL parameters to default search values intitially and application to all applications
        SqlDataSource6.SelectParameters["fromDateParam"].DefaultValue = searchDateFrom.SelectedDate.ToString();
        SqlDataSource6.SelectParameters["toDateParam"].DefaultValue = searchDateTo.SelectedDate.AddDays(1).ToString();
        SqlDataSource6.SelectParameters["applicationParam"].DefaultValue = "%";
        //set gridview parameter with logged in user initially
        SqlDataSource6.SelectParameters["userIdSearchParam"].DefaultValue = sqlQueryReturnString(getPKofUserLoggedIn);
    }
    //shows user whether they are working on a new story or an existing story
    showStoryInfo();
    searchApplicationDropDown.DataBind();
    //set user drop down list server control SQL parameter to currently logged in user so that they are at the top of the list and selected by default
    SqlDataSource1.SelectParameters["userLoggedIn"].DefaultValue = User.Identity.Name;

1 个答案:

答案 0 :(得分:0)

确保页面和控件的EnableViewState属性为true。