gpt4 book ai didi

C#自定义控件获取内部文本作为字符串

转载 作者:行者123 更新时间:2023-11-30 12:53:06 25 4
gpt4 key购买 nike

好的,我正在开发一个可以包含一些 javascript 的自定义控件,并将其从页面中读取到一个字符串字段中。

这是更新面板内动态 javascript 的解决方法。

目前,我已经开始工作了,但是如果我尝试将服务器标记放入 block 中:

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

编译器不喜欢它。我知道这些是在运行时生成的,因此可能与我正在做的事情不兼容,但有没有人知道我如何让它工作?

编辑

错误信息是:在此上下文中不支持代码块

编辑 2

控件:

[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;
}
}
}

最佳答案

编译器已编写,服务器端代码块仅在 ITemplate 上下文中受支持。

“文本”属性应该这样设置......

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

使用 ITemplate,您可以在代码隐藏中将其声明为 ...

public ITemplate Text
{
get;
set;
}

但是你需要这样做......

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

话虽如此,如果您有自定义控件,为什么不直接在后面的代码中执行此操作...

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

问题是,它不是很动态。

我更喜欢模板化选项。虽然更难实现。

关于C#自定义控件获取内部文本作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2872270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com