![](/img/trans.png)
[英]Xamarin Slider: 'Value is an invalid value for Maximum' (Only when navigating away from a page)
[英]Remember Cascading Dropdown value when navigating away from page
我為我的組織維護一個使用 Web 窗體的應用程序。 我必須添加級聯下拉菜單,當我離開頁面時,我需要讓這些下拉菜單記住它們的值。 我的第一個下拉菜單會記住它的值,但是當我返回時,它的級聯下拉菜單不會保留它的值。 有什么建議?
以下是我的下拉列表:
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate><div class="dropDownSelection">
<asp:DropDownList CssClass="topicDropDown" ID="topic1" DataTextField="NAME" DataValueField="ID" OnSelectedIndexChanged="Load_Section1" AutoPostBack="True" AppendDataBoundItems="true" runat="server"/>
<asp:DropDownList CssClass="sectionDropDown" ID="section1" DataTextField="NAME" DataValueFile="ID" AutoPostBack="True" runat="server">
<asp:ListItem Text="--- Select Section ---" Value="0"></asp:ListItem>
</asp:DropDownList></div><br/>
</ContentTemplate>
</asp:UpdatePanel>
以下是加載下拉值的方法:
protected void Load_Topic1()
{
var topicStore = new TopicStore();
var topics = topicStore.ReadTopics();
foreach (var topic in topics)
{
var topicListItem = new ListItem(topic.Name, topic.Id.ToString());
topic1.Items.Add(topicListItem);
//topic1.Attributes.Add("Title", topic.Description);//only shows description for item at the bottom of the dropdown
}
topic1.Items.Insert(0, new ListItem("--- Select Topic ---", "0"));
}
protected void Load_Section1(object sender, EventArgs e)
{
section1.Items.Clear();
var sectionStore = new SectionStore();
var sections = sectionStore.ReadForTopic(Guid.Parse(topic1.SelectedValue));
foreach (var section in sections)
{
var sectionListItem = new ListItem(section.Name, section.Id.ToString());
section1.Items.Add(sectionListItem);
}
section1.Items.Insert(0, new ListItem("--- Select Section ---", "0"));
}
Load_Topic1 在頁面加載時調用。 當您離開頁面時,下拉列表的值存儲在會話中。
下面是我如何將值加載到會話中:
if (Session["Page"] != null)
{
if (Session["SubmittedPayment"] != null)
{
//shazbot -- they've already hit submit
Server.Transfer("default.aspx?logout=true");
}
topic1.SelectedValue = Session["topic1"] as string;
section1.SelectedValue = Session["section1"] as string;
rating1DropDown.SelectedValue = Session["rating1DropDown"] as string;
if (Session["Page"].ToString() == "HighSchoolInformation2.aspx")
{
Session.Add("Page", "InterestSurvey.aspx");
}
else if (Session["Page"].ToString() == "Payment.aspx" || Session["Page"].ToString() == "InterestSurvey.aspx")
{
Session.Add("Page", "InterestSurvey.aspx");
}
else
{
topic1.SelectedValue = Session["topic1"] as string;
section1.SelectedValue = Session["section1"] as string;
rating1DropDown.SelectedValue = Session["rating1DropDown"] as string;
Response.Redirect(Session["Page"].ToString());
}
}
else
{
//they're not logged in, send them back to log in
Server.Transfer("Default.aspx?logout=true");
}
在后面的代碼中,我像這樣加載會話變量:
protected void next_Click(object sender, EventArgs e)
{
Session.Add("topic1", topic1.SelectedValue);
Session.Add("section1", section1.SelectedValue);
Session.Add("rating1DropDown", rating1DropDown.SelectedValue);
Page.Validate();
if (Page.IsValid)
{
ModLangRequired.Visible = false;
if (!checkModLang())
{
Response.Redirect("Payment.aspx");
}
}
}
就像我說的那樣,我繼承了這段代碼,目前我沒有時間完全重寫。
首先按如下方式更改Load_Section1
。 請注意我們如何使用Guid.TryParse
在選擇主題時有條件地加載部分。
protected void Load_Section1()
{
section1.Items.Clear();
section1.Items.Add(new ListItem("--- Select Section ---", "0"));
Guid topicId;
if (Guid.TryParse(topic1.SelectedValue, out topicId))
{
var sectionStore = new SectionStore();
var sections = sectionStore.ReadForTopic(topicId);
foreach (var section in sections)
{
var sectionListItem = new ListItem(section.Name, section.Id.ToString());
section1.Items.Add(sectionListItem);
}
}
}
然后添加一個新的事件處理程序,如下所示:
protected void TopicDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
{
Load_Section1();
}
現在將OnSelectedIndexChanged
事件關聯到新的處理程序:
<asp:DropDownList ID="topic1" ... OnSelectedIndexChanged="TopicDropDown_OnSelectedIndexChanged" ... />
現在您可以按如下方式恢復頁面狀態:
if (Session["Page"] != null)
{
if (Session["SubmittedPayment"] != null)
{
//shazbot -- they've already hit submit
Server.Transfer("default.aspx?logout=true");
}
Load_Topic1();
topic1.SelectedValue = IsPostBack ? Request.Form[topic1.UniqueID] : (string)Session["topic1"];
Load_Section1();
section1.SelectedValue = IsPostBack ? Request.Form[section1.UniqueID] : (string)Session["section1"];
Load_Rating1DropDown(); // not sure if you need this???
rating1DropDown.SelectedValue = IsPostBack ? Request.Form[rating1DropDown.UniqueID] : (string)Session["rating1DropDown"];
if (Session["Page"].ToString() == "HighSchoolInformation2.aspx")
{
Session.Add("Page", "InterestSurvey.aspx");
}
else if (Session["Page"].ToString() == "Payment.aspx" || Session["Page"].ToString() == "InterestSurvey.aspx")
{
Session.Add("Page", "InterestSurvey.aspx");
}
else
{
// you don't actually need to set values before you redirect
Response.Redirect(Session["Page"].ToString());
}
}
else
{
//they're not logged in, send them back to log in
Server.Transfer("Default.aspx?logout=true");
}
我的假設是上面的代碼是從Page_Load
調用的。 避免對Load_Topic1
進行任何額外調用。
實際上發生的事情是,您的級聯下拉列表(第 1 部分)在選擇的主題 1 上加載它的項目,當頁面刷新時,主題 1 從托管的 ViewState 中獲取它的值,而第 1 部分呈現空白...
有2種解決方案:
在腳本中管理它
將 section1 的 selectedValue 存儲在 ViewState 或 localStorage(瀏覽器存儲)等任何存儲中,並且在文檔准備就緒時,您可以通過 ajax 調用針對 topic1 中選擇的項目獲取 section1 的項目,並設置存儲在存儲中的選定值。 (聽起來很努力,請檢查以下選項)
在后端代碼中管理它
在您的 Page_Load 方法中,調用 Load_Section1 並傳遞 topic1.SelectedValue
不要忘記放置 Page.IsPostBack 條件以避免錯誤...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.