ASP / C#代码后面不能达到从标记控制 [英] ASP/C# code behind can not reach control from markup
问题描述
问题:结果
我有一个这样的标记(仅重要的行):
Issue:
I have a markup like this (only the important lines):
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RTDeluxe.ascx.cs"
Inherits="MainSolution.CONTROLTEMPLATES.Kunde.RTDeluxe" %>
<ul id="linkUl" class="teaserLinksUL" runat="server"/>
代码隐藏:
The code-behind:
namespace MainSolution.CONTROLTEMPLATES.Kunde
public partial class RTDeluxe : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
linkUl.InnerHtml = string.Empty;
}
}
我可以访问 UL
中的代码隐藏,并没有得到编译错误。但是,当我调试的代码,我收到了的NullReferenceException
,因为 linkUl
为NULL。
I can access the ul
inside the code-behind and get no compilation error. But, when I debug the code I get a NullReferenceException
because linkUl
is NULL.
首先,我认为命名空间的原因。但是,几次尝试后,我敢肯定,他们是正确的。在 FileLocation
似乎是正确的,我的IIS的 CONTROLTEMPLATES
文件夹中有与相应的ascx文件孔德文件夹中。在它
First I thought that the namespaces are the reason. But, after several tries, I'm sure that they're correct. The FileLocation
seems to be correct and the controltemplates
folder of my iis has a "Kunde" folder with the corresponding ascx files in it.
我有相同结构的其他.ascx文件 - >他们的工作就像一个魅力
I have other .ascx files with the same structure -> they're working like a charm.
问:结果
是否有任何其他原因比命名空间这样的行为? ?你有任何提示,我可以看看
Question:
Are there any other reasons than the namespace for such behaviour? Do you have any hints where I can look at?
编辑:结果
的RTDeluxe.ascx.designer.cs文件存在,生成的 linkUl
是这样的:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl;
EDIT2:结果
好吧,我会尽力回答你所有的问题。 !感谢您的时间和反馈
Ok, I will try to answer all your questions. Thanks for your time and feedback!
- 我已经重新启动Visual Studio中 - >问题依然存在
- 当我调试和检查控制层次结构,我可以看到标签是不存在的。
- 当我改变标识的编译器代码隐藏抛出一个错误(这是正确的)。如果我更改ID有两个I和以前一样得到同样的behavoiur
- 我也重新启动我的IIS和整个PC - >无变化
- 当我尝试使用<$ C $。 C>的FindControl 则返回NULL。
- 目标框架是.NET 3.5
- 的linkul是不是里面中继器或任何其他控件。
- 删除/修改web.config中也并不导致的解决方案。
- 添加
EnsureChildControls
访问前的linkUl
并没有改变任何东西。 - 移动代码到Page_PreRender也并不工作。
<华里。 >我也清理了解决方案,并部署一个新的。 - >问题仍然存在
<华里。 >我添加了一个
名称
属性到linkul定义 - >任何更改 - I have restarted Visual Studio -> The Problem persists.
- I also have cleaned up the solution and deployed a new one. -> The problem persists.
- When I debug and check the control hierachy I can see that the label is NOT there.
- When I change the ID the compiler throws an error in the code-behind (which is right). If i change the ID there two I get the same behavoiur as before.
- I also restarted my IIS and the whole pc -> No changes.
- I have added a
Name
attribute to the linkul-definition -> No changes. - When I try to use
FindControl
it returns NULL. - The target-framework is .NET 3.5
- The linkul is NOT inside a repeater or any other controls.
- Removing/changing the web.config does also not lead to a solution.
- Adding
EnsureChildControls
before accessing thelinkUl
doesnt change anything. - Moving the code into Page_PreRender does also not work.
我会尝试一下你的建议未在此处列出,并很快加入他们。
I will try out your suggestions not listed here and add them soon.
EDIT3:结果
在这里充分的标记:
Here the full markup:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RTDeluxe.ascx.cs" Inherits="MainSolution.CONTROLTEMPLATES.Kunde.RTDeluxe" %>
<ul id="linkUl" class="teaserLinksUL" runat="server"/>
* Edit4:结果
确定这里一些额外的信息,我发现:
当我改变的东西在标记,如添加纯HTML文本它不认可,或在浏览器中显示。当我做这样的事情:
*
Ok here some additional info I found out:
When I change something in the markup, like adding plain html text it's NOT recognized by or shown in the browser. When i do something like this:
Label label1 = new Label();
label1.Text = "hugo lives!";
Controls.Add(label1);
据中。这似乎是在Visual Studio中,一切都很好......但活在代码隐藏说话有些怪异不同的标记服务器...
It is shown. It seems like in visual studio everything is fine... But "live" at the server the code-behind speaks to some weird different markup...
推荐答案
这可能会帮助你一点:
在代码隐藏在类级别的用户控制的文件中,添加以下代码:
On the code-behind file of the user-control at class level, add this code:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl = new System.Web.UI.HtmlControls.HtmlGenericControl();
和删除保护全球:: System.Web.UI.HtmlControls。 HtmlGenericControl linkUl;
从 RTDeluxe.ascx.designer.cs
文件
这可能是因为其对象刚刚宣布不会创建。
希望这有助于..
It might be because its Object was just declared not created. Hope this helps..
这篇关于ASP / C#代码后面不能达到从标记控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!