[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

CompositeControl fails to display in Design view

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?
0
McExp
Asked:
McExp
  • 4
  • 3
1 Solution
 
Bob LearnedCommented:
Are you generating design-time HTML?

ControlDesigner.GetDesignTimeHtml Method
http://msdn2.microsoft.com/en-us/library/ew7620aw.aspx

Bob
0
 
McExpAuthor Commented:
This Shouldn't be necessary as I have inherited from compositeControl which is supposed to deal with the design time issues
0
 
Bob LearnedCommented:
The CompositeControl is an abstract class, and it is up to concrete class (your control) to set the Designer attribute for the class.  There is the CompositeControlDesigner that can help you with that.

Here is an article that talks about GetDesignTimeHtml:

Creating a web based tab control
http://www.codeproject.com/aspnet/WebTabControl.asp?df=100&forumid=304531&exp=0&select=1562618

Bob
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
McExpAuthor Commented:
Be that as it may, The control that is created in the article you cite is inheriting from a Placholder and as such requires it's own designer.

Inorder to prove that no GetDesignTimeHtml is required I have created the following test control. which renders perfectly fine in Desing mode.

    public class Test : CompositeControl
    {
        protected override void CreateChildControls()
        {
            Label lbl = new Label();
            lbl.Text = "Test Label";
            Controls.Add(lbl);
            TextBox tb = new TextBox();
            Controls.Add(tb);
        }
    }
0
 
Bob LearnedCommented:
Well, my friend, you got me there :(

Does AdRotator implement GetDesignTimeHtml?

Bob
0
 
McExpAuthor Commented:
That's one of the things I'm looking into.

I had assumed that it must be doing something becuase it renders fine when used in isolation and within an ascx user control

I have had a other lead thorugh the MS forums, I'm going to find out where the exception is actully triggered in Visual studo.;
0
 
McExpAuthor Commented:
So, I've had word from Microsoft and they have confimred the following: -

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.

So the result is the one I'm guessing we both expected.

Thanks for your input
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now