【问题标题】:asp control from ascx codebehind来自 ascx 代码隐藏的 asp 控制
【发布时间】:2013-10-04 22:20:02
【问题描述】:

我知道这个问题已经被问了数百次,但是我找不到解决问题的方法。 我有这个 ascx 文件:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="FrontClass.CreateClientPortals.TopNavBar, FrontClass.CreateClientPortals, Version=1.0.0.0, Culture=neutral, PublicKeyToken=40f3875c9d373801"%>
<div>
  <asp:Literal ID="test1" runat="server" visible="true">some text</asp:Literal>
</div>

还有这个代码隐藏:

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

namespace FrontClass.CreateClientPortals
{
    public partial class TopNavBar : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            test1.visible = false;
        }
    }
}

在尝试编译时,我收到错误: 当前上下文中不存在名称“test1”

我应该注意这两个文件不在同一个目录中(它们需要吗?)并且我似乎无法生成 Designer.cs 文件(尝试在设计模式下编辑 ascx 文件,但它没有出现)。我从另一个项目复制过来的 ascx 文件。

感谢您的帮助

【问题讨论】:

    标签: c# asp.net ascx


    【解决方案1】:

    如果没有设计器文件,您需要手动添加对 test1 的引用。将此添加到您的代码隐藏中。 Studio 创建了将所有内容链接在一起的部分类,并且没有设计器文件,您会丢失参考。

    protected global::System.Web.UI.WebControls.Literal test1;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      相关资源
      最近更新 更多