Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access master page control from content page

I have a master page which contains a label for status messages. I need to set the status text from different .aspx pages. How can this be done from the content page?

public partial class Site : System.Web.UI.MasterPage {     public string StatusNachricht     {         get         {             return lblStatus.Text;         }         set         {             lblStatus.Text = value;         }     }      protected void Page_Load(object sender, EventArgs e)     {                  } } 

I have tried this, but was unsuccessful in making it work:

public partial class DatenAendern : System.Web.UI.Page {     var master = Master as Site;      protected void Page_Load(object sender, EventArgs e)     {                        if (master != null)         {             master.setStatusLabel("");         }     }              protected void grdBenutzer_RowCommand(object sender, GridViewCommandEventArgs e)     {                        try             {                 //some code                  if (master != null)                 {                     master.setStatusLabel("Passwort erfolgreich geändert.");                 }             }             catch (Exception ex)             {                 if (master != null)                 {                     master.setStatusLabel("Passwort konnte nicht geändert werden!");                 }                                                    }         }     }                    } 
like image 622
LeonidasFett Avatar asked Mar 22 '13 14:03

LeonidasFett


People also ask

How do you access master page controls from the content page?

To access the master page controls we need to use the Master key word then call the Property method of control. As we use Master. EnterNameTextBox() to access the master page EnterName textbox and Master. SendDataToContentPageButton() method is used to acees master page button.

How do you access a public property defined in a master page from the content page?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.


Video Answer


1 Answers

In the MasterPage.cs file add the property of Label like this:

public string ErrorMessage {     get     {         return lblMessage.Text;     }     set     {         lblMessage.Text = value;     } } 

On your aspx page, just below the Page Directive add this:

<%@ Page Title="" Language="C#" MasterPageFile="Master Path Name"..... %> <%@ MasterType VirtualPath="Master Path Name" %>   // Add this 

And in your codebehind(aspx.cs) page you can then easily access the Label Property and set its text as required. Like this:

this.Master.ErrorMessage = "Your Error Message here"; 
like image 176
Praveen Nambiar Avatar answered Oct 08 '22 00:10

Praveen Nambiar