We help IT Professionals succeed at work.

CompositeControl fails to display in Design view

McExp
McExp asked
on
491 Views
Last Modified: 2013-11-07
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?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Are you generating design-time HTML?

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

Bob
Top Expert 2007

Author

Commented:
This Shouldn't be necessary as I have inherited from compositeControl which is supposed to deal with the design time issues
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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
Top Expert 2007

Author

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);
        }
    }
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2007

Author

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.;
Top Expert 2007

Author

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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.