访问/设置 ASP.NET 用户控件的对象属性的属性值 - 使用声明语法

发布于 2024-11-30 09:51:28 字数 953 浏览 1 评论 0原文

我感觉我记得以前有一篇文章解释了如何以声明方式设置对象属性的值,并将其公开为 ASP.NET 用户控件的属性。

我的情况是,我有一个用户控件,其中包含 LinkBut​​ton,当然还有其他内容。我希望用户控件的使用者能够在用于实现用户控件的声明性语法中设置链接按钮的 TEXT 值。

这是用户控件(设计器)...

<div id="toolbar">
    <ASP:LinkButton runat="server" id="btnFirst"    />
    <ASP:LinkButton runat="server" id="btnSecond"   />
    <ASP:LinkButton runat="server" id="btnThird"    />
    <ASP:LinkButton runat="server" id="btnFourth"   />
</div>

这是用户控件后面的代码中定义的属性...

public partial class Lookuptoolbar: UserControl
{
    public LinkButton FourthButton
    {
        get { return (this.btnFourth); }
    }
}

当我在页面中包含该控件时,我希望能够使用设置我的第四个按钮的文本以下声明语法...

   <UC:MyControl id="uc1" runat="server" FourthButton_Text="Click Me!"/>

很久以前,我在某处读过一次,您可以使用下划线语法访问对象的属性(公开为用户/服务器控件的属性)。这对我来说根本不起作用。这是不再允许的还是我错过了什么?有什么方法可以做到这一点吗?

谢谢你, 加里

I feel like I recall from days gone by an article explaining how to DECLARATIVELY set the value of the property of an object, exposed as the property of an ASP.NET User Control.

My situation is that I have a user control which contains a LinkButton, amongst other things, of course. I would like the consumer of the User Control to be able to set the TEXT value of the link button in the declarative syntax used to implement the User Control.

Here is the User Control (designer)...

<div id="toolbar">
    <ASP:LinkButton runat="server" id="btnFirst"    />
    <ASP:LinkButton runat="server" id="btnSecond"   />
    <ASP:LinkButton runat="server" id="btnThird"    />
    <ASP:LinkButton runat="server" id="btnFourth"   />
</div>

Here is the property as defined in the code behind of the User Control ...

public partial class Lookuptoolbar: UserControl
{
    public LinkButton FourthButton
    {
        get { return (this.btnFourth); }
    }
}

When I include the control in a page I EXPECTED to be able to set the TEXT of my FOURTH button using the following DECLARATIVE syntax ...

   <UC:MyControl id="uc1" runat="server" FourthButton_Text="Click Me!"/>

I had read once somewhere, a long time ago, that you could access the properties of an object (exposed as the property of a user/server control) by using the underscore syntax. This is not working for me at all. Is this no longer allowed or am I missing something? IS there ANY way of doing this?

Thank You,
Gary

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂ガ小子 2024-12-07 09:51:28

好吧......对于任何可能感兴趣的人,我想我已经找到了答案,或者至少是它的开始。语法将使用连字符而不是下划线。所以正确的语法是。

 <UC:MyControl id="uc1" runat="server" FourthButton-Text="Click Me!"/> 

当访问复杂类型的“子属性”时,可能会有更多内容;我没有详细讨论这一点,但 Microsoft Press 出版的《Developing Microsoft ASP.NET Server Controls and Components》(ISBN 0-7356-1582-9)一书在第 218-222 页对此进行了讨论。

如果有人学到更多东西,我很乐意听到,否则我希望这对那里的人有帮助!

-加里

Ok ... for any who might be interested I think I have found the answer, or at least the beginnings of it. The syntax would be with a HYPHEN and not an underscore. So the correct syntax would be.

 <UC:MyControl id="uc1" runat="server" FourthButton-Text="Click Me!"/> 

When accessing the "subproperties" of complex types there may be more to it; I haven't gone into it in detail, but the book "Developing Microsoft ASP.NET Server Controls and Components" by Microsoft Press (ISBN 0-7356-1582-9) discusses this on pages 218-222.

If anybody learns anything more I would love to hear about it, otherwise I hope that this helps somebody out there!

-Gary

青朷 2024-12-07 09:51:28

我从未听说过或见过任何人使用您谈论的方法(使用下划线在声明性语法中访问对象属性)。

执行您想要的操作的一种方法是在与 LinkBut​​ton 的文本属性交互的用户控件上公开 FourthButtonText 属性:

public string FourthButtonText
{
    get { return this.btnFourth.Text; }
    set { this.btnFourth.Text = value; }
}

I have never heard of or seen anybody use the method you talk about (using underscore for accessing object properties in the declarative syntax).

One way to do what you want would be to expose a FourthButtonText property on the user control that interacts with the LinkButton's text property:

public string FourthButtonText
{
    get { return this.btnFourth.Text; }
    set { this.btnFourth.Text = value; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文