April 29, 2015

How to create dynamic page in DNN

Posted By: Unknown - 6:21 PM

Share

& Comment



Most of the people using DotNetNuke an Open Source CMS platform to manage his content in his website but don't know how to create dynamic pages. Here is the some guidance to make pages dynamic, fetch data from database and display on the page.

Here I show you how to bind Country names in DropDownList control using DNN module.

  • First of all open your DNN website in Visual Studio. You can see the DesktopModules folder in your root directory, right click on the directory and add new directory give it to name as your module name, for example "CountryState". Now right click on the CountryState folder and Add->Add New Item->Select Web User Control and give it to name as your module name like "CountryState.ascx", it will show you CountryState.ascx and CountryState.ascx.cs files in DesktopModules/CountryState folder.

CountryState.ascx.cs file looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DesktopModules_CountryState_CountryState : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

  • Next step to update inherit class "System.Web.UI.UserControl" to "PortalModuleBase". This is because of the PortalModuleBase class defines a custom base class inherited by all desktop portal modules within the Portal. The PortalModuleBase class defines portal specific properties that are used by the portal framework to correctly display portal module.



Next step to add DropDownList control in .ASCX file as we already doing in .ASPX page and write logic in .ASCX.CS file to fill out DropDownList.

.ASCX MarkUp:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CountryState.ascx.cs" 
    Inherits="DesktopModules_CountryState_CountryState" %>


.ASCX.CS Logic:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;

public partial class DesktopModules_CountryState_CountryState : PortalModuleBase
{
    SqlConnection moConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
    SqlCommand moCommand = new SqlCommand();
    SqlDataAdapter moAdapter = new SqlDataAdapter();
    DataSet moDataSet = new DataSet();
    string msQuery;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindCountryList();
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        msQuery = "select * from states where CountryID="+ ddlCountry.SelectedValue +";";
        Execommand(msQuery);
        moAdapter.Fill(moDataSet);

        DataTable loTable = new DataTable();
        loTable = moDataSet.Tables[0];

        ddlState.DataSource = loTable;
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateID";
        ddlState.DataBind();
        ddlState.Items.Insert(0, new ListItem("Select State", "0"));
    }

    private void bindCountryList()
    {
        msQuery = "select * from countries;";
        Execommand(msQuery);
        moAdapter.Fill(moDataSet);

        DataTable loTable = new DataTable();
        loTable = moDataSet.Tables[0];

        ddlCountry.DataSource = loTable;
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryID";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("Select Country", "0"));
    }

    public void Execommand(string fsString)
    {
        try
        {
            moDataSet.Reset();
            moCommand = new SqlCommand(fsString, moConnection);
            moAdapter = new SqlDataAdapter(moCommand);
            moConnection.Open();
            moCommand.ExecuteNonQuery();
            moConnection.Close();
        }
        catch (Exception) { }
    }
}

  • Now logged-In in your website using your Host credential. 
  • In DNN panel, go-to Host->Extensions.
  • Click on Create New Module button on right top corner of the Extensions Page.


  • Create New Module button open a popup window with Create Module Form DropDownList. Select Control from that Drop-Down. 
  • It will show you form, select your .ascx web user control  in that from as below and click on Create Module button.

  • Finally Drag-N-Drop module on your page.

  • Enjoy! ;)

Source Code: Download Here

About Unknown

Keval Gangani is a web developer with more than five years of industry experience. On this blog, I share my ideas about various web development trends and technologies. I have worked mainly on Web Projects, Enterprise Portals and CMSes.

0 comments:

Post a Comment

Copyright © 2015 Keval's Blog™ is a registered trademark.

Designed by Templateism