CompositeControl fails to display in Design view

Discussion in 'ASP .Net Building Controls' started by MC, Oct 11, 2007.

  1. MC

    MC Guest

    I'm getting the following error messages when I view my
    ComposisteControl in design time mode.

    "Error Rendering Control - <My Control Name>"
    "An unhandled exception has occurred
    Object reference not set to an instance of an object"

    The Control is as follows: -

    public class TrackedAd : CompositeControl
    {
    public SqlDataSource sdsAds = new SqlDataSource();
    public System.Web.UI.WebControls.AdRotator adRot = new
    System.Web.UI.WebControls.AdRotator();

    private string _Target = "_blank";
    private int _SponsorId = -1;

    [Description("The Browser Target for the Attribute"),
    TypeConverterAttribute(typeof(TargetConverter))]
    public string Target
    {
    get { return _Target; }
    set { _Target = value; }
    }

    public int SponsorId
    {
    get { return _SponsorId; }
    set { _SponsorId = value; }
    }

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    Page.RegisterRequiresControlState(this);
    }

    protected override object SaveControlState()
    {
    object[] state = { base.SaveControlState(), _Target,
    _SponsorId };
    return state;

    }

    protected override void LoadControlState(object state)
    {
    if (state != null)
    {
    object[] stateTmp = (object[])state;
    base.LoadControlState(stateTmp[0]);
    _Target = (string)stateTmp[1];
    _SponsorId = (int)stateTmp[2];
    }
    }

    protected override void CreateChildControls()
    {
    sdsAds.ConnectionString =
    ConfigurationManager.ConnectionStrings["RedsSponsors"].ConnectionString;
    sdsAds.ProviderName =
    ConfigurationManager.ConnectionStrings["RedsSponsors"].ProviderName;
    sdsAds.SelectCommand = string.Format("SELECT
    '{0}'+iif(ImageName<>null,ImageName,'../transp.gif') AS ImageUrl,
    CompanyName AS AlternateText, URL AS NavigateUrl, Impressions, Clicks
    FROM Sponsors WHERE Id={1}", Utils.SponsorsImageBase, SponsorId);
    sdsAds.ID = "sdsAds";

    adRot.AdCreated += new AdCreatedEventHandler(this_AdCreated);
    adRot.DataSourceID = "sdsAds";
    adRot.Target = Target;

    Controls.Add(sdsAds);
    Controls.Add(adRot);
    }

    protected void this_AdCreated(object sender, AdCreatedEventArgs e)
    {
    SponsorsUtils.IncrementAdvertStat("CompanyName",
    "Impressions", e.AlternateText);
    if (e.NavigateUrl != "")
    {
    e.NavigateUrl =
    string.Format("~/Adverts/AdvertHandler.ashx?Url={0}",
    Page.Server.UrlEncode(e.NavigateUrl));
    }
    }
    }

    What is wrong? I thought by using compositecontrol I would not have to
    worry about designtime?
     
    MC, Oct 11, 2007
    #1
    1. Advertisements

  2. MC

    Philvb Guest

    Hi MC,
    the message you are getting here is from your logic in the rendering of the
    control at a guess i'd say that an object hasn't been instanciated.
    But here's how i debug ALL my custom controls in real time.

    In Order to debug a Custom Control you need to Start ANOTHER copy of Visual
    Studio along side your existing one,and pointing it to a test website.
    This can be done by

    Going To The Controls Project Properties and Selecting The Debug Tab then
    set the start action to `Start External Program` and set the box next to it
    to the location of devenv.exe from the correct folder of program files.

    i am using orcas beta2 here and this path is:
    C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe


    Now set the Command Line Arguments to: /Command File.OpenWebsite
    TheTestingWebSite.sln

    Now The Final Step Is To Enter The FULL Path of Your SOLUTION FILE in the
    Working Directory text box.

    Just press F5 to start your debugging Adventure...

    hope this helps
    Wbr
    Phil

     
    Philvb, Oct 11, 2007
    #2
    1. Advertisements

  3. MC

    MC Guest

    I have no problem with the Custom Control, It works fine in the browser,
    It is the design time experience that is not working.

    Can you debug the designer in Visual Studio?
     
    MC, Oct 11, 2007
    #3
  4. MC

    Philvb Guest

    Hi MC,
    Yes, if you follow the instructions then that is how you can debug the
    designer.
    Phil

     
    Philvb, Oct 11, 2007
    #4
  5. MC

    MC Guest

    apologies, should have read your description in more detail
     
    MC, Oct 11, 2007
    #5
  6. MC

    MC Guest

    I think I've followed your instructions however I get no exceptions.

    I have an empty site
    I set the debug to run devenv.exe (vs2005)
    It Loads my project fine
    I then view the page with the broken control on it and nothing happens?

    am I doing something wrong?
     
    MC, Oct 11, 2007
    #6
  7. MC

    Philvb Guest

    Hi MC,
    one thing that i have notice is that even at design time you add a
    handler for the ADCreated, i'm not sure this handler will work due to it not
    being just markup, try reformatting it like:

    If (DesignMode == False) {adRot.AdCreated += new
    AdCreatedEventHandler(this_AdCreated)}

    Not sure i wrote that correctly as i write VB, but i'm sure you get the
    meaning.

    Phil
     
    Philvb, Oct 11, 2007
    #7
  8. MC

    Philvb Guest

    Hi MC,
    one thing that i have notice is that even at design time you add a
    handler for the ADCreated, i'm not sure this handler will work due to it not
    being just markup, try reformatting it like:

    If (DesignMode == False) {adRot.AdCreated += new
    AdCreatedEventHandler(this_AdCreated)}

    Not sure i wrote that correctly as i write VB, but i'm sure you get the
    meaning.

    Phil
     
    Philvb, Oct 11, 2007
    #8
  9. MC

    MC Guest

    I've tried to simplify the problem and have removed all functionality
    from the code other than a stock adRotator (see code Below) and I still
    get the error. I also get the error with just a SqlDataSource as well.

    public class Test : CompositeControl
    {
    protected override void CreateChildControls()
    {
    AdRotator ar = new AdRotator();
    Controls.Add(ar);
    }
    }

    However my other control Test2 works fine


    public class Test2 : CompositeControl
    {
    protected override void CreateChildControls()
    {
    Label lbl = new Label();
    lbl.Text = "Test Label";
    Controls.Add(lbl);
    TextBox tb = new TextBox();
    Controls.Add(tb);
    }
    }
     
    MC, Oct 11, 2007
    #9
  10. Hi MC,

    Regarding on the problem, I've reproed it locally. After some debugging, I
    found that the NullReference exception is caused by the following method of
    AdRotator control:

    AdRotator.BaseUrl(an internal property)

    This property will be called during the rendering of Adrotator and it rely
    on the "TemplateControl" property of AdRotator. However, this
    "TemplateControl" property is null at design-time, thus result to the
    exception. I've performed some further research and seems so far we
    haven't any means to maually set the TemplateControl(to a valid one like
    runtime). One way to resolve this should be create a controldesigner for
    your custom control and manually render out some design-time html fragment.
    For example:

    #in the custom controldesigner's "GetDesignTimeHtml" method, we can define
    our own html fragment that want to output at design-time
    ======================
    [Designer(typeof(TestWebControlDesigner), typeof(IDesigner))]
    public class TestWebControl : CompositeControl
    {
    ..............
    }


    public class TestWebControlDesigner : ControlDesigner
    {

    public override string GetDesignTimeHtml()
    {
    return "<br/>Hello, this is Adrotator composite.";
    }
    }

    ===================

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.




    --------------------
     
    Steven Cheng[MSFT], Oct 12, 2007
    #10
  11. MC

    MC Guest

    Hi Steven,

    Thanks again for your time, your response was as expected. I will add
    the ControlDesigner.

    Could you explain how when using a ascx (user control) file everything
    is fine what extra functionality does this impliment in adition to a
    custom control?
     
    MC, Oct 12, 2007
    #11
  12. Thanks for your reply MC,

    For ascx user control scenario, it is just as if you put the control on the
    page directly. In that case, since the control is in a template control's
    template, it is still a standalone control, (unlike a child control in
    compsite control), it still can utilitze its design-time setting and use
    its custom controldesigner to render the designt-time html(that why no
    exception occur). While you use it as child control of composite control,
    it use runtime code to render out html at design-time and the exception
    occurs.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    --------------------
     
    Steven Cheng[MSFT], Oct 15, 2007
    #12
    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.