从内容页面访问母版页元素

时间:2019-08-21 13:42:11

标签: html asp.net asp.net-core webforms master-pages

我是asp.net的新手。我使用HTML模板创建登录页面,因此我进行了研究,发现我必须使用母版页。

我添加了一个母版页,并将代码从html模板复制到了它。之后,我添加了一个带有母版页的Webform。问题是我无法访问母版页代码中不是asp元素的母版页元素,并且无法从控制页访问任何控件。

请帮助我,并指导您掌握母版页和Web表单,以了解哪些代码必须包括每个部分并了解其用法。

例如,我可以访问Label2,但不能访问“母版”页面中的输入。

font_weight

母版页:

<input id="textbox_password" type="password" class="form-control" placeholder="Password" required="" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>. 

WebForm:


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login</title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge/">
    <meta name="viewport" content="width=device-width, initial-scale=1"/>


    <!-- Bootstrap -->
    <link href="../templateDesign/vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
    <!-- Font Awesome -->
    <link href="../templateDesign/vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <!-- NProgress -->
    <link href="../templateDesign/vendors/nprogress/nprogress.css" rel="stylesheet"/>
    <!-- Animate.css -->
    <link href="../templateDesign/vendors/animate.css/animate.min.css" rel="stylesheet"/>

    <!-- Custom Theme Style -->
    <link href="../templateDesign/build/css/custom.min.css" rel="stylesheet"/>
</head>
<body class="login">
      <form id="form1" runat="server">
    <div>
       <h1><i class=""></i> Test</h1>
        <a class="hiddenanchor" id="signin"></a>
        <div class="login_wrapper">
        <div class="animate form login_form">
          <section class="login_content">
              <h1>Sign in</h1>
              <div>
                <input id="textbox_username" type="text" class="form-control" placeholder="Username" required="" />
              </div>
              <div>
                <input id="textbox_password" type="password" class="form-control" placeholder="Password" required="" />
              </div>
              <div>
                <a class="btn btn-default submit" runat="server" onclick="VerifyCredentials">Log in</a>
                <a class="reset_pass" href="#">Lost your password?</a>
              </div>
              <div class="clearfix"></div>
                <div>
                  <p>©2019 All Rights Reserved</p>
                </div>
          </section>
        </div>
       </div>
      </div>
           <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ---- %>" ProviderName="<%$ ConnectionStrings:---- %>" SelectCommand="-------"></asp:SqlDataSource>
          <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
          <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
      </form>
       </body>
</html>

检查登录的代码:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

该代码无法识别textbox_username和textbox_password。谢谢!

1 个答案:

答案 0 :(得分:0)

您需要通过Master.FindControl()并使用文本框的ID查找控件。

public partial class _testForPw :  System.Web.UI.Page
{
    public void Page_Load(object sender, EventArgs e)
    {
        TextBox textBox = (TextBox)Master.FindControl("ControlId");

        if (textBox != null)
        {
            string test = textBox.Text;
        }
    }
}