如何访问用户控件在母版从ASP:使用主内容页? [英] How do you access user controls on a masterpage from the asp:content page using the master?

查看:91
本文介绍了如何访问用户控件在母版从ASP:使用主内容页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到了'让我的工具做XYZ的的 Web应用程序我们投入了。

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.

我有叫我MessageCenter使用的错误,成功和信息性消息的用户控件,所以我放弃了母版页上。

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

...

通常,当msgcenter是一个普通的aspx页面上,我呼吁从codebehind它的方法和材料如本次

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

但是,当我尝试这样做,从ASP:内容物的网页上使用母版,它告诉我,msgCenter1未声明。这是某种范围的问题。

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.

我读过有关使用的FindControl像

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.

我感觉好像我只是缺少一个一块拼图,但它已经躲避昨天我,因为下午4点左右。

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

任何意见,指针或链接将是最AP preciated。

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

感谢。

推荐答案

首先这个指令,你要访问的母版页添加到内容页

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:使用主内容页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆