如何将母版页中使用的下拉选择值保留到 asp.net 中的所有内容页

How to persist a dropdown selected value used in Master page to all content pages in asp.net

我正在设计工作门户。我有 母版页,在母版页中,我使用了 Select 国家下拉列表 .

我在所有 内容页面.

中显示基于用户 select 国家/地区 的工作

我只希望一个用户在一个国家/地区只 select 一次,并且在所有内容页面中保持 selected。我不希望他在每个内容页面上一次又一次地 select。

我只想在整个网站导航过程中保留下拉菜单select编辑的值,除非用户再次更改它。

这取决于您对母版页和内容页的实施,但其中之一是使用 cookie 或用户配置文件来保存用户偏好以供选择国家/地区。然后您的母版页可以读取此数据(来自 cookie 或配置文件)以显示选择。

最后,我认为您希望将用户选择保留在导航期间所有页面都可以访问的某个位置。母版页和内容页只是一种读取此信息的机制。

您可以使用 Session 来存储 DropDownList 的值

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="Netherlands" Value="nl-NL"></asp:ListItem>
    <asp:ListItem Text="England" Value="en-GB"></asp:ListItem>
    <asp:ListItem Text="Germany" Value="de-DE"></asp:ListItem>
</asp:DropDownList>

隐藏代码,在每次页面加载时设置正确的下拉列表值。您现在可以使用 Session["language"] 的值来过滤您的数据。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //check if the session exists and select the correct value in the dropdownlist
        if (Session["language"] != null)
        {
            DropDownList1.SelectedValue = Session["language"].ToString();
        }
        else
        {
            //set the session with the default language
            Session["language"] = "en-GB";
        }
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //set the session based on the dropdownlist value
    Session["language"] = DropDownList1.SelectedValue;
}