简体   繁体   中英

Pass string parameters to master page

I'm using C# language Asp.net version 3.5 I have a masterpage and pages which they use master. I think there are three ways to implement my application with ability passing parameters between controller to pages.

I need to pass a string parameter from page to master how I can do that ? 我需要从页面传递一个字符串参数,以掌握如何做到这一点?
I'm going to pass a string parameter between page to master when controller request page and then page pass parameter to it's master.

Passing some string parameters from a controller class to master page at first (not to page and then page to it's master). 一些字符串参数从控制器类传递到母版页(而不是先传递到母版页,再传到母版页)。

Passing to another class or field that must be static and then master load it's value. 传递到另一个必须为静态的类或字段,然后主加载其值。

Which is the best or possible way I mean with high performance and security.

Let's assume you have a master called MyMaster with a public method "Foo". You also have a page called MyPage.

In a MyPage instance, you can use the Master property to retrieve the reference to the master. If you cast that reference, you can call the Foo method on it.

((MyMaster)myPage.Master).Foo("some string")

So basically, as long as you have access to a page instance, you can always access the master's public members.

If what you need is to have some string values accessible to your MasterPage or any other page in the application, you could try one of the many client-side state management that ASP.NET supports:

  • View state
  • Control state
  • Hidden fields
  • Cookies
  • Query strings

Look at this page for details on how to use each one of these options and help you find the correct one:

If you are trying to do this on the server side, you could expose a property in your master page. The "child" page can then access the property via the Page.Master property.

For example:

public partial class MyMaster : MasterPage
{
    public string MyString { get; set; }
}

public partial class MyPage : Page  
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((MyMaster)(this.Master)).MyString = "some value";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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