locked
Add web control to web form designer at design time RRS feed

  • Question

  • Sad Someone please help me !!

    I am trying to add a standard web control (e.g. label, textbox, datagrid) to the currently active web form surface in VS2002/2003.

    If I add it using the DesignerHost.CreateComponent method it seems to add to the controls collection OK, and usually the code-behind shows the declarations.

    But the HTML design / source tabs never display the added item.

    How can I get this working? Someone mentioned simulating drop after placing a control in the clipboard. Anyone know how to solve this?

    The exact same code I developed so far is fine and dandy when used for Win Form.

    Thanks - Kevin

    Wednesday, September 28, 2005 12:22 PM

Answers

  • In VS 2002/2003 you should use the IE Dom to insert the HTML for the control.  You can cast the automation object for the web from design view to an IHTMLDocument2 using mshtml primary interop assembly definitions to access the DOM.  Alternatively, you can switch to HTML source view, incert the HTML for the control, and when you switch to design view it will be created.

    In VS 2005 you can still do the previous methods.  However, you might also want to look at WebFormsRootDesigner::AddControlToDocument.  This API was added because versions of VS may not support the IE DOM at design time.

    Thursday, January 5, 2006 8:25 PM

All replies

  • In VS 2002/2003 you should use the IE Dom to insert the HTML for the control.  You can cast the automation object for the web from design view to an IHTMLDocument2 using mshtml primary interop assembly definitions to access the DOM.  Alternatively, you can switch to HTML source view, incert the HTML for the control, and when you switch to design view it will be created.

    In VS 2005 you can still do the previous methods.  However, you might also want to look at WebFormsRootDesigner::AddControlToDocument.  This API was added because versions of VS may not support the IE DOM at design time.

    Thursday, January 5, 2006 8:25 PM
  • Thanks Tim

    Drop web control

    In the end I found I had to create the HTML required to represent the control, place this into the clipboard according to 'HTML Format', and then the web page recognizes its presence and allows a drop onto a web form designer.

    This is what (I believe) happens when you copy a control via ctrl-c and paste it using ctrl-v.

    I think there is another method relying on different clip formats that the toolbox uses (i.e. when you drag from toolbox to form) but I have not figured it out.

    Non-Visual Component in VS2005

    I wonder if you can help with this query (or if you know someone who can help):-

    I have a non-visual component that makes use of IDesigner that I drag/drop onto a web form at design time.

    In Visual Studio 2003, the auto-generated code behind in InitializeComponent is something like (VB):

     'Protected WithEvents myComponent as aNonVisualComponent'

    which allows the aspx page to 'see' it when using something like

    DataSource="<%# myComponent %>"

    BUT

    the SAME component, in VS2005, although it still emits the rest of the relevant code-behind, emits the declaration code as:

     'Private WithEvents myComponent as aNonVisualComponent'

    which means the aspx page will not render because it no longer can access the declared variable. Of course if I change the code-behind it works until the next time IntializeComponent is recreated.


    What steps can I take to remedy this - that is, to get VS2005 to emit Protected (or Public) instead of 'Private'?

    Here's hoping!

    Happy New Year by the way.

    Friday, January 6, 2006 3:31 PM
  • Tim:
     
    Any idea where I can find examples of WebFormsRootDesigner usage?
     
    Josh
    Tuesday, February 7, 2006 6:07 PM
  • Hi Josh,

    The WebFormsRootDesigner class is documented here:

    http://msdn2.microsoft.com/en-us/library/system.web.ui.design.webformsrootdesigner.aspx

    To get an instance of it from an add-in, you can use this article of mine:

    HOWTO: Getting IDesignerHost and IHTMLDocument of WebForms from a Visual Studio .NET add-in

    http://www.mztools.com/articles/2005/MZ004.htm

    And once you have the IDesignerHost, you get the root designer calling:

    objIDesignerHost.GetDesigner(objIDesignerHost.RootComponent)

    --
    Best regards,

    Carlos J. Quintero
    MZ-Tools: Productivity add-ins for Visual Studio
    You can code, design and document much faster:
    http://www.mztools.com

     

    Wednesday, February 8, 2006 12:08 PM
  • Carlos,

    I am trying to build an add-in that will create the web form based on criteria that I provide to it.  Even though I have quite a bit of experience with VB and VB.Net, I am quite new to using this level of extensibility (I've done some class generation add-ins before for VB6 and .Net 03). 

    I had been trying to connect to the designer for a few days and then I found your code.  I integrated the code that you reference in your HOWTO into my project but I am still unable to retreive the designer window.  When the code fires the "oHTML2 = DirectCast(oObject, mshtml.IHTMLDocument2)" I receive an error message.  I was wondering if I was missing a reference or something even though I followed your instructions.  Here is the error:

    "Unable to cast COM object of type 'System.__ComObject' to interface type 'mshtml.IHTMLDocument2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{332C4425-26CB-11D0-B483-00C04FD90119}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). "

    Any assistance you can provide would be appreciated. 

    Thanks,

    Jeremy Lattimore

    Friday, April 7, 2006 7:12 PM