views:

288

answers:

2

I keep getting these requests for 'make me a tool to do xyz' for a web app we're putting up.

So after the third one, I realized it'd be easier to lump them all together and use a master page.

I've got a user control called MessageCenter I use for error, success, and informational messages, and so I dropped that on the master page.

<%@ Master Language="VB" CodeFile="tfMasterPage.master.vb" Inherits="tfMasterPage" %>

<%@ Register Src="MessageCenter/msgCenter.ascx" TagName="msgCenter" TagPrefix="uc1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
    <title>INSERT TITLE HERE</title>
    <link href="Stylesheets/EogTool.css" rel="stylesheet" type="text/css" />
    <link href="stylesheets/TF_Main_Styles.css" rel="stylesheet" type="text/css" />
    <link href="stylesheets/TF_Print_Styles.css" rel="stylesheet" type="text/css" media="print" />
</head>
<body style="background-color: #eeeeee">
    <form id="form1" runat="server">
        <div class="page">
            <div class="headerArea">
                <div class="LogoImg">
                    <img alt="Transparency Florida" src="images/TF_Logo.jpg" /></div>
                <div class="SealImg">
                    <img alt="Shining the Light on Florida's Budget" src="images/TF_Seal.jpg" /></div>
            </div>
            <div class="content">
                <h1>
                    FIS - EOG Table Maintenance</h1>
            </div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <div>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <div class="content">
                            <div>
                                <uc1:msgCenter ID="MsgCenter1" runat="server" />
                            </div>
                            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

...

Normally, when the msgcenter is on a regular aspx page, I call its method and stuff from the codebehind as in this sub:

...

    rtn = dal.deleteRow(CInt(e.CommandArgument), currentTab())
    If Not IsNumeric(rtn) Then
        MsgCenter1.addMessage("An Error occured deletion" & rtn, , , , "E")
    Else
        MsgCenter1.addMessage("Delete Successful", , , , "S")
    End If
    bindGrid()
    MsgCenter1.Visible = True
End Sub

But when I try to do that from the asp:content thing on the page using the masterpage, it tells me that msgCenter1 is not declared. It's some sort of scope issue.

I've read about using findcontrol like

ctype(master.findcontrol("tbWhatever"), textbox).text = "FOO"

But when I try to cast to my user control, it complains because it once again, isn't declared.

I feel as though I'm just missing one piece of the puzzle, but it's been eluding me since around 4PM yesterday.

Any advice, pointers, or links would be most appreciated.

Thanks.

+4  A: 

First add this directive to the content page you want to access the master page

<%@ MasterType VirtualPath="~/NameOfMasterPage.master"%>

Second, On the master page setup a public propery that returns the control you want to access

public Label MasterLabel
        {
            get
            {
                return lblMaster;
            }
            private set
            {
                //do nothing
            }
        }

Lastly just access the control in the content page like so

Master.MasterLabel.Text = "Hello from the content page!";
cptScarlet
Very well explained.
AmitK
Is your method marked as public?
cptScarlet
I rescind that! My installation of VS is suffering dll rot and acting weird. It _does_ work! You rock! Thanks!!!!!!
aape
+1  A: 

I know your question has been answered and this doesn't apply to it, but I noticed you're passing in 1 length characters for your "MessageCenter" control. I would use an Enum instead of a string to make your code a little less brittle. As it stands now you can pass "fart" in as a parameter and it will compile just fine. An Enum will give you some compile time checking and avoid any issues at runtime.

Examples:

Message.Success
Message.Error
Message.Warning

Beavis
HA. Little do you know, that FART is a secret argument option that makes the text of the message multi-colored!Just kidding. You're totally right. This is a control written another developer, and tightening it up is on our 'things we need to do, but probably will never get the time' list.We started out just sending errors, and it was always red, so we didn't have to send the message type. Then _way_ after the requirements phase, one of the reqs people was like, "Hey guys! Let's make colored messages!"And the developers thought, "We're in final testing. Now's not the time."
aape