04 August 2011

Using PHDCC CodeModule with javascript

phdcc Director John Cant writes:

My use of DNN has become -

[a] Maintain any HTML in a text file / copy and paste it into the basic editor / convert to raw / save. (Online editors are too hard to use and keep slim)

[b] Put any functionality into a phdcc.CodeModule

Much valuable functionality - jquery-ui / google maps / facebook / janrain / plupload / galleria - is available as javascript libraries. To get them onto a DNN Page, I have created a template CodeModule as follows.

ASP.NET gives you limited control over where in the page javascript can be inserted - RegisterClientScriptBlock inserts somewhere near the top; RegisterStartupScript somewhere near the bottom. All I can say is - the template works on all browsers tested.

In this example, I load the jquery-ui library at the top, and daisy chain the DNN onload handler at the bottom. The javascript could of course be located in a separate file.

In the onload handler, I create an invisible empty jquery-ui dialog and initialise a jquery-ui accordion. I bind the submit button of a PHDCC form to a handler and examine one of the form fields to give feedback via the dialog.

I add the CodeModule somewhere out of the way on the page - the bottom pane is a good place. Once there, accordions and dialogs can be used anywhere on the page.

If you need any help with this or any of the suggested libraries, please get back to us.

<%@ Control Language="C#" ClassName="JCformWidgets" %>

<script runat="server">

//--------------------------------------
private void loadJS()
{
string scriptKey = "JCjsIncludes:" + this.UniqueID;
if( Page.IsStartupScriptRegistered( scriptKey) || Page.IsPostBack) return;

ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();

// ----------------------------
// load JS files at the top of the page
// ----------------------------
string scriptBlock = "\n";

scriptBlock += "<link type='text/css' href='/jqui/jquery-ui-1.8.14.custom.css' rel='Stylesheet'>\n";
scriptBlock += "<script src='/jqui/jquery-ui-1.8.14.custom.min.js' type='text/javascript'>";
scriptBlock += "</";
scriptBlock += "script>\n"; // avoid microsoft parsing bug

cs.RegisterClientScriptBlock( cstype, scriptKey, scriptBlock);

// ----------------------------
// load onload handler etc. at the bottom of the page
// ----------------------------
string startupScript =
@"<script language='JavaScript'>
<!--

function JConload(){

var oldOnLoad = window.onload;

var myDialog = $('<div></div>').dialog( {autoOpen: false} );

this.newOnload = function(){

// call existing handler
if(typeof oldOnLoad === 'function'){
oldOnLoad();
}

// -------------------------------------

$(function() {
$( '#accordion' ).accordion();
});

// -------------------------------------

$('#' + 'dnn_ctr422_ViewForm_btnSubmit').bind('click', function() {

var theseSkills = $( '#' + 'dnn_ctr422_ViewForm_Group_157_400_Question_157_400_1563_form_157_text_1563').val();
var thisDialogText = '';

if( theseSkills.length <= 0) {
thisDialogText = 'No skills given<br>Please try again.';

myDialog.html( thisDialogText);
myDialog.dialog( 'option', 'title', 'Things you forgot ...' );

myDialog.dialog( 'open');
return false;
}
});

// -------------------------------------

};

}

(function(){
var JCOL = new JConload();
window.onload = JCOL.newOnload;
}());

// -->";

startupScript += "</sc";
startupScript += "ript>"; // avoid microsoft parsing bug

cs.RegisterStartupScript( cstype, scriptKey, startupScript);
}

//--------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
loadJS();
}

</script>


12 May 2011

Scheduled task ping for plesk in Windows

I've been moving several domains to a Windows cloud hosting package at http://www.webhosting.uk.com/. I'm currently using Cloud Beginner hosting that gives 10 domains and a dedicated IP address for £12/month. This package lets us run DotNetNuke (DNN) sites - and fast too as there is not much contention on the included SQL Server databases. The cloud is UK based so the ping time is less from the UK and nearby.

The account is run using Windows plesk which lets you control most things. However intervention is often needed from the webhosting.uk.com staff. They are usually very responsive and competent on their Live Chat service. Typical requests include setting ASP.NET modify permissions for the Network Service user and setting Application Starting Points. You need to insist that you get the dedicated IP address if you've asked for it. Email is included using the Horde web client, which isn't great; perhaps using Google Apps for email might be the best solution. No web stats package is included. Nor are file or database backups.

For one domain, one task that I needed to do was to set up scheduled tasks to ping various URLs. While this could be done externally, I decided to use the Plesk scheduled task feature. When FTPing in, the main site is at /httpdocs/. The scheduled task VBS scripts should be placed in /cgi-bin/

Using a couple of resources on the Internet, I put together this script, called pingurls.vbs:

Call DoScheduledTask()

Sub DoScheduledTask()
On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\inetpub\vhosts\mydomainname.com\cgi-bin\urls2ping.txt", ForReading)

Dim URL
Do Until objFile.AtEndOfStream
URL = objFile.ReadLine
if Len(URL)>0 then PingURL(URL)
Loop
objFile.Close

end sub

sub PingURL(URL)

Dim RequestObj
Set RequestObj = CreateObject("Microsoft.XMLHTTP")
'Open request and pass the URL
RequestObj.open "POST", URL , false
'Send Request
RequestObj.Send
'cleanup
Set RequestObj = Nothing

End Sub

This script reads a text file urls2ping.txt, also in the /cgi-bin/ directory. Each line contains a URL to be pinged.

How do you know what absolute path to use to get to the text file? This ASP.NET code, say in MyPath.aspx, will tell you the path it is running from:
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
lblPath.Text = Request.MapPath("~")
End Sub
</script>
<html>
<head>
<title>Web app path using Request.MapPath</title>
</head>
<body>
<asp:Label ID="lblPath" Runat="server" />
</body>
</html>


The final stage is to set up the scheduled task in plesk. This is straight forward using [Scheduled Tasks] then [Add New Task]. The Path to Executable file should be similar to that used above, ie E:\inetpub\vhosts\mydomainname.com\cgi-bin\pingurls.vbs. Set the times you want the task to run.