如何使用ASP.NET C中的子页面访问母版页div [英] How to access master page div using child page in ASP.NET C#

查看:51
本文介绍了如何使用ASP.NET C中的子页面访问母版页div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的项目的第二页移动后,我想在母版页的标签控制中设置值。



请建议我一些想法。



我试过以下代码,但是它会抛出错误



对象引用未设置为对象的实例。



我尝试了什么:



我试过以下代码: -





标签lblSubHd = this.Master.FindControl(lblSubHd)作为标签;

lblSubHd .Visible = true;

解决方案

您好,



您不能直接访问主要组件页。它的分配属于受保护的类型。



为了访问标签组件,我建议您在母版页中使用公共属性。



主页中的HTML代码

 <   div  >  
< ; asp:标签 ID = lblSubHd runat = server 文本 = 嗯Texto > < ; / asp:标签 >
< asp:ContentPlaceHolder ID = ContentPlaceHolder1 runat = server >

< / asp :ContentPlaceHolder >
< / div >





访问组件,这里是代码。

母版页的代码隐藏

 公开标签MyLabel 
{
获取
{
return this .lblSubHd;
}
}



代码隐藏到子页面

标签mylabelchild =  this  .Master.MyLabel; 





访问文本属性标签组件,这里是代码。

母版页的代码隐藏

  public   string  ValueLabel 
{
get
{
return this .lblMyLabel.Text;
}
}



代码隐藏到子页面

  this  .MylabelChild.Text =  this  .Master.ValueLabel; 





重要!对于您使用上面的解决方案,您的子页面需要在asp标记中包含母版页。

 <%@     Page    语言  =  C#    AutoEventWireup   =  true     MasterPageFile   = < span class =code-keyword>〜/ Main.Master    CodeBehind   =  child_page.aspx.cs   继承  =  aplicacao.child_page   %>  
<%@ MasterType VirtualPath = 〜/ Main.Master %GT;


After moving from second page of my project,i want to set value in label control of master page.

please suggest me some idea.

I have tried following code,but it throws an error

Object reference not set to an instance of an object.

What I have tried:

I have tried following code:-


Label lblSubHd = this.Master.FindControl("lblSubHd") as Label;
lblSubHd.Visible = true;

解决方案

Hi,

You do not directly access a component of the main page. Its assignatury is of the protected type.

For you access the label component, I suggest that you use public properties in your master page.

HTML code in your master page

<div>
    <asp:Label ID="lblSubHd" runat="server" Text="Um Texto"></asp:Label>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
   
    </asp:ContentPlaceHolder>
</div>



To access the component, here, the code.
Code-behind to master page

public Label MyLabel
{
   get
   {
      return this.lblSubHd;
   }
}


Code-behind to child page

Label mylabelchild = this.Master.MyLabel;



To access the text property of the label component, here, the code.
Code-behind to master page

public string ValueLabel
{
    get
    {
      return this.lblMyLabel.Text;
    }
}


Code-behind to child page

this.MylabelChild.Text = this.Master.ValueLabel;



IMPORTANT! For you use the solution above your child page need to have the master page in asp tag.

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Main.Master" CodeBehind="child_page.aspx.cs" Inherits="aplicacao.child_page" %>
<%@ MasterType VirtualPath="~/Main.Master" %>


这篇关于如何使用ASP.NET C中的子页面访问母版页div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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