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

How do you access user controls on a masterpage from the asp:content page using the master?(您如何使用母版从 asp:content 页面访问母版页上的用户控件?)
本文介绍了您如何使用母版从 asp:content 页面访问母版页上的用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

解决方案

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

这篇关于您如何使用母版从 asp:content 页面访问母版页上的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)