简体   繁体   English

用户控件的内部属性不支持代码块,但面板中是否支持代码块?

[英]Code blocks not supported in inner property of user control, but are supported in panel?

I have a UserControl that works much like an <asp:Panel> . 我有一个UserControl,其工作方式非常类似于<asp:Panel>

While a panel can do this: 虽然面板可以做到这一点:

<asp:Panel runat="server">
    <img src='<%= ResolveUrl("~/img.png")%>' />
</asp:Panel>

I get an error when trying this with my control: 使用我的控件尝试操作时出现错误:

<uc1:NotPanel runat="server">
    <img src='<%= ResolveUrl("~/img.png")%>' />
</uc1:NotPanel>

error: Code blocks are not supported in this context 错误:在此上下文中不支持代码块

NotPanel is referenced like so: NotPanel的引用方式如下:

<%@ Register Src="~/Controls/NotPanel.ascx" TagName="NotPanel" TagPrefix="uc1" %>

and defined as follows (this is a grossly stripped-down version): 并定义如下(这是完全精简的版本):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="NotPanel.ascx.cs" Inherits="MyNamespace.NotPanel" %>
<div class="something">
    <asp:PlaceHolder ID="phControls" runat="server" />
</div>

with this code-behind: 具有以下代码:

[ParseChildren(true, "Content")]
[PersistChildren(false)]
public partial class NotPanel : System.Web.UI.UserControl
{   
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public Control Content { get; set; }

    public void Page_Init(object sender, EventArgs e)
    {
        phControls.Controls.Add(Content);
    }
}

Have I done something wrong with the definition of this class, that I can't use code blocks? 我对此类的定义做错了什么,我不能使用代码块? Or is this a limitation of UserControls? 还是这是UserControls的限制?

A related issue (presumably with the same cause) occurs when a textbox is placed inside one of these controls - an <asp:CompareValidator> that is not inside the same control cannot reference it, without getting the following error: 当将文本框放置在这些控件之一中时,会发生一个相关的问题(可能是相同的原因)- 不在同一控件中的<asp:CompareValidator>无法引用它,而不会出现以下错误:

Unable to find control id 'txtCompareTo' referenced by the 'ControlToCompare' property of 'compareValidator'. 找不到由“ compareValidator”的“ ControlToCompare”属性引用的控件标识“ txtCompareTo”。

Again, this works fine when <uc1:NotPanel> is replaced with <asp:Panel> . 同样,将<uc1:NotPanel>替换为<asp:Panel>时,此方法工作正常。 Is there anything that can be done to make this work? 有什么可以做的使这项工作吗?

As far as I know (and I'll happily be corrected) you can't inject code blocks like that into a User Control. 据我所知(我会很高兴地得到纠正),您无法将这样的代码块注入到用户控件中。

User Controls as basically mini-standalone pages and are generally isolated from their parent. 用户控件基本上是微型独立页面,通常与其父级隔离。 And they're not actual custom web controls like the name might imply. 而且它们并不是真正的自定义Web控件,如名称所示。

However you can pass in values via Properties. 但是,您可以通过属性传递值。 So in your User Control ascx.cs contain something like: 因此,在用户控件中,ascx.cs包含以下内容:

private string _myImagePath;
public string MyImagePath
{
    get
    {
        return _myImagePath;
    }
    set
    {
        _myImagePath = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    //do stuff
}

Then you can do <uc1:NotPanel runat="server" MyImagePath="Foo" /> 然后,您可以执行<uc1:NotPanel runat="server" MyImagePath="Foo" />

OR 要么

in the ASPX: <uc1:NotPanel runat="server" ID="MyUserControl" /> and in the codebehind: MyUserControl.MyImagePath = "Foo"; 在ASPX中: <uc1:NotPanel runat="server" ID="MyUserControl" />并在代码隐藏中: MyUserControl.MyImagePath = "Foo";

This wil sound strange, but if you put an <asp:PlaceHolder> control around the markup code containing your code blocks, the runtime errors will probably go away. 这听起来很奇怪,但是如果在包含代码块的标记代码周围放置<asp:PlaceHolder>控件,则运行时错误可能会消失。

Intellisense might complain a bit, though. 不过,Intellisense可能会有所抱怨。

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

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