简体   繁体   中英

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> .

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:

<%@ 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?

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:

Unable to find control id 'txtCompareTo' referenced by the 'ControlToCompare' property of 'compareValidator'.

Again, this works fine when <uc1:NotPanel> is replaced with <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.

However you can pass in values via Properties. So in your User Control ascx.cs contain something like:

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" />

OR

in the ASPX: <uc1:NotPanel runat="server" ID="MyUserControl" /> and in the codebehind: 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.

Intellisense might complain a bit, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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