How do you access user controls on a masterpage from the asp:content page using the master?

空扰寡人 提交于 2019-12-30 07:22:06

问题


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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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.


回答1:


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!";



回答2:


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




回答3:


this is what i'd been used.

Master.FindControl("ControlID").Visible = false;


来源:https://stackoverflow.com/questions/1240564/how-do-you-access-user-controls-on-a-masterpage-from-the-aspcontent-page-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!