简体   繁体   English

在主文件的代码隐藏中找不到ASP控件

[英]Can't find asp control in codebehind of Master file

This is driving me crazy, because I thought I had the runat=server stuff totally figured out. 这让我发疯,因为我以为我完全知道了runat=server内容。

I have an asp:Literal control in my markup (it's in a masterpage, but that doesn't matter, right?) 我在标记中有一个asp:Literal控件(在母版页中,但这没关系,对吧?)

In MyMaster.Master : MyMaster.Master

<asp:Literal ID="myLiteral" runat="server" Text="Some Text"></asp:Literal>

Then, in the codebehind ( MyMaster.Master.cs ), I have: 然后,在MyMaster.Master.cs的代码( MyMaster.Master.cs )中,我有:

myLiteral.Text = "Some different text";

No matter what I try, I get 无论我尝试什么,我都会

The name 'myLiteral' does not exist in the current context 名称“ myLiteral”在当前上下文中不存在

I've tried cleaning and rebuilding my whole solution, but it doesn't make any difference. 我曾尝试清洁和重建整个解决方案,但没有任何区别。

What am I doing wrong? 我究竟做错了什么?

Edit: To clarify, I'm trying to change the text of the Literal control in the codebehind of the master page , not a content page. 编辑:为澄清起见,我试图在母版页 (而不是内容页)的代码背后更改Literal控件的文本。 Specifically, I'm doing this in the Page_Load method of the master file. 具体来说,我是在主文件的Page_Load方法中执行此操作的。

Figured it out! 弄清楚了! I wish I knew why, but the Masterfile was missing it's corresponding .designer.cs file. 我希望我知道为什么,但是.designer.cs丢失了它对应的.designer.cs文件。 I tried several things to get VS to regenerate the designer file, but I finally found the trick in one of the answers to this question . 我尝试了几种方法来使VS重新生成设计器文件,但最终在该问题的答案之一中找到了窍门。

The trick is to create an empty code file with the correct name (in my case MyMaster.Master.designer.cs . Then open the masterfile ( MyMaster.Master ) and make some small change to cause VS to resave the file. When that happens, it magically fills in the empty designer file! 诀窍是使用正确的名称(在我的情况下为MyMaster.Master.designer.cs创建一个空的代码文件,然后打开masterfile( MyMaster.Master )并进行一些小的更改以使VS重新保存该文件。 ,它神奇地填充了空的Designer文件!

So, this problem is solved. 这样,这个问题就解决了。 However, if anyone knows why a) my designer file disappeared in the first place, and b) if there's any better way of forcing VS to regenerate the designer files if they happen to disappear, that would fantastic. 但是,如果有人知道为什么a)首先我的设计器文件消失了,以及b)如果有任何更好的方法迫使VS在它们消失时重新生成设计器文件,那就太好了。

that is some problem of VS, i have witness such issues in VS 2010 when the controls were not forming. 这是VS的问题,我在控件未形成的VS 2010中见证了此类问题。 Whenever such things happens i try the following things - 1. Cleaning and rebuilding solution. 每当发生这种情况时,我都会尝试以下操作-1.清洁和重建解决方案。 2. Dragging and dropping the contacts from toolbox which is not creating. 2.从未创建的工具箱中拖放联系人。 3. Restarting computer. 3.重新启动计算机。

This thing happens only sometimes. 这件事只在某些时候发生。 I would recommend you to use VS 2013 as it is mostly bug free(if not 100%). 我建议您使用VS 2013,因为它几乎没有错误(如果不是100%)。

Thanks 谢谢

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

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