在其代码隐藏文件中访问母版页的标签

时间:2014-07-17 22:58:01

标签: asp.net

好吧,当我构建解决方案时,我在代码隐藏文件中访问母版页的标签控制时遇到了问题,它给出了错误: “在当前背景下,'lbllogin'这个名字并不存在” 任何人都可以告诉我如何解决这个问题......

这是我的masterpage.master文件代码:

<%@ Master Language="C#" AutoEventWireup="true"  CodeFile="Masterpage.master.cs" Inherits="Masterpage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My WebSite</title>
    <script type="text/javascript" src="JavaScript/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
        function mainmenu() {
            $(" #nav ul ").css({ display: "none" });
            $(" #nav li ").hover(function () {
                $(this).find('ul:first').css({ visibility: "visible", display: "none" }).show(400);
            }
            , function () {
                $(this).find('ul:first').css({ visibility: "hidden" });
            });
        }
            $(document).ready(function(){
                mainmenu();
        });
    </script>
   <link rel="stylesheet" type="text/css" href="~/Styles/StyleSheet.css" />
</head>
<body>
    <form id="form1" runat="server">
    <div  id="wrapper">
        <div id="banner">

        </div>
        <div id="navigation">
            <ul id="nav">
                <li><a href="Home.aspx" runat="server">Home</a></li>
                <li><a href="#">Review</a>
                    <ul>
                        <li><a href="CoffeeDisplay.aspx">Coffee</a></li>
                        <li><a href="#">Coffee Shops</a></li>
                        <li><a href="#">Coffee Brands</a></li>
                    </ul>
                </li>
                <li><a href="#">Shop</a></li>
                <li><a href="#">About</a></li>
                 <li><a href="Coffee_Overview.aspx">Management</a></li>
                <ul>
                          <li><a href="Coffee_Overview.aspx">Coffee</a></li>
                        <li><a href="#">Users</a></li>
                </ul>
            </ul>


                <div id="login" align="right">

                <asp:Label ID="lbllogin" runat="server" Text="" ForeColor="White"></asp:Label>
                <asp:LinkButton ID="LinkButton1" runat="server" ForeColor="White" OnClick="LinkButton1_Click" >LinkButton</asp:LinkButton>

            </div>


        </div>
        <div id="content_area">

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>

        </div>
        <div id="sidebar">

        </div>
        <div id="footer">
            <p>All rights reserved.</p>
        </div>
    </div>
    </form>
</body>
</html>

and this is my code behined

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Masterpage : MasterPage
{
    //protected global::System.Web.UI.WebControls.Label lbllogin;
    //protected global::System.Web.UI.WebControls.LinkButton LinkButton1;


    protected void Page_Load(object sender, EventArgs e)
    {
       lbllogin.Text = "error";

    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {


    }enter code here
}

1 个答案:

答案 0 :(得分:0)

尝试使用'This'关键字访问控件..

例如

This.lbllogin.Text = "error";

希望这会有所帮助..