C # user control for getting inner text as a string

ok, I'm working on a custom control that might contain some javascript, and read this on the page in the line box.

This is a workaround for dynamic javascript inside the update panel.

This works for me at the moment, but if I try to put the server tag inside the block:

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

He doesn't like the compiler. I know that they are generated at runtime, and therefore may not be compatible with what I am doing, but does anyone know how I can do this?

EDIT

Error message: Code blocks in this context are not supported

EDIT 2

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(true), Localizable(true)] public string Text { get { return (string)(ViewState["Text"] ?? string.Empty); } set { ViewState["Text"] = value; } } } 
+4
source share
1 answer

A compiler is a record, server-side code blocks are supported only in the context of ITemplate.

The Text property should be set as follows:

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

Using ITemplate, you can declare it in code as ...

 public ITemplate Text { get; set; } 

But then you will need to do this ...

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

Having said that, if you have user control, why not just do it in the code behind ...

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

The problem is that it is not very dynamic.

I would prefer a template option. It’s harder to implement, though.

+1
source

Source: https://habr.com/ru/post/1310345/


All Articles