如何找到内容页面是用户控件的母版页里面的标签控制? [英] How to find label control from content page which is inside master page of user control?

查看:260
本文介绍了如何找到内容页面是用户控件的母版页里面的标签控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页: Abc.aspx

在此 Abc.aspx 我已经使用了一个主控页面为 Master1.Master

现在这个母版页上我已经变为1用户控件的名称 Usercontrol1.ascx。

在这个用户控件我已经把1个标签命名为LBL1。

 < ASP:标签=服务器ID =LBL1>< / ASP:标签>

所以,现在对Abc.aspx页的页面加载事件我想找到这种控制和我都试过,但得到空:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            VAR LBL =((标签)this.Master.FindControl(LBL1)); //这里空
            ((标签)this.Page.Master.FindControl(LBL1))文本=你好; //错误的位置:对象引用未设置为对象实例
        }

这是我的母版页:

 <%@主语言=C#AutoEventWireup =真codeBehind =Master1.Master.cs继承=%GT;
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头ID =头像1=服务器>
< ASP:的ContentPlaceHolder ID =头=服务器>< / ASP:&的ContentPlaceHolder GT;
< /头><身体GT;
    <形式=服务器>
   < D​​IV ID =装载>
                    < UC2:ID的UserControl1 =的UserControl1=服务器/>
                    < ASP:的ContentPlaceHolder ID =cphMain=服务器>
                    < / ASP:&的ContentPlaceHolder GT;
                < / DIV>
 < /表及GT;
< /身体GT;
< / HTML>


解决方案

最好的办法是提供一个属性在母版使用有意义的名称和类型字符串。该属性获得/设置标签在用户控件的文字。

为了达到这个目标还提供了一个属性的用户控件具有相同意义的名称和类型字符串 。此属性获取/设置标签的文本。

您已经投 this.Master 来的实际类型的母版 Master1 。然后你就可以访问该自定义属性。

因此​​,这里是你的用户控件

 公共部分类的UserControl1:System.Web.UI.UserControl
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }    公共字符串MeaningfulNameForLabelText
    {
        {返回this.lbl1.Text; }
        集合{this.lbl1.Text =价值; }
    }
}

这是你的母版

 公共部分类Master1:System.Web.UI.MasterPage
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }    公共字符串MeaningfulNameForLabelText
    {
        {返回this.UserControl1.MeaningfulNameForLabelText; }
        集合{this.UserControl1.MeaningfulNameForLabelText =价值; }
    }
}

这就是你的页面:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    VAR主= this.Master为Master1;
    如果(主!= NULL)
    {
        master.MeaningfulNameForLabelText =你好;
    }
}

I have one page:Abc.aspx

On this Abc.aspx page i have used one master page that is Master1.Master.

Now on this master page i have rendered 1 usercontrol name as Usercontrol1.ascx.

On this User control i have put 1 label named as lbl1.

 <asp:Label runat="server" ID="lbl1"></asp:Label>

So now on page load event of Abc.aspx page i want to find this control and i have tried but getting null:

 protected void Page_Load(object sender, EventArgs e)
        {
            var lbl = ((Label)this.Master.FindControl("lbl1")); // null here
            ((Label)this.Page.Master.FindControl("lbl1")).Text = "Hello"; //error here:object reference not set to instance of object
        }

This is my master page:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Master1.Master.cs" Inherits="" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>

<body>
    <form runat="server">
   <div id="Load">
                    <uc2:UserControl1 ID="UserControl1 " runat="server" />
                    <asp:ContentPlaceHolder ID="cphMain" runat="server">
                    </asp:ContentPlaceHolder>
                </div>
 </form>
</body>
</html>

解决方案

The best approach is to provide a property in your MasterPage with a meaningful name and type string. This property gets/sets the text of the label in the UserControl.

In order to meet this objective provide also a property in your UserControl with the same meaningful name and type string. This property gets/sets the text of the label.

You have to cast this.Master to the actual type of your MasterPage which is Master1. Then you can access this custom property.

So here is your UserControl:

public partial class UserControl1 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string MeaningfulNameForLabelText
    {
        get { return this.lbl1.Text; }
        set { this.lbl1.Text = value; }
    } 
}

This is your MasterPage

public partial class Master1 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string MeaningfulNameForLabelText
    {
        get { return this.UserControl1.MeaningfulNameForLabelText; }
        set { this.UserControl1.MeaningfulNameForLabelText = value; }
    } 
}

and this is your page:

protected void Page_Load(object sender, EventArgs e)
{
    var master = this.Master as Master1;
    if (master != null)
    {
        master.MeaningfulNameForLabelText = "Hello";
    }
}

这篇关于如何找到内容页面是用户控件的母版页里面的标签控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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