You are on page 1of 10

I.

Mục đích Module HelloWorld

II. Các file Module HelloWorld

Tạo ra một Solution ”Blank C#" tên là HelloWorld .
Trông Solution này có Project là AG.HelloWorld và một Project thư viện
AG.HelloWorld.Controller . Project thư viện này chứa các mã lệnh để kết nối cơ sở
dữ liệu.
Sau khi tạo ra 2 Project này, chúng ta bắt tay vào xây dựng Module.
1. Xây dựng bảng CSDL và các store Procedure

1.Trong Project AG.HelloWorld.Controller, chúng ta Add reference các file *.dll như
sau:

Sau đó thêm 2 Class HelloControllerBase.cs và SqlHelloController.cs

File HelloControllerBase.cs :
using System;
using System.Data;
using DotNetNuke;

namespace AG.HelloWorld
{
public abstract class HelloControllerBase
{

namespace AG.Collections.ApplicationBlocks.Entities. using DotNetNuke.Providers[m_objProviderConfig. private string m_strConnection.HelloWorld { public class SqlHelloController : HelloControllerBase { # region "Private methods" #region Fields & Properties private ProviderConfiguration m_objProviderConfig. using DotNetNuke.ConfigurationManager.Configuration.UI.Entities.Providers.AppSettings[objProvider.Web.WebControls. using System.At tributes["connectionStringName"]] != "")) { . // Read the configuration specific information for this provider Provider objProvider = (Provider)m_objProviderConfig. string content. string content.Attributes["connectionStringName"] != "") && (System.DefaultPr ovider].IO. private string m_strObjectQualifier. public abstract void UpdateText(int moduleID. int userID). using System. using System. //Read the attribute for this provider if ((objProvider.Search.GetProviderConfiguration(PROVIDER_TYPE). using System. private string m_strDatabaseOwner. using DotNetNuke.cs using System.Framework.Modules.cs kế thừa các hàm trong file HelloControllerBase. private string m_strProviderPath. #region "Abtractn methods" public abstract void AddText(int moduleID. using Microsoft. using DotNetNuke.Services.Data. public abstract string GetText(int moduleID). #endregion #endregion #region "Contructor" //Kết nối cơ sở dữ liệu public SqlHelloController() { m_objProviderConfig = ProviderConfiguration.Tabs.Data.Data.SqlClient. #endregion } } File SqlHelloController. using System.Configuration. using System. private const string PROVIDER_TYPE = "data". int userID).

string content. m_strObjectQualifier. } m_strProviderPath = objProvider.".Configuration. if (m_strDatabaseOwner != null && ! m_strDatabaseOwner. GetSPName("GetText"). .GetNull(field. if (m_strObjectQualifier != null && ! m_strObjectQualifier.ExecuteNonQuery(m_strConnection. userID).Att ributes["connectionStringName"]]. storeName). content. GetText và UpdateText public override void AddText(int moduleID.ConfigurationManager. GetSPName("AddText"). objResult = SqlHelper. int userID) { SqlHelper.ToString(). } #endregion #region "Public Methods" private object GetNull(object field) { return DotNetNuke.Null.Attributes["databaseOwner"]. m_strDatabaseOwner = objProvider. if (objResult != null) return objResult. } #endregion #region Method Utility //Hàm trả lại tên Store Procedure private string GetSPName(string storeName) { return string.Attributes["providerPath"]. } public override void UpdateText(int moduleID.Attributes["connectionString"].Value). GetSPName("UpdateText").AppSettings[objProvider. int userID) { SqlHelper.Common.")) m_strDatabaseOwner += ". } public override string GetText(int moduleID) { object objResult. } //Các hàm AddText.ExecuteScalar(m_strConnection. m_strDatabaseOwner. content.Attributes["objectQualifier"]. DBNull. string content. moduleID.EndsWith(".EndsWith("_")) m_strObjectQualifier += "_". moduleID). m_strObjectQualifier = objProvider.Format("{0}{1}AGHelloWorld_{2}". moduleID. userID). } else { m_strConnection = objProvider. m_strConnection = System.Utilities. else return null.ExecuteNonQuery(m_strConnection.

dll của DotNetNuke như sau vào thư mục Bin: Thư mục AG.HellOWorld chứa cá User controls của mình. } #endregion } } 3.ascx. Trong Project AG.HelloWorld.HelloWorld/HelloWorldSettings. Giao diện rất đơn giản.ascx dùng để hiện thị đoạn text ra màn hình. Chúng ta Add Reference các file *.ascx.HellOWorld .HellOWorld gồm 3 User Control làm 3 công việc khác nhau: User Control HellOWorldView.cs" Inherits="AG. Controls(Chứa các Control có sẵn) và Desktop Module/AG.ascx" %> <asp:Label ID="lblHelloWorld" runat="server"></asp:Label> HellOWorldView. chúng ta tạo ra các Folder Bin (Chứa các file *.dll). chỉ có một Label để hiện thị dòng Text người dùng đánh vào: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="HelloWorldView.HelloWorldView" %> <%@ Reference Control="~/DesktopModules/AG.cs như sau: .

UI.using System.Modules. IActionable { private bool m_blnTextBold.Web. protected void Page_Load(object sender. if (m_blnTextItalic) strContent = "<i>" + strContent + "</i>".Equals(this. using DotNetNuke. using DotNetNuke.Entities.Equals(this.Localization.UI.Text = Server. .Entities. if (m_blnTextUnderline) strContent = "<u>" + strContent + "</u>". string strContent = objController. using System. namespace AG. EventArgs e) { if (!IsPostBack) { try { SqlHelloController objController = new SqlHelloController().Entities.Services. using DotNetNuke.GetText(this. ex).ProcessModuleLoadException(this. using System.Length != 0) { if (m_blnTextBold) strContent = "<b>" + strContent + "</b>". if (strContent != null && strContent.Settings[HelloWorldSettings. using System.Exceptions.Drawing.Web.ModuleId).WebControls.Services. using DotNetNuke.Data.Web. using DotNetNuke.HtmlDecode(strContent).UserControls.UI.Modules. using DotNetNuke. using DotNetNuke. } } catch (Exception ex) { Exceptions. private bool m_blnTextItalic.TEXT_BOLD_KEY]). lblHelloWorld. using System.Security. using System. } } } #region Web Form Designer generated code protected override void OnInit(EventArgs e) { m_blnTextBold = ! "false". m_blnTextItalic = ! "false". using DotNetNuke.HtmlControls.Actions.TEXT_ITALIC_KEY]). private bool m_blnTextUnderline.HelloWorld { public partial class HelloWorldView : PortalModuleBase.Settings[HelloWorldSettings.

Security. ModuleActionType. } ModuleActionCollection objActions = new ModuleActionCollection(). InitializeComponent(). DotNetNuke.cs" Inherits="AG.SecurityAccessLevel.Settings[HelloWorldSettings. "". "". false. "". this.Add(this. true.ascx" TagName="texteditor" TagPrefix="ucl" %> <table id="Table1" style="font-family:Tahoma.ascx.Edit. false).GetNextActionID(). return objActions.EditUrl("Edit"). font-size:medium" cellpadding="1" cellspacing="1" width="100%" border="0"> <tr> <td></td> </tr> <tr> <td> <ucl:texteditor Width="100%" Height="400" ID="txtMain" runat="server" /> </td> </tr> <tr> <td align="center"> .TEXT_UNDERLINE_KEY]).AddContent. } private void InitializeComponent() { } #endregion #region IActionable Members public ModuleActionCollection ModuleActions { get { //throw new Exception("The method or operation is not implemented. objActions. m_blnTextUnderline = ! "false".").OnInit(e). base. "Edit Text".Equals(this. Giao diện gồm 2 nút Update và Cancel: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="HelloWorldEditing. } } #endregion } } User HellOWorldEditing.HelloWorldEditing" %> <%@ Register Src="~/Controls/texteditor.HelloWorld.ascx dùng để chỉnh sửa nội dung đoạn Text.

using DotNetNuke.Text = "Add new content . } } ViewState["IsNew"] = m_isNew.Entities. //Kiểm tra đã có dữ liệu trong database chưa. try { SqlHelloController objController = new SqlHelloController(). m_isNew = true. using System. using DotNetNuke. using System.ProcessModuleLoadException(this. m_isNew = Convert.cs : using System.Drawing.GetText(ModuleId). using System.Services. <asp:LinkButton id="btnCancel" runat="server" OnClick="btnCancel_Click">Cancel</asp:LinkButton>&nbsp.Services. <asp:LinkButton id="btnUpdate" runat="server" OnClick="btnUpdate_Click">Update</asp:LinkButton>&nbsp.ascx. using System.UI.UI. } .UI.IsPostBack) { if (strContent != null) { txtMain.Common.Services. } catch (Exception ex) { Exceptions. ex).Exceptions. using DotNetNuke. using System. using DotNetNuke. using DotNetNuke.Entities. EventArgs e) { //Todo: Hiển thị nội dung thông báo.Web. } else { txtMain.HelloWorld { public partial class HelloWorldEditing : PortalModuleBase { protected bool m_isNew = true.Web. m_isNew = false.Text = strContent. using DotNetNuke.WebControls.".Localization. protected void Page_Load(object sender..Modules. if (!this. using DotNetNuke.ToBoolean(ViewState["IsNew"]). namespace AG. </td> </tr> </table> HellOWorldEditing.Web.Data.. string strContent = objController.UserControls.HtmlControls. using DotNetNuke.

ascx dùng để chỉnh sửa về giao diện (In nghiêng. Giao diện gồm 3 nút checkBox: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="HelloWorldSettings. ex). base.UserId).NavigateURL(). EventArgs e) { try { SqlHelloController objController = new SqlHelloController().ascx. true).HelloWorld.ProcessModuleLoadException(this. if (m_isNew == true) { objController.Redirect(Globals. } } protected void btnUpdate_Click(object sender. txtMain. font-family:Tahoma. } private void InitializeComponent() { } #endregion protected void btnCancel_Click(object sender.AddText(this.UpdateText(this.Text.Text. } catch (Exception ex) { Exceptions. this.Redirect(Globals.ProcessModuleLoadException(this.UserId). EventArgs e) { try { Response. } else { objController. } } } } User HellOWorldSetting.HelloWorldSettings" %> <table border="0" style="width: 100%.NavigateURL(). } catch (Exception ex) { Exceptions. } // Chuyen ve trang truoc do Response.ModuleId. font-size: 12px"> . gặch dưới) của đoạn Text. } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(). ex). this.cs" Inherits="AG.OnInit(e).ModuleId. đậm. true). txtMain.

WebParts.HelloWorld { public partial class HelloWorldSettings : ModuleSettingsBase { #region "Private Members" private bool m_blnTextBold. using DotNetNuke.UI.Entities. using System. EventArgs e) . using System.Web. using System.Web.Configuration.WebControls. using System. using System.WebControls.Web. #endregion #region "Public Members" public const string TEXT_BOLD_KEY = "TextBold".Web.UI.cs : using System.ascx. #endregion protected void Page_Load(object sender. using System. namespace AG.UI.Security. private bool m_blnTextItalic. using System. public const string TEXT_UNDERLINE_KEY = "TextUnderline".HtmlControls. public const string TEXT_ITALIC_KEY = "TextItalic".<tr> <td> <asp:Label ID="Label1" runat="server" Font-Bold="True"> Text Bold</asp:Label> </td> <td> <asp:CheckBox ID="chkTextBold" runat="server" /> </td> </tr> <tr> <td style="height:16px"> <asp:Label ID="Label2" runat="server" Font-Italic="True"> Text Italic</asp:Label> </td> <td style="height:16px"> <asp:CheckBox ID="chkTextItalic" runat="server" /> </td> </tr> <tr> <td> <asp:Label ID="Label3" runat="server" Font-Bold="False" Font- Underline="True"> Text Underline</asp:Label> </td> <td> <asp:CheckBox ID="chkTextUnderline" runat="server" /> </td> </tr> </table> HellOWorldSetting. private bool m_blnTextUnderline.Modules. using System.Data.UI.Collections.Web.Web. using System.

TabModuleId.TabModuleId.Checked. } public override void UpdateSettings() { m_blnTextBold = chkTextBold. m_blnTextItalic ? "true" : "false"). m_blnTextItalic = ! "false". TEXT_ITALIC_KEY.UpdateSettings().Checked = m_blnTextUnderline.Checked = m_blnTextBold. objController. TEXT_UNDERLINE_KEY.UpdateTabModuleSetting(this. ModuleController objController = new ModuleController(). m_blnTextItalic = chkTextItalic. base. m_blnTextUnderline = chkTextUnderline. m_blnTextBold ? "true" : "false").Checked.UpdateTabModuleSetting(this. objController. chkTextUnderline. } } public override void LoadSettings() { m_blnTextBold = ! "false".Equals(TabModuleSettings[TEXT_BOLD_KEY]).Checked. base.Checked = m_blnTextItalic.Equals(TabModuleSettings[TEXT_ITALIC_KEY]). chkTextItalic.LoadSettings(). m_blnTextUnderline ? "true" : "false"). m_blnTextUnderline = ! "false". TEXT_BOLD_KEY.TabModuleId. objController. { if (!IsPostBack) { chkTextBold.UpdateTabModuleSetting(this. } } } .Equals(TabModuleSettings[TEXT_UNDERLINE_KEY]).