locked
CompositeControl and XML Deserialization on design-time problem RRS feed

  • Question

  • User-1598340850 posted

    <strike>I get "Error rendering control" error that only happens when I place the control on the webform in desgin-mode, if I run the page the control is displayed correctly.</strike>

    The first  sentence is not important, because this error occurs due to returned toolbars object as null.


    After debugging, the problem is in a function that is called from CeateChildControls():


    public static ToolBars LoadToolbarsFromConfigFile()
    {

    ToolBars toolbars;
    Assembly executingAssembly = Assembly.GetExecutingAssembly();
    string resource = "Editor.ConfigFiles.ToolBars.xml";
    using (Stream stream = executingAssembly.GetManifestResourceStream(resource))
    {
    XmlSerializer serializer = new XmlSerializer(typeof(ToolBars));
    toolbars = (serializer.Deserialize(stream)) as ToolBars;

    }
    return c;
    }

    the returned toolbars is  null! (in design-mode)
    But when I run the page, returned toolbars gets appropriate data.

    It must be something with Assembly, because if I use file stream instead stream, with specified file, it does work.

    Friday, November 27, 2009 9:43 AM

Answers

  • User-16411453 posted

    When I design a control , I use two forms of rendering. One is runtime, and the other is design mode. I model the control in design mode, and then when perfected, I make theruntime control. You be able to adjust for CSharp code

    Render the controls first to the screen. In Protected OnLoad, go ahead and serialize your data, after the controls have been rendered.

    Here's a simple sample control, that makes a webpage header, OnInit is theruntime, renderdesignmode is the designmode, onrender forks the program into design or runtime mode

    Don't run the code in design mode, it can't render it.


    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    <DefaultProperty("Text"), ToolboxData("<{0}:Display_EmbeddedHeader runat=server></{0}:Display_EmbeddedHeader>")> _
    Public Class Display_EmbeddedHeader
    	Inherits WebControl
    
    	Private Panel As Panel
    
    	Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
    
    		If Not Me.Context Is Nothing Then
    			RenderContents(writer)
    		Else
    			RenderDesignMode(writer)
    		End If
    
    	End Sub
    	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    		MyBase.OnInit(e)
    
    		Controls.Clear()
    
    		Dim cs As ClientScriptManager = Me.Page.ClientScript
    		Dim rsType As Type = Me.GetType()
    
    		Dim Comment As LiteralControl
    		Comment = New LiteralControl
    		Comment.Text = "<!-- Internet Commerce Engine 5 Display_EmbeddedHeader Control V1.4.2 for ASP.NET -->" & vbCrLf
    		Controls.Add(Comment)
    
    		Panel = New Panel
    		With Panel
    			.ID = [ID] & "_EmbeddedHeader"
    			.Width = 960
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.TextAlign, "center")
    			.Style.Add(HtmlTextWriterStyle.Margin, "0px auto")
    		End With
    		Controls.Add(Panel)
    
    		Dim table_Header As Table
    		table_Header = New Table
    		With table_Header
    			.CellPadding = 0
    			.CellSpacing = 0
    			.Width = 960
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.Width, "65px")
    		End With
    		Panel.Controls.Add(table_Header)
    
    		Dim tr_Header As TableRow
    		tr_Header = New TableRow
    		With tr_Header
    		End With
    		table_Header.Controls.Add(tr_Header)
    
    		Dim td_Header As TableCell
    		td_Header = New TableCell
    		With td_Header
    			'.CssClass = "DefaultHeader"
    			.Width = [Width]
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, [Width].ToString)
    			.Style.Add(HtmlTextWriterStyle.Height, "65px")
    			.VerticalAlign = VerticalAlign.Middle
    		End With
    		tr_Header.Controls.Add(td_Header)
    
    		Dim img_header As Image
    		img_header = New Image
    		With img_header
    			.ImageAlign = ImageAlign.AbsMiddle
    			.ImageUrl = cs.GetWebResourceUrl(rsType, "redCopper.ice5.admin.Header-TestDrive-Right.gif")
    		End With
    		td_Header.Controls.Add(img_header)
    
    
    	End Sub
    	Private Sub RenderDesignMode(ByVal writer As HtmlTextWriter)
    
    		Controls.Clear()
    
    		Dim cs As ClientScriptManager = Me.Page.ClientScript
    		Dim rsType As Type = Me.GetType()
    
    		Dim Comment As LiteralControl
    		Comment = New LiteralControl
    		Comment.Text = "<!-- Internet Commerce Engine 5 Display_EmbeddedHeader Control V1.4.2 for ASP.NET -->" & vbCrLf
    		Controls.Add(Comment)
    
    		Panel = New Panel
    		With Panel
    			.ID = [ID] & "_EmbeddedHeader"
    			.Width = 960
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.TextAlign, "center")
    			.Style.Add(HtmlTextWriterStyle.Margin, "0px auto")
    		End With
    		Controls.Add(Panel)
    
    		Dim table_Header As Table
    		table_Header = New Table
    		With table_Header
    			.CellPadding = 0
    			.CellSpacing = 0
    			.Width = 960
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.Width, "65px")
    		End With
    		Panel.Controls.Add(table_Header)
    
    		Dim tr_Header As TableRow
    		tr_Header = New TableRow
    		With tr_Header
    		End With
    		table_Header.Controls.Add(tr_Header)
    
    		Dim td_Header As TableCell
    		td_Header = New TableCell
    		With td_Header
    			'.CssClass = "DefaultHeader"
    			.Width = [Width]
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, [Width].ToString)
    			.Style.Add(HtmlTextWriterStyle.Height, "65px")
    			.VerticalAlign = VerticalAlign.Middle
    		End With
    		tr_Header.Controls.Add(td_Header)
    
    		Dim img_header As Image
    		img_header = New Image
    		With img_header
    			.ImageAlign = ImageAlign.AbsMiddle
    			.ImageUrl = cs.GetWebResourceUrl(rsType, "redCopper.ice5.admin.Header-TestDrive-Right.gif")
    		End With
    		td_Header.Controls.Add(img_header)
    
    		Panel.RenderControl(writer)
    
    	End Sub
    	<Bindable(True)> _
     <Category("Appearance")> _
     <DefaultValue("")> _
     <Localizable(True)> Property CssStyle_Text() As String
    		Get
    			Dim s As String = CStr(ViewState("CssStyle_Text"))
    			If s Is Nothing Then
    				Return String.Empty
    			Else
    				Return s
    			End If
    		End Get
    
    		Set(ByVal Value As String)
    			ViewState("CssStyle_Text") = Value
    		End Set
    	End Property
    
    End Class
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 5, 2009 2:44 AM

All replies

  • User-1635004338 posted

    Hi markiz,

    Declare your control in web.config with namespace and assembly:

    http://markitup.com/Posts/Post.aspx?postId=3c0625ff-d86f-4e2f-aabe-f47c9cca0999

    Or a incompatibility issue:

    http://www.codenewsgroups.net/group/microsoft.public.dotnet.framework.aspnet.webcontrols/topic9496.aspx

    Thanks,

    Sunday, November 29, 2009 10:40 PM
  • User-16411453 posted

    RenderDesignMode is for just rendering the control, so you can set properties, and adjust the look and feel of the control.

    When you run the control, you can do anything you want with it. The render mode is sort of dumb, and is not a web browser. Some folks out there will differ with me, but design mode has it's limitations. So that why you have 2 routines, one for runtime, and one for designmode. You make a call to detect what mode your in, and display the appropiate code for it.

    Sunday, November 29, 2009 11:03 PM
  • User-1598340850 posted


    web.config didn't work, anyway why should it if the control already declared on the page like:

    <%@ Register assembly="Editor" namespace="Editor" tagprefix="cc1" %>


    And for the ncompatibility issue I use VS 2008 for both control and the web page.

    Monday, November 30, 2009 2:16 AM
  • User-1598340850 posted

    RenderDesignMode is for just rendering the control, so you can set properties, and adjust the look and feel of the control.

    When you run the control, you can do anything you want with it. The render mode is sort of dumb, and is not a web browser. Some folks out there will differ with me, but design mode has it's limitations. So that why you have 2 routines, one for runtime, and one for designmode. You make a call to detect what mode your in, and display the appropiate code for it.

    So what are you saying is that i should display,on the design-time, "dummy" control?

    I am sure there must be a way to do what I need....


    Again this only happens when I try to deserialize a stream from resource, if I deserialize from filestream, ii works for both cases , design and run time.



    Monday, November 30, 2009 2:25 AM
  • User-16411453 posted

    When I design a control , I use two forms of rendering. One is runtime, and the other is design mode. I model the control in design mode, and then when perfected, I make theruntime control. You be able to adjust for CSharp code

    Render the controls first to the screen. In Protected OnLoad, go ahead and serialize your data, after the controls have been rendered.

    Here's a simple sample control, that makes a webpage header, OnInit is theruntime, renderdesignmode is the designmode, onrender forks the program into design or runtime mode

    Don't run the code in design mode, it can't render it.


    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    <DefaultProperty("Text"), ToolboxData("<{0}:Display_EmbeddedHeader runat=server></{0}:Display_EmbeddedHeader>")> _
    Public Class Display_EmbeddedHeader
    	Inherits WebControl
    
    	Private Panel As Panel
    
    	Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
    
    		If Not Me.Context Is Nothing Then
    			RenderContents(writer)
    		Else
    			RenderDesignMode(writer)
    		End If
    
    	End Sub
    	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    		MyBase.OnInit(e)
    
    		Controls.Clear()
    
    		Dim cs As ClientScriptManager = Me.Page.ClientScript
    		Dim rsType As Type = Me.GetType()
    
    		Dim Comment As LiteralControl
    		Comment = New LiteralControl
    		Comment.Text = "<!-- Internet Commerce Engine 5 Display_EmbeddedHeader Control V1.4.2 for ASP.NET -->" & vbCrLf
    		Controls.Add(Comment)
    
    		Panel = New Panel
    		With Panel
    			.ID = [ID] & "_EmbeddedHeader"
    			.Width = 960
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.TextAlign, "center")
    			.Style.Add(HtmlTextWriterStyle.Margin, "0px auto")
    		End With
    		Controls.Add(Panel)
    
    		Dim table_Header As Table
    		table_Header = New Table
    		With table_Header
    			.CellPadding = 0
    			.CellSpacing = 0
    			.Width = 960
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.Width, "65px")
    		End With
    		Panel.Controls.Add(table_Header)
    
    		Dim tr_Header As TableRow
    		tr_Header = New TableRow
    		With tr_Header
    		End With
    		table_Header.Controls.Add(tr_Header)
    
    		Dim td_Header As TableCell
    		td_Header = New TableCell
    		With td_Header
    			'.CssClass = "DefaultHeader"
    			.Width = [Width]
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, [Width].ToString)
    			.Style.Add(HtmlTextWriterStyle.Height, "65px")
    			.VerticalAlign = VerticalAlign.Middle
    		End With
    		tr_Header.Controls.Add(td_Header)
    
    		Dim img_header As Image
    		img_header = New Image
    		With img_header
    			.ImageAlign = ImageAlign.AbsMiddle
    			.ImageUrl = cs.GetWebResourceUrl(rsType, "redCopper.ice5.admin.Header-TestDrive-Right.gif")
    		End With
    		td_Header.Controls.Add(img_header)
    
    
    	End Sub
    	Private Sub RenderDesignMode(ByVal writer As HtmlTextWriter)
    
    		Controls.Clear()
    
    		Dim cs As ClientScriptManager = Me.Page.ClientScript
    		Dim rsType As Type = Me.GetType()
    
    		Dim Comment As LiteralControl
    		Comment = New LiteralControl
    		Comment.Text = "<!-- Internet Commerce Engine 5 Display_EmbeddedHeader Control V1.4.2 for ASP.NET -->" & vbCrLf
    		Controls.Add(Comment)
    
    		Panel = New Panel
    		With Panel
    			.ID = [ID] & "_EmbeddedHeader"
    			.Width = 960
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.TextAlign, "center")
    			.Style.Add(HtmlTextWriterStyle.Margin, "0px auto")
    		End With
    		Controls.Add(Panel)
    
    		Dim table_Header As Table
    		table_Header = New Table
    		With table_Header
    			.CellPadding = 0
    			.CellSpacing = 0
    			.Width = 960
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, "960px")
    			.Style.Add(HtmlTextWriterStyle.Width, "65px")
    		End With
    		Panel.Controls.Add(table_Header)
    
    		Dim tr_Header As TableRow
    		tr_Header = New TableRow
    		With tr_Header
    		End With
    		table_Header.Controls.Add(tr_Header)
    
    		Dim td_Header As TableCell
    		td_Header = New TableCell
    		With td_Header
    			'.CssClass = "DefaultHeader"
    			.Width = [Width]
    			.Height = 65
    			.Style.Add(HtmlTextWriterStyle.Width, [Width].ToString)
    			.Style.Add(HtmlTextWriterStyle.Height, "65px")
    			.VerticalAlign = VerticalAlign.Middle
    		End With
    		tr_Header.Controls.Add(td_Header)
    
    		Dim img_header As Image
    		img_header = New Image
    		With img_header
    			.ImageAlign = ImageAlign.AbsMiddle
    			.ImageUrl = cs.GetWebResourceUrl(rsType, "redCopper.ice5.admin.Header-TestDrive-Right.gif")
    		End With
    		td_Header.Controls.Add(img_header)
    
    		Panel.RenderControl(writer)
    
    	End Sub
    	<Bindable(True)> _
     <Category("Appearance")> _
     <DefaultValue("")> _
     <Localizable(True)> Property CssStyle_Text() As String
    		Get
    			Dim s As String = CStr(ViewState("CssStyle_Text"))
    			If s Is Nothing Then
    				Return String.Empty
    			Else
    				Return s
    			End If
    		End Get
    
    		Set(ByVal Value As String)
    			ViewState("CssStyle_Text") = Value
    		End Set
    	End Property
    
    End Class
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 5, 2009 2:44 AM