Object property binding

Discussion in 'ASP .Net Building Controls' started by Italo Gomes, Jul 15, 2008.

  1. Italo Gomes

    Italo Gomes Guest

    Hello there,

    I'm new on creating WebControls and I'm having problems trying to assign
    two webcontrols that I have developed by using a property. Let me explain it

    In the first WebControl I declared a property like the following:

    private WebControl2 object2;
    public WebControl2 Object2
    get { return object2; }
    set { object2 = value; }

    The property display correctly in the Property Grid, but when I try to
    run the application, I get an error message saying that it's not possible to
    create an instance of WebControl2 from a string. I have a similar pair of
    controls for WinForms and it works perfectly there without doing anything
    else, so I don't have any idea of what's wrong.

    Can anyone point me some tutorial/how-to/walkthrough about this subject?

    Thank you in advance.

    Italo Gomes.
    Italo Gomes, Jul 15, 2008
    1. Advertisements

  2. Italo Gomes

    Teemu Keiski Guest


    ASP.NET uses markup to describe these relationships and settings of controls
    whatsoever, e.g you do for example with validation controls

    <asp:TextBox ID="TextBox1" runat="server" />
    <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1"

    If you inspect the ControlToValidate property further, you'll find it is a
    string, having a typeconverter defined

    <TypeConverterAttribute(GetType(ValidatedControlConverter))> _

    which works under the cover so that validator control actually calls
    FindControl with the ID to get reference to the target control.
    ValidatedControlConverter is for picking the list of available controls on
    the page.

    Point is that ASP.NET pages are described through markup and it just cannot
    provide live reference to controls, since they actually live on the server
    and not in your markup (which is just "meta" description of what lives on
    the server).
    Teemu Keiski, Jul 15, 2008
    1. Advertisements

  3. Italo Gomes

    Teemu Keiski Guest

    To add, you certainly can have control type properties but you could assign
    them in code...as a workaround
    Teemu Keiski, Jul 15, 2008
  4. Italo Gomes

    Italo Gomes Guest

    Hi there, Teemu,

    Thank you for your reply.

    I was aware of TypeConverters, and actually tried creating one, but it
    did not help. It's not clear to me what I should do in the ConvertFrom
    method in this case.

    At first I tried using the context parameter to find the Page object and
    then using the FindControl to get the object reference. But after that, the
    designer started to say that it could not create an instance of the class
    based on the string ID.

    I tried the following code:

    if (value is string)

    return base.ConvertFrom(context, culture, value);

    Any ideas?

    Thank you again. I really appreciate your help.

    Italo Gomes.
    Italo Gomes, Jul 15, 2008
  5. Italo Gomes

    Italo Gomes Guest

    I was thinking about the string value to store the object's ID. I agree this
    would make it easier to deal with the fact of the way ASP.NET stores the
    relationships, but in this case I miss the part that shows the available and
    compatible objects in the property grid as a combo-box.

    I mean, there should be some kind of helper class to scan the entire page
    after the object's ID which are or derive from the class the property is

    Am I right?

    Sorry for so many basic questions, the beggining days are not always easy...
    Italo Gomes, Jul 15, 2008
  6. Italo Gomes

    Italo Gomes Guest

    I have just found the GetStandardValues which may be what I was looking
    for... (at least I hope so) :)
    Italo Gomes, Jul 15, 2008
  7. Italo Gomes

    Teemu Keiski Guest


    I suggest you pick Reflector and have a look at some type converters in .NET
    Framework. They are good for inspection and seeing how they can be used.

    There's also a good walkthrough:

    How To:Implement a Type Converter
    Teemu Keiski, Aug 3, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.