简体   繁体   English

无法从内容页面访问母版页的<input type =“button”>

[英]Can not access <input type=“button”> of a master page from a content page

I have an 我有一个

 <input type="button" id="Button1"> 
in my Master1.master masterpage. 在我的Master1.master (that resides in a <div> ). (位于<div> )。

I also have a content page Main.aspx . 我还有一个内容页面Main.aspx I want to set the visibility of the button to true when I access the content page Main.aspx , for example, from another page. 我想在访问内容页面Main.aspx时将按钮的可见性设置为true ,例如,从另一个页面访问。 (I have a Login.aspx page, and after I insert user/psw and press a button I ged redirected to Main.aspx ). (我有一个Login.aspx页面,在我插入user / psw并按下一个按钮后,我将重定向到Main.aspx )。

I have added the directive <%@ MasterType virtualpath="~/Master1.master" %> and i am using the following code 我添加了指令<%@ MasterType virtualpath="~/Master1.master" %> ,我使用以下代码

 Button home = new Button(); home = (Button)Master.FindControl("Button1"); home.Visible = false; 

However I get a NullReferenceException when i try to run this. 但是当我尝试运行它时,我得到一个NullReferenceException So basically I can't see the "Button1". 所以基本上我看不到“Button1”。 I have tried to change the values to head or ContentHolder1 and with these it runs perfectly. 我试图将值更改为headContentHolder1 ,并且这些值可以完美地运行。

Can anyone please help me ? 谁能帮帮我吗 ?

You can't access input which is not a server side control : 您无法访问不是服务器端控件的输入:

Youll need : 你需要 :

<input type="button" id="Button1" runat="server">

您需要添加runat属性:

<input type="button" id="Button1" runat="server">

Have the master page expose a property that you can then use to reference the Button . 让母版页公开一个属性,然后可以使用该属性来引用Button The ID of the control is going to be changed during the render event; 在渲染事件期间将更改控件的ID; that is why you cannot find it. 这就是为什么你找不到它。 Using the property on the master page, you can then address the control by accessing the Page.Master variable. 使用母版页上的属性,您可以通过访问Page.Master变量来解决控件问题。 You will need to cast it into the master page class so that you can use IntelliSense to access the public and friend methods/properties. 您需要将其强制转换为母版页面类,以便您可以使用IntelliSense访问公共和朋友的方法/属性。 Here is some code ... sorry it is in VB. 这是一些代码...对不起,它在VB中。

Here is the master page HTML. 这是主页面HTML。

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" id="button1" runat="server" />
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Here is the master page code-behind. 这是代码隐藏的母版页。

Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
    Public ReadOnly Property Button As Control
        Get
            Return button1
        End Get
    End Property
End Class

Here is the content page. 这是内容页面。

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        CType(Page.Master, MasterPage).Button.Text = "My Text"
    End Sub
End Class

Notice the rendered control name below; 注意下面呈现的控件名称; they are not what you expect. 它们不是你所期望的。 You can get the real rendered name from the UniqueID property. 您可以从UniqueID属性中获取实际呈现的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM