Getting values ​​from the main page to a child page in asp.net

I have a masterpage.master main page in which I saved the value in a variable

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

Now I want to use the value that is in "Name" on the child pages of masterpage.master , but without using a session on each page. Can i achieve this? If so, please report.

I use c# and ASP.net web forms

+4
source share
5 answers

You can try the following:

  // 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 ; 
+4
source

You can put Name in the control, i.e. TextBox on MasterPage , and find it on such content pages.

 // On Master page TextBox mastertxt = (TextBox) Master.FindControl("txtMaster"); // On Content Pages lblContent.Text = mastertxt.Text; 

See MSDN for more on this.

+2
source

You can access the main page from the current page and apply it to your class type:

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

Looks like here on MSDN in the comments:

An open property is a good way, but you would need to put a MasterType directive on every page of content (.aspx file). If content pages extend the base class (which extends the page), the same strong text input can be made in the CodeBehind base class. For instance:

  // 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 of the main page. The MyPage class can return Message as its own property or allow derived classes to access it directly.

+1
source

Add a new Readonly property to the home page

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

Add this code after the content page page declaration (change the name and path to the main page)

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

You can then access your property from the content page.

 var MyNameFromMaster = Master.MyName; 
+1
source

Use masterpagetype directives on an aspx page as shown below

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

Now you can access the variables of the main page using the "Wizard. [VariableName]"

+1
source

Source: https://habr.com/ru/post/1492022/


All Articles