Tuesday, September 3, 2013

Dynamic Page content with the help of Ckeditor, ASp.net & SQL server

Here I will be explain you, how can you create dynamic content on page with the help of Ckeditor , Asp.Net and sql server. All these stes,p I will be explain one by one, and download all CKeditor dll and project code from here.
Step 1: Design database

CREATE TABLE [dbo].[page](
      [pageid] [int] IDENTITY(1,1) NOT NULL,
      [pagecontent] [nvarchar](max) NULL,
      [page] [nvarchar](50) NULL
) ON [PRIMARY]

Storeprocedure
ALTER PROCEDURE [dbo].[usp_gallary]

@Action nvarchar(50)='',
@content nvarchar(max)='',
@page nvarchar(20)=''
AS
BEGIN if(@Action='inserpageinfo')
      begin
      insert into page(page,pagecontent)values(@page,@content)
      end
      if(@Action='getpagecontent')
      begin
      select * from page where page=@page
      end
END
Step 2 :Add a reference to the CKEditor for ASP.NET Control to your website.
In Visual Studio use the Add Reference command and browse to bin\Release\CKEditor.NET.dll file from the unpacked CKEditor for ASP.NET installation package. You can also manually copy the DLL file to the bin folder of your application.

Step 3:Copy the unpacked editor files from the CKEditor 3.x installation package and paste them into the application directory of your website.
1.    Register the CKEditor for ASP.NET control in your page:
<%@ Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
2.    Insert a CKEditor instance into the page body:
<CKEditor:CKEditorControl ID="CKEditor1" BasePath="/ckeditor/" runat="server"></CKEditor:CKEditorControl>
(where BasePath points to the directory with CKEditor)
A sample page of your project containing CKEditor might look like the one below. When you run the application in the browser, you will be able to use all features of the editor.
Step 4: Editor.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Editor.aspx.cs" Inherits="Editor" %>
<%@Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server"><table class="table">
<tr><td colspan="2" align="center"><asp:Label ID="lblmessage" runat="server"></asp:Label></td></tr>
<tr><td>Select page</td><td><asp:DropDownList ID="ddlpage" runat="server" >
    <asp:ListItem>Home</asp:ListItem>
    <asp:ListItem>About us</asp:ListItem>
    </asp:DropDownList></td></tr>
<tr><td colspan=" 2" align="center">Page Content</td></tr>
 <tr ><td colspan="2" align="center" style=" padding-bottom:20px; "><CKEditor:CKEditorControl ID="CKEditor1"  runat="server" Width=""></CKEditor:CKEditorControl></td></tr>
                    <tr><td colspan="2" align="center"><asp:Button ID="cmdupdate" Text="Update"
                            runat="server" onclick="cmdupdate_Click" /> </td></tr>
</table>
    </form>
</body>
</html>

Editor.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Data.SqlClient;

public partial class Editor : System.Web.UI.Page
{
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adpt = new SqlDataAdapter();
    Hashtable hst = new Hashtable();
    DataTable dt = new DataTable();
    DataSet ds = new DataSet();
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
     

      
    }
 


    public DataSet GetDataSet(string spName, Hashtable hst)
    {
        con = new SqlConnection();
        cmd = new SqlCommand();
        con.ConnectionString = constr;
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = spName;
        con.Open();
        if (hst != null)
            if (hst.Count > 0)
                AttachParameters(cmd, hst);
        DataSet dataset = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = cmd;
        adapter.Fill(dataset);
        con.Close();
        return dataset;
    }
    private void AttachParameters(SqlCommand command, Hashtable hstParameters)
    {
        try
        {
            IEnumerator parameters = hstParameters.Keys.GetEnumerator();
            while (parameters.MoveNext())
            {
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@" + parameters.Current;
                param.Value = hstParameters[parameters.Current];
                command.Parameters.Add(param);
            }
        }
        catch (Exception exception)
        {
            HttpContext.Current.Response.Write("Error : " + exception.Message + " </br> Source : " + exception.Source);
        }
    }
    protected void cmdupdate_Click(object sender, EventArgs e)
    {
        hst.Clear();
        ds.Clear();
        hst.Add("Action", "inserpageinfo");
        hst.Add("content", CKEditor1.Text);
        hst.Add("page", ddlpage.SelectedValue.ToString());
        ds = GetDataSet("[usp_gallary]", hst);
    }
}
Default.aspx:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="lbcon" runat="Server"></asp:Label>
</asp:Content>


Default.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adpt = new SqlDataAdapter();
    Hashtable hst = new Hashtable();
    DataTable dt = new DataTable();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        getpage();
    }
    public void getpage()
    {
        hst.Clear();
        ds.Clear();
        hst.Add("Action", "getpagecontent");
        hst.Add("page", "Home");
        ds = GetDataSet("usp_gallary", hst);
        lbcon.Text = ds.Tables[0].Rows[0]["pagecontent"].ToString();
    }
    public DataSet GetDataSet(string spName, Hashtable hst)
    {
        con = new SqlConnection();
        cmd = new SqlCommand();
        con.ConnectionString = constr;
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = spName;
        con.Open();
        if (hst != null)
            if (hst.Count > 0)
                AttachParameters(cmd, hst);
        DataSet dataset = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = cmd;
        adapter.Fill(dataset);
        con.Close();
        return dataset;
    }
    private void AttachParameters(SqlCommand command, Hashtable hstParameters)
    {
        try
        {
            IEnumerator parameters = hstParameters.Keys.GetEnumerator();
            while (parameters.MoveNext())
            {
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@" + parameters.Current;
                param.Value = hstParameters[parameters.Current];
                command.Parameters.Add(param);
            }
        }
        catch (Exception exception)
        {
            HttpContext.Current.Response.Write("Error : " + exception.Message + " </br> Source : " + exception.Source);
        }
    }
}


  project will be display like this.




No comments:

Post a Comment