开发者

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.

First Way : I need to pass a string parameter from page to master how I can do that ?

I'm going to pass a string param开发者_JAVA百科eter between page to master when controller request page and then page pass parameter to it's master.

Second way : Passing some string parameters from a controller class to master page at first (not to page and then page to it's master).

Third Way : 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";
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜