开发者

vb.net load and call files dynamically

开发者 https://www.devze.com 2023-01-27 06:35 出处:网络
I\'m a bit new to .net so i was wondering if a thing like this is possible. I\'m having one basic aspx script that needs to load some \"plugins\".

I'm a bit new to .net so i was wondering if a thing like this is possible. I'm having one basic aspx script that needs to load some "plugins". Every plugin has its own settings and it should perform different logics. What i'd do in php is create a class for each plugin, load that class, pass the settings file to it and call some method on 开发者_开发问答that class that would give me some output. But is it even possible in vb.net? I'd need to have multiple files with multiple classes, load those files, depending on the plugin name and call some method on them to parse that plugin's settings.

Thank you


There are a variety of ways you can accomplish what you're looking to accomplish in ASP.NET. You can use User Controls or Server Controls. The major difference being User Controls are dynamically compiled, like ASP.NET pages, and Server Controls are pre-compiled.

Server Controls are very useful for creating self-contained controls where reuse is very important. Allowing you to deploy a single .dll to projects that need the control and access it much like you would any other shared library in your OOP code.

Adding a Server Control to your page is very straight forward. You simply need to place the .dll in your BIN folder and add a reference to the page. The reference would be similar to this:

<%@ Register TagPrefix="radM" Namespace="Telerik.WebControls" Assembly="RadMenu.Net2" %>

Accessing that control on your page then becomes as simple as the following:

<radm:radmenu id="RadMenu5" runat="server"></radm:radmenu>

User Controls are great for grouping code and controls together in an application. The need to reuse the code across a variety of projects isn't there and you're combining or manipulating controls in a manner that will only be useful in that one application.

Registering and using a User Control is similar to that of a Server Control, as shown below:

<%@ Register TagPrefix="Control1" TagName="Menu" Src="controls/Menu.ascx" %>

Accessing it in code can be done in a similar manner as well:

<Control1:Menu ID="ucMenu" runat="Server" />

You can also add controls to the page in a programmatic fashion if you add a placeholder control to the page first. Similar to the method below:

This code would be in your code-behind file:

Dim ucMenu As UserControl = LoadControl("/Controls/Header.ascx")
MenuPlaceHolder.Controls.Add(ucMenu)

Your .aspx page would then only need the PlaceHolderControl at the spot where you want to dynamically add the control; as show below:

<asp:PlaceHolder ID="MenuPlaceHolder" runat="server" /> 


you can use UserControl for this kind of purpose.
you can load usercontrols in a page and call methods available on the usercontrols and access public members as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消