Question

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.
The above statement is not important, this error happens because the returned toolbars object is 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 toolbars;                
}

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

If you need some more info about my code please ask.

Update:

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

Another UPDATE:

I've modified my code a bit, and added "dataset" for test purpose:

using (DataSet ds = new DataSet())
{               
    ds.ReadXml(typeof(TheEditor).Assembly.GetManifestResourceStream("Editor.ConfigFiles.ToolBars.xml"));
    //show message box to see if it works
    System.Windows.Forms.MessageBox.Show(ds.Tables.Count.ToString());    
}     

Another thing that I noticed, all above happens when I add my control to a new website project, but if I set debug property of control's project to start external program (i start visual studio) , and there I create a new project and add the control everything works.

Was it helpful?

Solution

When you are running this within Visual Studio, you don't have an Application context, and so you can't "GetExecutingAssembly" on it - or more accurately, the Executing Assembly is devenv.exe, and that doesn't have the resources you're looking for.

You can use the DesignMode property of the control to check and see if you are rendering the control within Visual Studio, and modify your behaviour appropriately:

public static ToolBars LoadToolbarsFromConfigFile()
{
  ToolBars toolbars;
  if (!DesignMode)
  {
    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;
    }
  }
  else
  {
    // Load a dummy toolbar here.
  }
  return toolbars;                
}

Alternatively, you could perform the check in the calling code, but as this is a public method, there's no guarantee that all callers would perform this check, so you're better off doing it in the method.

A final option would be to create a Designer class that overrides the LoadToolbarsFromConfigFile method and supplies a dummy toolbar for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top