Simple WYSIWYG Content Management

3. March 2010

I had to put this small site together for someone and one of the things he wanted was a way to put up his own content for one of his pages. In a sense, this is a content management system in its simplest form. So I came up with, what I think, is a simple and effective way using XML as the data source. Here are the requirements for what I am doing:

  • WYSIWYG Editor: TinyMCE
  • Data Source: XML File
  • XMLDataSource in ASP.NET
  • Read/Write Access to App_Data or whatever folder you choose to store the XML file.


The goal was to have the user login, edit his page and have the front end page read it and show it. I took it one step further and made the XML file handle multiple pages, this way in the future if he needs a few more pages, it's already setup.

First, here is the format of the XML file

As you can see, it's a very simple format.

Second, Lets look at how some of the markup will look like for the page where the editing will take place.  This is a very simple format

Note: You will still need to add the code for the tinymce editor so it converts the textbox into the editor

You'll notice the DropDownList is using the XMLDataSource as it's DataSourceID. This is so we can keep track of which page we will be editing.  You'll see how below in the code behind.

Here is the code behind for the Submit button

 

Not much huh?  The XMLDataSource takes care of saving the document for you, so you don't have to worry about it.  What's going on here is the following:

  • We read the XML document into our own XMLDocument object
  • Find the node we want based off the item in the drop down ("page" name)
  • Set the text of that node to the text of the editor.
  • Call the Save() method from the XMLDataSource object.

 

You'll notice we don't have to do any type of encoding for the editor text.  The Save() method will automatically convert items as needed from what I have found. For example a < will turn into "&lt;"

Here is some code on how to read it back out. This is explicity specifying the page name, but you could make it dynamic as needed.  I chose to read this out into a LiteralControl.

Sample Markup for ASPX

 

Sample Code Behind for ASPX Page

 

That's it everybody.  Simple, quick and easy.  You don't have to worry about setting up any SQL database, you just need to make sure to give read/write access to the XML file you will be using.  Don't forget this can handle multiple pages as well.

Note: I tested basic data entry and a copy and paste from word and all turned out ok in the XML file. I thought I would need the <![CDATA[]]> tag but I didn't...well not from what I have ran into yet.


 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET

Assign Default Role to New User in ASP.NET

25. January 2010

Recently I was working on a project to help administer the user accounts that used the asp.net membership provider.  Why was I doing this? I did this because you can't really use the ASP.NET Configuration Tool in Visual Studio to manage a remote website. Now I'm sure there is a way to take the files that it uses and make it work on a remote server, but probably more time than what it's worth. 

Now, over my years of coding I personally have not used the asp.net membership provider (weird huh) because I either used Active Directory for internal applications, or made my own solution for managing user accounts.  I will admit, I do like the asp.net membership provider and what it has to offer and I will definitely consider it for future solutions. The one thing I did find myself doing was making custom templates for managing the accounts. I didn't mind doing this because the classes and sql side were already setup for me to pass my values into it (thanks sql membership provider!). 

I'll finally get to the point of this blog post. I was a bit dissapointed with no "default way" to add a user to a role when creating a new user. No big deal though, wasn't too hard to write the code for it.  So this is how I went about automatically adding a new user to a role.

Prerequisite: Must have the role created before trying to add the user to the role

ASPX PAGE

Code Behind

And that is that. Using the built in provider makes is super easy to accomplish this task.  Here is a great reference link for those just starting out using the ASP.NET Membership Provider:

http://www.4guysfromrolla.com/articles/120705-1.aspx

Side note: I think the only problem I had with the link above is the tool they use to manage directory access, the app would need read/write access to write the XML file to manage that.  I chose to stay away from that example because if you host it on a shared server with someone like Go Daddy, you would need to give write access to the directories you would want to secure.

Good luck.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET

Call WebMethod from Javascript in ASP.NET

6. November 2009

I needed to be able to remove a session variable from javascript so this post will talk about how to call a webmethod from javascript to remove a session item.  I will be utilizing a JS file, ASP.NET page with a WebMethod exposed in it and an asp.net user control.

The basis of this is a few things:

First we need an <asp:ScriptManager> on the ASPX page with the EnablePageMethods property set to true:

Second we need to expose a static function in our ASPX code behind so it can be available to our javascript file.

Note the [WebMethod] above the function and that the function is static. This is what allows us to make it available to the javascript file.  

Please make sure to add the statement using System.Web.Services; into the ASPX page, otherwise [WebMethod] will throw an error when compiling.

Third we need a ScriptProxyManager on the ASCX control with a reference to your JS file. This will also take care of including the JS file for your control as well so there is no need to add the <script src=....> tag. Put this as the first thing in your ASCX control.

Fourth we need the code from the JS file to bring it all together:

--------------------------------------------------------------

Anywhere in your javascript you can call the ClearSessionItem() method. Such as

function MyFunction(item){
    
    ClearSessionItem("MySessionItem");
}

Also you could call it via an onclick event with an HTML element such as:

<span onclick="ClearSessionItem('MySessionItem')">Clear Item</span>

---------------------------------------------------------------
Of course you can use this concept for almost about anything.  It allows you to not have a seperate ASMX file (Web Service) in your project.  Again, the concept here allows you to call a server side method inside javascript.


Resources:

http://www.asp.net/ajax/documentation/live/Tutorials/ExposingWebServicesToAJAXTutorial.aspx
http://www.asp.net/ajax/documentation/live/ViewSample.aspx?sref=Sys.Net.PageMethod%23PageMethod.aspx
http://www.asp.net/AJAX/Documentation/Live/ViewSample.aspx?sref=Sys.Net.PageMethod/cs/PageMethod.js


Good luck...leave questions if you got them and I'll do my best to answer  

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET, Javascript ,