离开页面时,请记住级联下拉列表的值

时间:2018-11-07 17:19:17

标签: c# asp.net webforms dropdown cascadingdropdown

我为使用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");
            }
        }
    }

就像我上面说的那样,我继承了这段代码,目前没有时间进行完全重写。

2 个答案:

答案 0 :(得分:1)

首先按如下所述更改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 :(得分:0)

实际上,发生的是,您的层叠下拉列表(section1)将其加载到选择topic1的项目中,并且当页面刷新时,topic1从托管的ViewState中获取其值,section1呈现空白...

有2种解决方案:

通过脚本进行管理

将section1的selectedValue存储在诸如ViewState或localStorage(浏览器存储)之类的任何存储中,并且在准备好文档后,您可以通过ajax调用针对topic1中选择的项目获取section1的项目,并设置存储在存储中的所选值。 (听起来很努力,请检查下面的选项)

通过后端代码进行管理

在您的Page_Load方法中,调用Load_Section1并传递topic1.SelectedValue

别忘了放置Page.IsPostBack条件以避免错误...