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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17716984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:17:14  来源:igfitidea点击:

Getting values from master page to child page in asp.net

c#asp.netsessionmaster-pages

提问by

I have a master page masterpage.masterin 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.masterbut without using session on every page. Can I achieve that? If yes..then please tell.

现在我想将“名称”中的值用于子页面 masterpage.master但不在每个页面上使用会话。我能做到吗?如果是..那么请告诉。

I am using c#and ASP.netwebforms

我正在使用c#和网络ASP.net表单

回答by Mayank Pathak

You can put Namein a control i.e. TextBoxon MasterPageand find that in content pages like this.

您可以放入Name一个控件即TextBox打开MasterPage并在这样的内容页面中找到它。

// 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

回答by Roar

You can access your masterpagefrom current page and cast it to your class type:

您可以访问你的母版从当前页面,并将其投放到您的类的类型:

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

Looks like here in MSDNin 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). If content pages extend a base class (which extends Page), then the same strong typing can be done in the base class CodeBehind. For example:

公共属性是一种很好的方法,但必须在每个内容页面(.aspx 文件)中放置 MasterType 指令。如果内容页面扩展了一个基类(它扩展了 Page),那么可以在基类 CodeBehind 中完成相同的强类型化。例如:

    // 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. The MyPage class could expose Message as its own property or allow derived classes to access it directly.

MessageContent 是母版页上的控件。MyPage 类可以将 Message 作为它自己的属性公开或允许派生类直接访问它。

回答by Nalaka526

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;

回答by Murugavel

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]”访问母版页的变量

回答by Bhupendra Shukla

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 ;