Accessing/Setting the property value of an object property of an ASP.NET User Control - Using Decalarative Syntax

StackOverflow https://stackoverflow.com/questions/7110163

Question

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

Was it helpful?

Solution

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

OTHER TIPS

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; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top