My content pages allow me to manipulate data and so, their code includes error handling blocks. What I'd like to do is set the text value for several labels in a modal window on the master page in order to display the error message to the user:
<!-- master page -->
<div runat="server" id="divModalBg" class="modalbg"> </div>
<div runat="server" id="divErrorModal" class="modal">
<asp:Label runat="server" id="lblErrorTitle"></asp:Label>
<asp:Label runat="server" id="lblErrorMessage"></asp:Label>
<asp:Button runat="server" id="btnRetry" text="Retry" />
</div>
I'm not sure how I would set the values for those labels in my content page...
// Users.aspx.cs
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
// do some stuff
}
catch(Exception ex)
{
// lblErrorTitle = "Update User";
// lblErrorMessage = String.Format("Error actioning request: [ {0} ]", ex.Message);
// btnRetry just closes the modal (sets visible to false) so that
// the user can try again
}
}
So how can I access the controls on the master page so that I can do this?
I'm thinking properties on the master page, set them onPageLoad...
EDIT
As per mason
's answer below, I've done this...
// Default.master
// properties for error handling
public virtual Label ErrorTitle { get { return lblErrorAction; } }
public virtual Label ErrorMessage { get { return lblErrorMessage; } }
public virtual Panel ErrorBG { get { return pnlErrorBackground; } }
public virtual Panel ErrorModal { get { return pnlErrorModal; } }
Here's my directive:
<%@ MasterType VirtualPath="~/Default.master" %>
And here's my code file
// Users.aspx.cs
private void deleteUser(int userid)
{
ImajUser u = new ImajUser(userid, true);
try
{
u.Delete();
lblerr.Text = "User was deleted successfully.";
lblerr.ForeColor = System.Drawing.Color.Green;
}
catch (Exception ex)
{
Page.Master.ErrorTitle.Text = "Delete User";
Page.Master.ErrorMessage.Text = String.Format("Error actioning request: [ {0} ]", ex.Message);
Page.Master.ErrorBG.Visible = true;
Page.Master.ErrorModal.Visible = true;
}
Now getting the following error for each of these controls:
'System.Web.UI.MasterPage' does not contain a definition for 'ErrorTitle' and no extension method 'ErrorTitle' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?)