[RESOLVED] Reference user control from a .vb class in the app_code directory

Hi, i've created a user control and i want to have an instance of it passed to a Method in my app_code folder.

Bassically this is what i need(SomeName.vb):

 

Public Shared Sub SomeName(myc as SomeUserControl)


'Some code here

End Sub

 

Please Note that the above file is located in the app_code directory, and the user control is not.

Thanks.

to make the usercontrols specific Type available to your app_code class/method, you can:

  1. <div mce_keep="true">move the usercontrols codebehind file into your app_code folder</div>
  2. <div mce_keep="true">change the usercontrols control declaration to point to the new code location</div>

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="~/app_code/usercontrols/YourUserControl.ascx.vb" Inherits="YourUserControl" %>

In addition to making the usercontrols type available to your app_code classes, your usercontrols type is now also available to all other pages/control in your web.  this is often a convenient way to be able to deal with dynamically created users controls in a strongly typed way even if they have not been drag/dropped onto a page.

 

In addition to Mikes post you could abstract the methods and properties that you really want to access from the UC (User Control) into an abstract / must inherit base class.

This base class would inherit from UserControl and the UC instance would inherit from the Base Class. The base class defintion / code would be in the APP_Code folder, or a referenced library, and the parameter for the functions in the other classes would of the base class type. You could also use an interface, but a base class is likely more appropriate.

The benefit of this approach is that VS will still integrate seamlessly with the code behind file for the UC instance.

About Mike's post, this line:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="~/app_code/usercontrols/YourUserControl.ascx.vb" Inherits="YourUserControl" %>

Does not work for me.  If I create a UserControl, and put it's code behind file in my app_code folder as shown above, (obviously which folder inside the app_code that it's in doesn't matter) I get the following error while building the site:

"The file <relative path to file> is in the special directory 'App_Code', which is not allowed."

Am I doing something wrong?  I read somewhere that it doesn't work with regular websites, you have to have a Web Application Project.  Is that what's wrong?  I have been beating my brains out to try and figure out how to instantiate a UserControl from a codebehind file of a .aspx page in order to dynamically set properties in it and send an HTML email with the UserControl as the HTML template.  I have come to a whoppin road block as nothing works for me in all the examples that I see (it appears that way anyway).

mokeefe

you could abstract the methods and properties that you really want to access from the UC (User Control) into an abstract / must inherit base class.

Martin's solution is what I've seen most-often suggested in similar posts.. (sometimes other people suggest an interface instead of an abstract class, but I believe abstract class is a better way to go, since it enforces the "IS A" relationship).  

Does anybody know if there are any adverse affects to creating these "extra" classes in App_Code?  

thoughts on "[RESOLVED] Reference user control from a .vb class in the app_code directory"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>