Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在事件上更改Root.master中的文本?_C#_Asp.net_Login_Master Pages - Fatal编程技术网

C# 如何在事件上更改Root.master中的文本?

C# 如何在事件上更改Root.master中的文本?,c#,asp.net,login,master-pages,C#,Asp.net,Login,Master Pages,我在VS 2013中有一个带有母版页的项目。在Root.master中,我有一个“登录”按钮,它将我重定向到登录页面 <a href="Logare.aspx" runat="server" id="loginLink">Log In</a> 当我从Root.master登录文本以从“登录”更改为“欢迎,用户名”时,我怎么能这样做?我试图从Root.master获取loginLink id,但在Default.aspx.cs中不知道 更新 这就是现在从Root.mast

我在VS 2013中有一个带有母版页的项目。在Root.master中,我有一个“登录”按钮,它将我重定向到登录页面

<a href="Logare.aspx" runat="server" id="loginLink">Log In</a>
当我从Root.master登录文本以从“登录”更改为“欢迎,用户名”时,我怎么能这样做?我试图从Root.master获取loginLink id,但在Default.aspx.cs中不知道

更新

这就是现在从Root.master“登录”的方式:

<a href="Logare.aspx"><asp:Label ID="Label8" runat="server" Text="Log In"></asp:Label></a>
但是,有了这个,我得到了一个错误

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


mpLabel.Text=“欢迎,”+会话[“用户名”]

因此,如果我正确理解了您的问题,这只是关于从内容页代码中访问母版页上声明的控件。你有两个选择来实现这一点

  • FindControl
  • 代码很简单(借用OP自己的注释):

    但是请注意,
    FindControl
    仅适用于该控件的直接子级。所以如果你的结构看起来像

    MasterPage
        Control1
            Control2
                loginLink
    
    您需要这样做:

    Control c1 = Master.FindControl("Control1");
    Control c2 = c1.FindControl("Control2");
    Label mpLabel = (Label) c2.FindControl("loginLink");
    
    或者,您可以使用递归版本的
    FindControl
    。它不是现成的,但是很多(真的很多)版本已经在网上了。这是

  • 通过主界面公开控件
  • 在母版页代码隐藏中定义如下属性:

    public Label LoginLabel
    {
        get { return this.loginLabel; }
    }
    
    在内容页面上,只需使用它。不要忘记将master强制转换为您的特定类型:

    Label loginLabel = ((YourMasterPageClass)Master).LoginLabel;
    
    这可能不是特别安全,因此您可以只公开标签文本:

    public Label LoginLabelText
    {
        get { return this.loginLabel.Text; }
        set { this.loginLabel.Text = value; }
    }
    
    
    ((YourMasterPageClass)Master).LoginLabelText = "Welcome!";
    

    你的问题有点让人困惑。如果在主aspx中定义了
    loginLink
    id,则它在主代码隐藏中可用,但不在页面上。因此,您应该不会在主机的代码隐藏中通过ID访问它,并且运行与您在此处发布的代码完全相同的代码。Defaul.aspx在主要内容中。登录名在Root.master中。那么,有没有一种方法可以将其作为一种全局变量引用到Default.aspx上呢?我仍然不理解您的用例,但是如果您希望从页面访问母版页控件,您可以始终使用
    master.FindControl()
    。另一种选择是通过公共属性公开主机上的控件,然后在我尝试的页面上调用它们,而不是
    loginLink.Text=“欢迎,”+会话[“用户名”]
    标签mpLabel=(标签)Master.FindControl(“loginLink”);mpLabel.Text=“欢迎,”+会话[“用户名”]
    ,但它给了我一个错误:
    对象引用未设置为对象的实例。
    如何通过公共属性公开主机上的控件?我用我的问题更新了信息。当你说“YourMasterPageClass”时,你是指我的Root.master页吗?请确保重新访问答案中有关FindControl的部分,我非常确定它不适用于你,因为嵌套控件。而
    YourMasterPageClass
    代表母版页的类。看看主程序背后的代码,您会发现它实际上是在那里声明的一个类。这就是我的意思我唯一拥有的东西是:
    public部分类RootMaster:System.Web.UI.MasterPage{受保护的无效页面加载(对象发送方,事件参数e){ASPxSplitter1.GetPaneByName(“Header”).Size=ASPxWebControl.GlobalTheme==“Moderno”?95:83;ASPxSplitter1.GetPaneByName(“Header”).MinSize=ASPxWebControl.GlobalTheme==“Moderno”?95:83;}
    很抱歉,我刚刚开始使用母版页,因此我感到困惑,不知道如何操作。谢谢你的耐心!那你的班级就是根主了。剩下的你自己写吧,让我看看我是否写对了。在Root.master.cs中,我必须放置
    公共标签LoginLabel{get{返回this.LoginLabel;}}
    ,对吗?如果是,则说明出现问题,因为它无法识别
    loginLabel
    是谁。
    public Label LoginLabel
    {
        get { return this.loginLabel; }
    }
    
    Label loginLabel = ((YourMasterPageClass)Master).LoginLabel;
    
    public Label LoginLabelText
    {
        get { return this.loginLabel.Text; }
        set { this.loginLabel.Text = value; }
    }
    
    
    ((YourMasterPageClass)Master).LoginLabelText = "Welcome!";