Click here to Skip to main content
15,881,938 members
Articles / Web Development / HTML

A Plugin for CKEditor With Dynamic Data

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
16 Dec 2010CPOL8 min read 47.8K   830   12  
With JavaScript and a bit of ASP.NET, it is possible to create a plugin for CKEditor that allows users to select from items drawn from a database.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Doc As XmlDocument = GenerateData()

        Response.Buffer = True
        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = "text/xml"
        Response.Write(Doc.OuterXml)
        Response.End()
    End Sub
    
    Protected Function GenerateData() As XmlDocument
        Dim Doc As New XmlDocument
        Dim Root As XmlElement = Doc.CreateElement("Personnel")
        
        Doc.AppendChild(Doc.CreateXmlDeclaration("1.0", Nothing, Nothing))
        Doc.AppendChild(Root)
        
        Dim Sql As New StringBuilder
        Dim Conn As SqlConnection = Nothing
        Dim Cmd As SqlCommand = Nothing
        Dim Adapter As SqlDataAdapter = Nothing
        Dim DS As DataSet = Nothing
        
        Dim Person As XmlElement = Nothing
        Dim Node As XmlElement = Nothing
        
        Sql.Append("SELECT EmpId, FirstName, LastName ")
        Sql.Append("FROM Employees WHERE TerminatedDate IS NULL ")
        Sql.Append("ORDER BY LastName, FirstName")
        Try
            Conn = New SqlConnection(ConnectionString)
            Cmd = New SqlCommand(Sql.ToString, Conn)
            Cmd.CommandTimeout = 300
            Adapter = New SqlDataAdapter(Cmd)
            DS = New DataSet
            Adapter.Fill(DS)
            For Each DR As DataRow In DS.Tables(0).Rows
                Person = Doc.CreateElement("Person")
                Person.SetAttribute("id", DR("EmpId").ToString)
                Person.SetAttribute("firstName", DR("FirstName").ToString)
                Person.SetAttribute("lastName", DR("LastName").ToString)
                Root.AppendChild(Person)
            Next
     
        Catch ex As Exception
            Throw ex
        Finally
            If DS IsNot Nothing Then DS.Dispose()
            If Adapter IsNot Nothing Then Adapter.Dispose()
            If Cmd IsNot Nothing Then Cmd.Dispose()
            If Conn IsNot Nothing AndAlso Conn.State <> ConnectionState.Closed Then
                Conn.Close()
                Conn.Dispose()
            End If
        End Try

        Return Doc
    End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Gregory Gadow recently graduated from Central Washington University with a B.S. that combined economics and statistical analysis, and currently works for the Washington Department of Fish & Wildlife as an IT developer. He has been writing code for 30 years in more than a dozen programming languages, including Visual Basic, VB.Net, C++, C#, ASP, HTML, XML, SQL, and R.

Comments and Discussions