简体   繁体   English

在asp.net中从母版页获取值到子页面

[英]Getting values from master page to child page in asp.net

I have a master page masterpage.master in which i have stored a value in a variable that is 我有一个母版页masterpage.master ,其中我已经在一个变量中存储了一个值

string Name = (string)(Session["myName"]);

Now i want to use the value which is in "Name" into the child pages of masterpage.master but without using session on every page. 现在我想在masterpage.master的子页面中使用“Name”中的值,但不在每个页面上使用session。 Can I achieve that? 我能做到吗? If yes..then please tell. 如果是..那么请告诉我。

I am using c# and ASP.net webforms 我正在使用c#ASP.net webforms

You can try like this: 你可以尝试这样:

   // Master Page File (Storing the value in label)
    string Name = (string)(Session["myName"]);
    lblmsg.Text= name;

   // cs File
    Label str = Master.FindControl("lblmsg") as Label;
    TextBox10.Text = str.Text ;

You can put Name in a control ie TextBox on MasterPage and find that in content pages like this. 您可以将Name放在一个控件中,即MasterPage TextBox ,并在像这样的内容页面中找到它。

// On Master page
TextBox mastertxt = (TextBox) Master.FindControl("txtMaster");

// On Content Pages
lblContent.Text = mastertxt.Text;

For more details on it check this on MSDN 有关它的更多详细信息,请在MSDN上查看

You can access your masterpage from current page and cast it to your class type: 您可以从当前页面访问您的母版页并将其转换为您的班级类型:

MyMasterPage master = Master as MyMasterPage;
var value = master.NeededProperity;

Looks like here in MSDN in comments: 在评论中看起来像在MSDN中这样:

Public property is a good way to go, but one would have to put the MasterType directive in every content page (the .aspx file). 公共属性是一个很好的方法,但是必须将MasterType指令放在每个内容页面(.aspx文件)中。 If content pages extend a base class (which extends Page), then the same strong typing can be done in the base class CodeBehind. 如果内容页面扩展了一个基类(扩展了Page),那么可以在基类CodeBehind中完成相同的强类型。 For example: 例如:

    // MySiteMaster : System.Web.UI.MasterPagepublic

 string Message
    {
        get
        {
            return MessageContent.Text;
        }
        set
        {
            MessageContent.Text = value;
        }
    }

    // MyPage : System.Web.UI.Page
    MySiteMaster masterPage = Master as MySiteMaster;
    masterPage.Message = "Message from content page";

MessageContent is a control on the master page. MessageContent是母版页上的控件。 The MyPage class could expose Message as its own property or allow derived classes to access it directly. MyPage类可以将Message作为自己的属性公开,或者允许派生类直接访问它。

Add a new Readonly Property to your Master Page 将新的只读属性添加到母版页

public string MyName
{
    get { return (string)(Session["myName"]); }
}

Add this code after your page declaration of content page (change the Master Page file name & path accordingly ) 在页面声明内容页面后添加此代码(相应地更改母版页文件名和路径)

<%@ MasterType virtualpath="~/Site.master" %>

Then you can access your property from the content page 然后,您可以从内容页面访问您的属性

var MyNameFromMaster = Master.MyName;

Use the masterpagetype directives in your aspx page like below 在aspx页面中使用masterpagetype指令,如下所示

  <%@ MasterType  virtualPath="~/Site.master"%>

Now you can access the variables of master page using "Master.[VariableName]" 现在,您可以使用“Master。[VariableName]”访问母版页的变量

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

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