Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

June 5, 2015

Identify the website operated in which device type whether is it Computer or Mobile

Posted By: Unknown - 8:52 PM


Below is the C# method to check whether it is Computer or Mobile.

public static bool isMobileBrowser()
        {
            //GETS THE CURRENT USER CONTEXT
            HttpContext context = HttpContext.Current;

            //FIRST TRY BUILT IN ASP.NT CHECK
            if (context.Request.Browser.IsMobileDevice)
            {
                return true;
            }
            //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
            if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
            {
                return true;
            }
            //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
            if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
                context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
            {
                return true;
            }
            //AND FINALLY CHECK THE HTTP_USER_AGENT 
            //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
            if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
            {
                //Create a list of all mobile types
                string[] mobiles =
                    new[]
                {
                    "midp", "j2me", "avant", "docomo", 
                    "novarra", "palmos", "palmsource", 
                    "240x320", "opwv", "chtml",
                    "pda", "windows ce", "mmp/", 
                    "blackberry", "mib/", "symbian", 
                    "wireless", "nokia", "hand", "mobi",
                    "phone", "cdm", "up.b", "audio", 
                    "SIE-", "SEC-", "samsung", "HTC", 
                    "mot-", "mitsu", "sagem", "sony"
                    , "alcatel", "lg", "eric", "vx", 
                   "philips", "mmm", "xx", 
                    "panasonic", "sharp", "wap", "sch",
                    "rover", "pocket", "benq", "java", 
                    "pt", "pg", "vox", "amoi", 
                    "bird", "compal", "kg", "voda",
                    "sany", "kdd", "dbt", "sendo", 
                    "sgh", "gradi", "jb", "dddi", 
                    "moto", "iphone"
                };
                // "NEC", 
                //Loop through each item in the list created above 
                //and check if the header contains that text
                foreach (string s in mobiles)
                {
                    if (context.Request.ServerVariables["HTTP_USER_AGENT"].
                                                        ToLower().Contains(s.ToLower()))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

April 29, 2015

How to create dynamic page in DNN

Posted By: Unknown - 6:21 PM


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

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

Designed by Templateism