zaczek.net
Home Events Fotos Projects Diplomarbeit   Zitate Download Feedback Lebenslauf
 
Projects  >>  Personal .NET Portal  >>  Making new Modules
Making new Modules

Creating a Module is simple. Just create a directory in the Modules directory and put there the View- and Edit Web User Control. The Controls names must be (or can be reconfigured) <ModuleName>.ascx and Edit<ModuleName>.ascx.

Implementation

Just derive form Portal.API.Module or Portal.API.EditModule and implement "IsVisible" if necessary.

The Module Class provides some properties and methods which describes the Module.

Current Module Settings

Name Description
IsVisible Can be overridden. Tells the Portal Framework if the Module should be rendered or not.
TabRef The current Tab Reference. This is a unique string.
ModuleRef The current Module Reference. The Module Reference is not necessarily unique. TabRef + ModuleRef is unique.
ModuleVirtualPath The virtual path to the Module.
ModulePhysicalPath The physical path to the Module.
BuildURL Build a URL to the current Page. Use this method to implement Modules that needs URL Parameter.
ModuleHasEditRights True if the current user has edit rights.

Configuration

Each Module has the responsibility to store its configuration and state. The Portal API provides some Helper Methods.

Name Description
ModuleConfigFile Physical Path to the configuration file. (<ModulePhysicalPath>\Module_<ModuleRef>.config)
ModuleConfigSchemaFile Physical Path to the configuration schema file. (<ModulePhysicalPath>\ Module_<ModuleRef>.configModule.xsd)
ReadCommonConfig Reads (XML Deserialize) the common configuration file.
ReadConfig Reads (XML Deserialize/Dataset) the configuration file.
WriteConfig Writes (XML Serialize/Dataset) the configuration file.

Furthermore each Module can a configure its control files. These settings are stored in the "ModuleSettings.config" file.

<module>
  <ctrl>Counter.ascx</ctrl>
  <editCtrl>none</editCtrl>
</module>

The "ctrl" Tag defines the View Web User Control. The "editCtrl" Tag can contain "none", which means: there is no Edit Control. E.g. the Login or HitCounter Modules are using this.

Example (Simple Html Module)

This simple Module reads a .htm file and renders it into a DIV Tag.

View Control Html.ascx:

<%@ Control Language="c#" Inherits="Portal.API.Module" %>
<%@ Import namespace="System.IO" %>
<script runat="server">
    private string GetPath()
    {
        return ModulePhysicalPath + ModuleRef + ".htm";
    }

    void Page_Load(object sender, EventArgs args)
    {
        // Open file            
        if(File.Exists(GetPath()))
        {
            FileStream fs = File.OpenRead(GetPath());
            StreamReader sr = new StreamReader(fs);
            content.InnerHtml = sr.ReadToEnd();
            fs.Close();
        }        
    }

</script>
<div id="content" runat="server">
</div>

Edit Control EditHtml.ascx:

<%@ Control Language="c#" autoeventwireup="true" 
    Inherits="Portal.API.EditModule" %>
<%@ Import namespace="System.IO" %>
<script runat="server">

    private string GetPath()
    {
        return ModulePhysicalPath + ModuleRef + ".htm";
    }

    void Page_Load(object sender, EventArgs args)
    {
        if(!IsPostBack)
        {
            // Open file            
            if(File.Exists(GetPath()))
            {
                FileStream fs = File.OpenRead(GetPath());
                StreamReader sr = new StreamReader(fs);
                txt.Text = sr.ReadToEnd();
                fs.Close();
            }
        }
    }
    
    void OnSave(object sender, EventArgs args)
    {
        FileStream fs = null;
        try
        {
            fs = new FileStream(GetPath(), FileMode.OpenOrCreate, 
                 FileAccess.ReadWrite, FileShare.None);
            fs.SetLength(0); // Truncate
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(txt.Text);
            sw.Close();
            
        }
        finally
        {
            if(fs != null)
            {
                fs.Close();
            }
        }
        RedirectBack();
    }

</script>
<asp:TextBox id="txt" Width="100%" Height="300px" 
    TextMode="MultiLine" Runat="server"></asp:TextBox>
<asp:LinkButton CssClass="LinkButton" runat="server" OnClick="OnSave">
    Save & Back</asp:LinkButton>
Google
zaczek.net 08.09.2008