开发者

C# custom control to get internal text as string

ok, I'm working on a custom control that can contain some javascript, and read this out of the page into a string field.

This is a workaround for dynamic javascript inside an updatepanel.

At the moment, I've got it working, but if I try to put a server tag inside the block:

<custom:control ID="Custom" runat="server">
    <%= ControlName.ClientID %>
</custom:control>

The compiler does not like it. I know these are generated at runtime, and so might not be compatible with what I'm doing, but does anyone have any idea how I can get that working?

EDIT

Error message is: Code blocks are not supported in this context

EDIT 2

The control:

[DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ControlValueProperty("Text"), DefaultProperty("Text"), ParseChildren(true, "Text"), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class CustomControl : Control, ITextControl
{
    [DefaultValue(""), Bindable(tru开发者_高级运维e), Localizable(true)]
    public string Text
    {
        get
        {
            return (string)(ViewState["Text"] ?? string.Empty);
        }

        set
        {
            ViewState["Text"] = value;
        }
    }
}


The compiler is write, server side code blocks are only supported within the context of an ITemplate.

The "Text" property should be set like this ...

<custom:control ID="Custom" runat="server" Text="YourText">

Using ITemplate you can declare it in the codebehind as ...

public ITemplate Text
{
    get;
    set;
}

But then you would need to do this ...

<custom:control ID="Custom" runat="server">
   <Text><%= ControlName.ClientID %></Text>
</custom:control>

Having said that, if you have a custom control why not just do this in the code behind ...

this.text = ((ITextControl)Page.FindControl(controlName)).Text;

Trouble is, it's not very dynamic.

I would favour the templated option. Harder to implement though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜