我有一个主要从 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>
此外,我添加了 1 行不在数据库中:
//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;