Sei sulla pagina 1di 8

Basic Content Management System with ASP.

NET and MS
Access
An easy to setup and use, no-frills content management system with asp.net and MS Access. If you need to setup a
small site, or quickly add some pages to an existing site, or let someone else update pages on your website. This
simple system was the quick answer. This system can be a basic framework to build up a more advanced content
management system. Or you can integrate this into your existing website to update some pages quickly from your
web browser.

I'm rather new to .NET, trying to move my skills from classic ASP. So please let me know if you find things that
could have been done better, or if something isn't working right.

This basic content management system does not use any templates, and no HTML editors. This makes the design of
your pages completely up to you. It also has a bare bones file management system to go along with this, so you can
upload your images easily as well.

Once you have all the files copied into a folder somewhere in your website structure, and assuming you have
default.aspx and a "default page" in IIS. Then when you browse to the folder you should get the page that is set as
default within the CMS (Content Management System).

Open up include.aspx, this has the basic settings used with the system. This page will then be included in each file
within the system for "global" settings.

<script runat="server" language="vb">


dim dbsource as string = Server.MapPath("acm2000.mdb")
dim user as string = "admin"
dim pass as string = "1234"
dim uploadpath as string = server.MapPath("images\") '"
</script>
 set the path to your database.
 set the username for the system.
 set the password for the system.
 set the upload path for uploading files.

Note: your database and upload path should have read/write/delete permissions

The database is best located outside of your webroot for best security. There is no usernames or passwords stored
in the database, so this is not critical, but generally a good idea.

Sessions are used for security, so be sure that sessions are enabled on your web server for this app to work. The
following code is used to check for this session on the administration pages.

if session("loggedin") = "" then


response.redirect("login.aspx")
end if

The code below will open up the database, look for a record matching the id of the querystring variable passed to it,
if no querystring value is passed, then it opens up the record that is marked as "home page". This enables you to
have a "default" page, that will be displayed.

<%@ Page Language="vb" Debug=true %>


<%@ import Namespace="System.Data.OLEDB" %>
<!-- #INCLUDE FILE="include.aspx" -->
<script language=vb runat="server">
dim mydata as string

Sub Page_Load(sender As Object, e As EventArgs)


Dim queryvalue = Request.QueryString("id")
If (IsNumeric(queryvalue)) then
Dim query As String = "Select pagedata FROM tblpages
WHERE id = " & queryvalue & ";"
Dim myConn As New OleDbConnection
("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbsource & "")
Dim myCmd As OleDbCommand = New OleDbCommand(query, myConn)
myConn.Open()
Dim myReader As OleDbDataReader = myCmd.ExecuteReader()

While myReader.Read()
mydata = (myReader("pagedata").tostring)
End While

'close connections'
myReader.Close()
myConn.Close()
MyConn = Nothing
Else
Dim query As String = _
"Select pagedata FROM tblpages WHERE homepage = 1;"
Dim myConn As New OleDbConnection _
("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbsource & "")
Dim myCmd As OleDbCommand = New OleDbCommand(query, myConn)
myConn.Open()
Dim myReader As OleDbDataReader = myCmd.ExecuteReader()

While myReader.Read()
mydata = (myReader("pagedata").tostring)
End While

'close connections'
myReader.Close()
myConn.Close()
MyConn = Nothing
End if

End Sub
</script>
<%=mydata%>

A simple page list makes it easy to add or delete "pages" from your site. All pages are stored in the MS Access
database.
This code below basically loops through the database then uses repeater to display the data on the page. This page
also has links to create a new page, which will do a simple "insert" into the database. This list also lets you set which
page will be the "default" page as described earlier.

<script runat="server" language="vb">


Sub btnHelp_OnClick(Src As Object, E As EventArgs)
lblhelp.visible = true
End Sub

Sub Page_Load(sender As Object, e As EventArgs)


lblhelp.Visible = False
if session("loggedin") = "" then
response.redirect("login.aspx")
end if

dim homepage as integer = request.querystring("homepage")


dim homepageid as integer = request.querystring("id")
if homepage = 1 then
'set all to 0 '
Dim strSQL As String = "UPDATE tblPages SET homepage = 0;"
Dim iConn As New OleDbConnection(_
"PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbsource)
Dim iCmd As New OleDbCommand(strSQL, iconn)
iConn.Open()
iCmd.ExecuteReader()
iConn.Close()
'set homepage '
Dim strSQL2 As String = _
"UPDATE tblPages SET homepage = 1 where ID = " & homepageid & ";"
Dim iConn2 As _
New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" &_
dbsource)
Dim iCmd2 As New OleDbCommand(strSQL2, iconn2)
iConn2.Open()
iCmd2.ExecuteReader()
iConn2.Close()
end if
dim newpage as integer = request.querystring("newpage")
if newpage = 1 then
Dim strSQL As String = "INSERT INTO tblPages(pagename, pagedata, _
homepage)
VALUES('*new page description*', '<html>" & vbcrlf & "<head>" & vbcrlf &_
"<title>Untitled</title>" & vbcrlf & vbcrlf & "</head>" & vbcrlf &_
"<body>" & vbcrlf & vbcrlf & vbcrlf & "</body>" & vbcrlf & "</html>',
0);"
Dim iConn As New OleDbConnection( _
"PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbsource)
Dim iCmd As New OleDbCommand(strSQL, iconn)
iConn.Open()
iCmd.ExecuteReader()
iConn.Close()
end if

dim deleteid as integer = request.querystring("deleteid")


if deleteid > 0 then
Dim queryvalue = Request.QueryString("deleteid")
Dim strSQL As String = "DELETE FROM tblPages WHERE ID = " &_
queryvalue & ";"
Dim dconn As New OleDbConnection(_
"PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbsource)
Dim iCmd As New OleDbCommand(strSQL, dconn)
dconn.Open()
iCmd.ExecuteReader()
dconn.Close()
end if

Dim query As String = _


"Select pagename,id,homepage FROM tblpages order by pagename;"
Dim myConn As New OleDbConnection(_
"PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbsource & "")
Dim Cmd as New OLEDBCommand(query,myConn)
MyConn.Open()
dim dbread
dbread=Cmd.ExecuteReader()
tblpages.DataSource=dbread
tblpages.DataBind()
dbread.Close()
myconn.Close()
dim numberofresults as string = tblpages.Items.Count
lblrcount.text = numberofresults & " pages found"
dim idvalue as string = id

End Sub

</script>
<h3 class="myfont" title="visit www.basic-cms.com for updates">Basic CMS -
Page Management</h3>
<p><a href="list.aspx?newpage=1" title="click here to create a new page">
Create New Page</a></p>
<asp:Repeater id="tblpages" runat="server">
<HeaderTemplate>
<table border="1" cellspacing="0" cellpadding="4" bordercolor="#000000"
class="myfont">
<tr bgcolor="#b0c4de">
<th>ID</th>
<th>Page Description</th>
<th>Default</th>
<th>URL</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</HeaderTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#FFFFFF">
<td><%#Container.DataItem("id")%> </td>
<td><%#Container.DataItem("pagename")%> </td>
<td><a href="list.aspx?id=<%#Container.DataItem("id")%>&homepage=1"
title="Click to set as home page">
<%#Container.DataItem("homepage")%></a> </td>
<td><a href="default.aspx?id=<%#Container.DataItem("id")%>
" title="default.aspx?id=<%#Container.DataItem("id")%>"
target="_blank">default.aspx?id=<%#Container.DataItem("id")%></td>
<td><a href="edit.aspx?id=<%#Container.DataItem("id")%>"
title="Click to Edit">Edit</a> </td>
<td><a href="list.aspx?deleteid=<%#Container.DataItem("id")%>"
title="Delete!">Delete</a></td>
</tr>
</AlternatingItemTemplate>

<ItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#Container.DataItem("id")%> </td>
<td><%#Container.DataItem("pagename")%> </td>
<td><a href="list.aspx?id=<%#Container.DataItem("id")%>&homepage=1"
title="Click to set as home page">
<%#Container.DataItem("homepage")%></a> </td>
<td><a href="default.aspx?id=<%#Container.DataItem("id")%>"
title="default.aspx?id=<%#Container.DataItem("id")%>" target="_blank">
default.aspx?id=<%#Container.DataItem("id")%></td>
<td><a href="edit.aspx?id=<%#Container.DataItem("id")%>"
title="Click to Edit">Edit</a> </td>
<td><a href="list.aspx?deleteid=<%#Container.DataItem("id")%>"
title="Delete!"
onclick="return confirm('Are you sure you want to delete?')">Delete</a></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Label Id="lblrcount" RunAt="server" CssClass="myfont"
ForeColor="#000000" font-size="12" />

<form runat="server">
<asp:Button Id="btnShowHelp" RunAt="server"
Text="Help" OnClick="btnHelp_OnClick" class="myfont"
title="click for basic help" />
</form>

<asp:Label Id="lblhelp" RunAt="server" class="myfont"


Text="<p><strong>Page Name:</strong>
The page description</p><p><strong>Default:</strong> 1=default, or
start page. Click to set.</p>
<p><strong>URL:</strong> To link to
this page, use this location.</p><p><strong>Edit:</strong> Click Edit to
edit the page.</p>
<p><strong>Delete:</strong> Click Delete to delete the page.</p>" />

The simple file manager used all .NET code, no 3rd parth components are required. This file manager will allow you
to upload files and delete files, as well as view what files are in the upload directory.
<%@ Page Language="vb" Debug=true ValidateRequest=false %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- #INCLUDE FILE="include.aspx" -->
<!-- #INCLUDE FILE="menu.aspx" -->
<html>
<head>
<title>File Manager</title>
<script runat="server" language="vb">
Sub Page_Load(sender As Object, e As EventArgs)
if session("loggedin") = "" then
response.redirect("login.aspx")
end if
show_files()
End Sub

Sub Show_files
Dim file As String
Me.ListBox1.Items.Clear()
Dim files() As String = Directory.GetFiles(uploadpath)
For Each file In files
Me.ListBox1.Items.Add(FileNameWithoutExtension(file))
Next
End Sub

sub updatelist_Click(Sender as Object, e as EventArgs)


show_files()
end sub

sub del_Click(Sender as Object, e as EventArgs)


uploaddetails.visible = false
dim deletefile as string = request.form("listbox1")
if deletefile <> "" then
deletefile = uploadpath & deletefile
File.Delete(deletefile)
else
nofile.visible = true
nofile.InnerHtml = "No file was selected to delete."
Span1.visible = false
end if
show_files
end sub

Sub Upload_Click(Sender as Object, e as EventArgs)

FileName.InnerHtml = MyFile.PostedFile.FileName
FileContent.InnerHtml = MyFile.PostedFile.ContentType
FileSize.InnerHtml = MyFile.PostedFile.ContentLength
UploadDetails.visible = True

Dim strFileName as string = MyFile.PostedFile.FileName


Dim c as string = System.IO.Path.GetFileName(strFileName)
c = REPLACE(c," ","_")

if strFileName <> "" then


MyFile.PostedFile.SaveAs(uploadpath + c)
else
nofile.visible = false
Span1.visible = true
Span1.InnerHtml = "No file was selected to upload."
end if
show_files()
End Sub

Public Function FileNameWithoutExtension(ByVal FullPath As String) As String


Return System.IO.Path.GetFileName(FullPath)
End Function
</script>
<head>
<body style="font-family: Arial, Helvetica, sans-serif;">
<h3>Basic CMS - File Management</h3>
<hr width="50%" style="text-align: left;">
<Form Method="Post" EncType="Multipart/Form-Data" RunAt="Server">
<h3>File Upload </h3>
<Input ID="MyFile" Type="File" RunAt="Server" Size="40">
<p>Click browse, choose file, then click upload</p>
<Input Type="Submit" Value="Upload" OnServerclick="Upload_Click"
RunAt="Server">
<P><Div ID="UploadDetails" Visible="False" RunAt="Server">
File Name: <Span ID="FileName" RunAt="Server"/>
<BR>File Content: <Span ID="FileContent" RunAt="Server"/>
<BR>File Size: <Span ID="FileSize" RunAt="Server"/> bytes
<BR></Div> <Span ID="Span1" Style="Color:Red" RunAt="Server"/>
</p>
<hr width="50%" style="text-align: left;">
<h3>File Listing</h3>
<p><asp:ListBox Id="listbox1" RunAt="server" Width="250" Rows="5" /></p>

<p>Select file and click delete to remove</p>


<input type="submit" name="del" value="Delete" OnServerclick="del_Click"
RunAt="Server">
<input type="submit" name="refresh" value="refresh list"
OnServerclick="updatelist_Click" RunAt="Server">
</Form>
<Span ID="nofile" Style="Color:Red" RunAt="Server"/>
<hr width="50%" style="text-align: left;">

<p><a href="upload.aspx">Refresh Page</a></p>

For the complete download of ALL code and files visit this site

This system is VERY basic yes, but sometimes simple and basic is better.

I'm rather new to .NET, trying to move my skills from classic ASP. So please let me know if you find things that
could have been done better, or if something isn't working right.

Hope you enjoy it!

Potrebbero piacerti anche