简体   繁体   English

如何将查询字符串中的参数传递给多个页面?

[英]How to pass a parameter in a querystring to multiple pages?

I want to change my site (consists of multiple pages) theme dynamically. 我想动态更改我的网站(由多个页面组成)的主题。 I pass a parameter in a querystring as "theme" to all the solution. 我将查询字符串中的参数作为“主题”传递给所有解决方案。 It works just for one page that is redirected with query string (It's theme changes correctly), but other webforms' theme don't change. 它仅适用于使用查询字符串重定向的页面(其主题正确更改),但其他Webforms的主题不变。

so how can I pass a parameter in a querystring to multiple pages? 那么如何将查询字符串中的参数传递给多个页面?

the below code is an Event that when user click the button, redirect him to other page and change the theme of sit. 下面的代码是一个Event,当用户单击按钮时,将他重定向到其他页面并更改sit的主题。

protected void btn_blue_gray_Click(object sender, EventArgs e)
        {
            String bl = "blue";
            Response.Redirect("page1.aspx?theme=" + bl);
        }

if it helps: it's asp.net 4.5 and we used Microsoft.AspNet.Identity package for login User. 如果有帮助,它是asp.net 4.5,我们使用Microsoft.AspNet.Identity包来登录User。

When you navigate to other pages you lose the query string passed in unless you add it to the route/links to other pages yourself. 导航到其他页面时,除非您自己将其添加到其他页面的路由/链接中,否则丢失传递的查询字符串。

When you pass a parameter to your page (assuming you login you can do it there) store the theme choice in a bit of state (cookie or session or MemoryCache or something) then just look for this value on each page (or stick it into a MasterPage or baseView with the default being your standard theme value. 当您将参数传递给页面(假设您可以在此处登录)时,将主题选择存储在某种状态(cookie或会话或MemoryCache或其他状态)中,然后仅在每个页面上查找该值(或将其粘贴到MasterPage或baseView,默认值为您的标准主题值。

something like 就像是

return Session["theme"] ?? "default";

Then every page derived from that MasterPage or View will have access to the theme value (regardless of where you set it). 然后,从该MasterPage或View派生的每个页面都可以访问主题值(无论您在何处进行设置)。

If you want to allow the user to change the value then set it behind your button click and then navigate or refresh the page and the new theme value will be picked up automatically. 如果要允许用户更改值,请在单击按钮后将其设置,然后浏览或刷新页面,新主题值将自动被拾取。

You would need to set the theme in a global class, which all web forms can access. 您需要在所有网络表单都可以访问的全局类中设置主题。

public static class Theme { public static string theme = ""; public static class Theme {public static string theme =“”; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM