locked
Web references within custom webusercontrols inside webparts RRS feed

  • Question

  • i have a standard asp.net 2.0 web  project that has a web reference to a webservice

    i have created a webusercontrol that calls a function within the webservive

    i then create/deploy a webpart which loads this webusercontrol.

    i have copied the usercontrol ascx & vb files to the _layouts folder.

    the webpart appears in the list of selectable webparts but when i add it to the page i get the following error.

    Object reference not set to an instance of an object. : C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\template\layouts\uscWebPartl.ascx.vb(11): error BC30002: Type 'webGIAC.HCIAccess' is not defined.


    i thought it would be just a matter of adding the web reference key into the main (C:\Inetpub\wwwroot\wss\VirtualDirectories\80) web.config in the appSettings section but this doesnt seem to work. or even the config file in the layouts folder

    i guess there something else i need to be doing?

    Cheers,
    Craig
    • Edited by Craig G Wednesday, October 29, 2008 5:23 PM
    Wednesday, October 29, 2008 5:17 PM

Answers

  • Hi Craig,

    I would suggest some changes what you're doing and how you're doing it.

    First off, the common convention is to place user controls (.ascx) files into a 12\Template\ControlTemplates\<subfolder>.  In your code, you would reference this using the _controltemplates virtual directory (instead of _layouts). This won't fix your problem, but is a better practice.

    Secondly, since you're deploying the user control into the 12 Hive, you should be placing the assembly for the user control into the GAC.  You do not and should not be putting your code behind file (i.e. .vb file) into this folder.  To put the assembly into the GAC, you'll need to strongly name it with a signing key (.snk file).

    Inside your .ascx control declaration, you'll need to reference the class in the GAC.  One way to do this is to specify a Inherits attribute like this

    <%@ Control Language="C#" AutoEventWireup="true" Inherits="namespace.class, assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<token>" %> 
     

    Finally, depending on how you are loading the user control into your web part, there may be code access permission issues.  This can also be resolved if you put your web part assembly into the GAC as well.

    Hope this helps get you on the right track.


    Randy - http://sharepointhawaii.com/randywilliams
    • Proposed as answer by daniel.larson Thursday, October 30, 2008 6:13 AM
    • Marked as answer by Craig G Thursday, October 30, 2008 9:23 AM
    Wednesday, October 29, 2008 7:41 PM

All replies

  • Hi Craig,

    I would suggest some changes what you're doing and how you're doing it.

    First off, the common convention is to place user controls (.ascx) files into a 12\Template\ControlTemplates\<subfolder>.  In your code, you would reference this using the _controltemplates virtual directory (instead of _layouts). This won't fix your problem, but is a better practice.

    Secondly, since you're deploying the user control into the 12 Hive, you should be placing the assembly for the user control into the GAC.  You do not and should not be putting your code behind file (i.e. .vb file) into this folder.  To put the assembly into the GAC, you'll need to strongly name it with a signing key (.snk file).

    Inside your .ascx control declaration, you'll need to reference the class in the GAC.  One way to do this is to specify a Inherits attribute like this

    <%@ Control Language="C#" AutoEventWireup="true" Inherits="namespace.class, assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<token>" %> 
     

    Finally, depending on how you are loading the user control into your web part, there may be code access permission issues.  This can also be resolved if you put your web part assembly into the GAC as well.

    Hope this helps get you on the right track.


    Randy - http://sharepointhawaii.com/randywilliams
    • Proposed as answer by daniel.larson Thursday, October 30, 2008 6:13 AM
    • Marked as answer by Craig G Thursday, October 30, 2008 9:23 AM
    Wednesday, October 29, 2008 7:41 PM
  • hi thanks for the reply

    i was just following this example as im new to sharepoint development

    http://www.a2zdotnet.com/View.aspx?id=95

    i guess i really need my development book to hurry up & arrive!!

    you dont know of any good online examples i could use in the meantime?

    Cheers,
    Craig
    Thursday, October 30, 2008 9:23 AM
  • https://secure.codeproject.com/KB/sharepoint/WebParticles.aspx?display=PrintAll&fid=400774&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=126&select=2242636

     ok i have now followed this walkthrough on creating custom webparts. and i assume this is the more accepted way of doing it, installing in GAC etc

    so the next question is, how do i get this webpart to communicate with a webservice?

    i have the submit button wired up to call a webservie method. but what or where do i need to put the web reference details so sharepoint can use it?


    Cheers,
    Craig
    Thursday, October 30, 2008 5:20 PM