Wolfish
Wolfish

Reputation: 970

Referencing another page programmatically

Note: For the purposes of this question, I am using the "Asp.Net Webpage with Master Page" template built in MS Visual Studio 2013.

My master page contains a label that specifies any exception errors. It must be defined in the master page for positioning reasons.

The default web page (Default.aspx.cs) gets the error message from a class, which is defined in the page.

My handler class file produces the error message like so:

public string errorString...  

...catch (Exception e)
{
    errorString = e.Message.ToString();
}

Then Default.aspx.cs instantiates the handler and string like so:

 Program.invoiceHandler handler = new invoiceHandler();
 string errorString 
     {get; set;}

and from within one of its classes populates the string:

errorString = handler.errorString;

So, ideally, Site.Master.cs should be able to populate the error string by looking at the default web page:

errorLabel.Text = _Default.errorString;

This does not work. Intellisense gives me three options when I type in _Default:

How do I access Default.aspx.cs from Site.Master.cs, and what am I doing wrong?

Upvotes: 0

Views: 580

Answers (1)

Rumpelstinsk
Rumpelstinsk

Reputation: 3241

Here is an example of what I was talking about. I have copied the code directly of one of my projects and it is not c#. It is vbnet, sorry for the inconvenience.

In Site.Master.cs add this code:

Public ReadOnly Property getErrorLabel() As [label_type]
    Get
        Return Me.[label]
    End Get
End Property

Then in Default.aspx.cs, after having set "errorString", you can use this code to set the text of the label in your MasterPage

Ctype(Master, [MasterType]).getErrorLabel().Text = errorString 

Obviously you have to change [label_type], [label] and [MasterType] for the correct values.

An other solution is to use this piece of code in Default.aspx.cs (also in vbnet, sorry :D )

Page.Master.FindControl("label_id").Text = errorString

This is the corresponding code (As best I can translate) in C#:
Site.Master.cs:

public Label getErrorLabel
        {
            get { return this.errorLabel; }
        }

Default.aspx.cs:
((SiteMaster)Master).getErrorLabel.Text = errorString;

Upvotes: 2

Related Questions