如何使用母版从 asp:content 页面访问母版页上的用户控件?

发布于 2024-07-30 13:04:30 字数 3055 浏览 1 评论 0原文

我不断收到这些“让我成为一个执行 xyz 操作的工具”的请求 网络应用 我们正在发布。

因此,在第三次之后,我意识到将它们全部放在一起并使用母版页会更容易。

我有一个名为 MessageCenter 的用户控件,用于显示错误、成功和参考消息,因此我将其放在母版页上。

<%@ 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">

...

通常,当 msgcenter 位于常规 aspx 页面上时,我会从代码隐藏中调用其方法和内容,如下所示:

...

    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

但是当我尝试使用页面上的 asp:content 来执行此操作时母版页,它告诉我 msgCenter1 未声明。 这是某种范围问题。

我读过有关使用 findcontrol 的内容,例如

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

但是当我尝试转换为我的用户控件时,它会抱怨,因为它再次没有声明。

我感觉自己好像只漏掉了拼图中的一块,但从昨天下午 4 点左右开始,它就一直在困扰着我。

任何建议、指示或链接将不胜感激。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唔猫 2024-08-06 13:04:30

首先将此指令添加到您要访问母版页的内容页中

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

其次,在母版页上设置一个公共属性,该属性返回您要访问的控件

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

最后只需访问内容页中的控件,如下所示

Master.MasterLabel.Text = "Hello from the content page!";

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!";
燕归巢 2024-08-06 13:04:30

我知道您的问题已得到解答,但这不适用于它,但我注意到您正在为“MessageCenter”控件传递 1 个长度的字符。 我会使用枚举而不是字符串来使您的代码不那么脆弱。 就目前情况而言,您可以将“屁”作为参数传递,它会编译得很好。 枚举将为您提供一些编译时检查并避免运行时出现任何问题。

示例:

消息.成功
消息.错误
消息.警告

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

野生奥特曼 2024-08-06 13:04:30

这就是我所使用的。

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

this is what i'd been used.

Master.FindControl("ControlID").Visible = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文