MC
10/12/2007 10:29:00 AM
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?
Steven Cheng[MSFT] wrote:
> 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.
>
>
>
>
> --------------------
>> From: MC <mc@community.nospam>
>> Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
>> Subject: Re: CompositeControl fails to display in Design view
>> Date: Thu, 11 Oct 2007 21:10:22 GMT
>
>> 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);
>> }
>> }
>>
>> Philvb wrote:
>>> 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
>>> "MC" wrote:
>>>
>>>> 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 wrote:
>>>>> apologies, should have read your description in more detail
>>>>>
>>>>> Philvb wrote:
>>>>>> Hi MC,
>>>>>> Yes, if you follow the instructions then that is how you can debug
>>>>>> the designer.
>>>>>> Phil
>>>>>>
>>>>>> "MC" wrote:
>>>>>>
>>>>>>> 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?
>>>>>>>
>>>>>>> Philvb wrote:
>>>>>>>> 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
>>>>>>>>
>>>>>>>> "MC" wrote:
>>>>>>>>
>>>>>>>>> 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?
>>>>>>>>>
>