C# 无法在后面的代码中访问控制 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1316757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:03:48  来源:igfitidea点击:

Can't access control ID in code behind

c#asp.netuser-controlsaspx-user-control

提问by

I have the following code in my aspx page:

我的aspx页面中有以下代码:

 <asp:Button runat="server" ID="myButton" Text="hello" />

and this in my code behind:

这在我后面的代码中:

protected void Page_Load(object sender, EventArgs e)
{ 
    myButton.Text = "bye"; 
}

For some reason the intellisense picks up the "myButton" id in the code behind but when it compiles it says

出于某种原因,智能感知在后面的代码中获取了“myButton”id,但是当它编译时它说

it can't build because it doesn't recognise it!

它无法构建,因为它无法识别它!

My page is a default aspx page which uses a master page, the button is inside a content control and all is set to run at server correctly, the page runs and displays fine except for this button resolution issue!

我的页面是默认的 aspx 页面,它使用母版页,按钮位于内容控件内,并且所有内容都设置为在服务器上正确运行,页面运行并显示正常,除了此按钮解析问题!

Any ideas?

有任何想法吗?

采纳答案by Matthew Dresser

Sounds like your designer file (PageName.designer.cs) has got a bit messed up. I'd try deleting the button from your page and adding it again.

听起来您的设计器文件 (PageName.designer.cs) 有点混乱。我会尝试从您的页面中删除该按钮并重新添加它。

回答by Joel Coehoorn

If the button is inside a naming container like a repeater you won't be able to use it like that. Instead, you need to do something like this:

如果按钮位于像中继器这样的命名容器内,您将无法像那样使用它。相反,您需要执行以下操作:

Button myButton = (Button)Container.FindControl("myButton");
myButton.Text = "bye";

回答by Darthg8r

I've run into this issue a few times. Sometimes it classnames in the designer and code behind don't match. Other times it's the namespace not matching. Other times, the designer page doesn't get the member variable generated.

我遇到过几次这个问题。有时设计器中的类名和后面的代码不匹配。其他时候是命名空间不匹配。其他时候,设计器页面不会生成成员变量。

As mdresser mentioned, regenerating the designer file very well may take care of the problem. Just make sure that you save the aspx page first.

正如 mdresser 所提到的,很好地重新生成设计器文件可能会解决这个问题。只要确保先保存 aspx 页面即可。

回答by Dr TJ

I faced the problem and i noticed that Designer.csfiles were excluded from my project.
try to look for them include to your project again.
good luck

我遇到了这个问题,我注意到Designer.cs文件被排除在我的项目之外。
尝试再次查找它们包含到您的项目中。
祝你好运

回答by Protector one

Make sure you have no files that accidentally try to inherit or define the same (partial) class. These files can be completely unrelated to where the error actually appeared!

确保您没有意外尝试继承或定义相同(部分)类的文件。这些文件可能与错误实际出现的位置完全无关!

回答by Kohen Holms

I've encountered the same problem, first I was trying to figure what was wrong with my designer.cs file. But after I realized that for my WebForm markup I was using a copy/paste from the Default webform. So the problem was, like Protector one said be sure you check the inherit property of @Page:

我遇到了同样的问题,首先我试图弄清楚我的designer.cs 文件出了什么问题。但是在我意识到对于我的 WebForm 标记后,我使用了默认 Web 表单中的复制/粘贴。所以问题是,就像保护者说的,一定要检查@Page 的继承属性:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="InputValidation._Default" %>

回答by Lightning3

i know that problem is solved, but when i had a similiar problem, when i have convert WebAplication From c# to VB - i solved this by adding Namespace X / End Namespace

我知道问题已解决,但是当我遇到类似问题时,当我将 WebAplication 从 c# 转换为 VB 时 - 我通过添加 Namespace X / End Namespace 解决了这个问题

回答by Developer

I found a simple solution for this case.

我为这种情况找到了一个简单的解决方案。

Just open the designer file and add a line that corresponds to your UI control.

只需打开设计器文件并添加与您的 UI 控件对应的行。

protected global::System.Web.UI.WebControls.Button myButton;

Where myButtonis Idof your control.

其中myButton的Id你无法控制的。

回答by Reginaldo Rigo

It's been a long time. Buuuut I was facing this same problem today, so it seems never it's too late. For me it was happening cause the TextBoxwas inside a nonnamed form. Then I set an id to the form and I could acess the TextBoxlike this: idForm.idTextBox.ToString(). I guess it is gonna happen the same with a button.

已经很久了。Buuuut 我今天也面临着同样的问题,所以似乎永远不会太晚。对我来说,这是因为它TextBox在一个未命名的表单中。然后,我设置一个ID的形式,我可以接取的TextBox是这样的:idForm.idTextBox.ToString()。我猜它会发生同样的事情button

回答by Sandip.Nascar

Problem is you might have multiple aspx files with codefile in page directive points to same codebehind file. It expects the same control to exists in all the aspx file linked to same code behind and thus throwing compilation error.

问题是您可能有多个 aspx 文件,其中页面指令中的代码文件指向同一个代码隐藏文件。它期望链接到相同代码的所有 aspx 文件中存在相同的控件,从而引发编译错误。