Posts Tagged ‘Javascript’

Javascript Confirm on ASP LinkButton

In your C# web application, a common task is adding items to a database. But often just as important is the ability to delete items! The first method to accomplish this goal is linking an event to a LinkButton. This event links to a method that deletes. However, because a delete operation is a times permanent change, it is a good idea to force a user to confirm the decision to delete an item. This is to protect against accidental clicks.

Javascript Confirm on LinkButton Screenshot

Javascript Confirm on LinkButton Screenshot

As shown in the screenshot above, when a user clicks on a LinkButton, a Javascript confirm dialog box will appear with two buttons. Clicking OK will tell the method to continue and fire the event while clicking Cancel will prevent the event for firing. In this example, our text shows as “Are you sure?“, which is completely customizable to any text.

Via the LinkButton’s Properties Window
This is a quick and easy change to do via the LinkButton’s Property Window, so here’s how to do it!

Javascript Confirm on LinkButton

Javascript Confirm on LinkButton

  1. Goto the LinkButton’s properties window.
  2. Find the OnClientClick attribute.
  3. Add the following: return confirm(‘Are you sure?’);

Programmatically
Even though changing this via the properties window is easy, there are still those who prefer to programmatically make the change. Here’s the code below for the example in our screenshot.

<asp:LinkButton ID="lbJavascriptConfirm" runat="server"
 onclientclick="return confirm('Are you sure?');">Delete</asp:LinkButton>

And now this is how to make the change both programmatically and via a LinkButton’s property window. Please remember that this method will require that the web user has Javascript enabled. Additionally, the example uses this in a LinkButton, but any ASP Web Control that has the OnClientClick property.

Change Web Browser Title with Javascript

Every web browser has a title at the top of the page and is specified in HTML. By default, the the title tag is located in the header of the HTML. Despite the default in HTML, it is possible to change the default title name with javascript. Below is a the basic snippet of javascript code that changes the current Web Browser title to “The Web Page title has been changed“.

document.title = "The Web Page title has been changed"

In the live demo below, the above snippet is inserted into the onclick event of an html input button. Before clicking the button below, note the current browser window title.

After clicking the button above, you will notice that the web browser title has been changed to The Web Page title has now been changed. Below is the actual code on how the button was created.

<input type="button" onclick="document.title='The Web Page title has been changed'" />

Post Fancy Sample Code on Website

You may have noticed the fancing code snippets that accompany many of the tutorials on this website! I would love to say I developed this myself, but the truth is that this is a free resource free for anybody to download and use. It is called SyntaxHighlighter and can be found at http://code.google.com/p/syntaxhighlighter/.

What is Included?

So what does the Syntax Highlighter come with? Below is a list my favorite features of this software:

  1. It’s completely free!
  2. Associate keywords for supported languages are highlighted.
  3. Each line of code is numbered and colored for easier reading!
  4. Allows one click to code only view.
  5. Allows one click copy to clipboard of code.
  6. Allows one click copy to printer.

What Comes With the Download

The file can be downloaded from their official website at http://code.google.com/p/syntaxhighlighter/ as a rar compressed file. Inside the compressed file, you will find various folders and files. The core files that are required include:

  1. clipboard.swf
  2. shCore.js
  3. SyntaxHighlighter.css

Still, there are many more JavaScript files that have been included in the download. Not all of these files are required. Instead, you choose additional optional JavaScript files for the programming or scripting languages you want the Syntax Highlighter to associate.

Installation After Uploading Files

After you have uploaded the required and accompanying optional files, you will need to include the javascript and css files to the header of the pages with which you plan on using the Syntax Highlighter. See the example below:

<link type="text/css" rel="stylesheet" href="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Styles/SyntaxHighlighter.css"></link>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shCore.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushCSharp.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushJava.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushPhp.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushXml.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushJScript.js"></script>
<script language="javascript">
  window.onload = function () {
    dp.SyntaxHighlighter.ClipboardSwf = 'http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
  }
</script>

In the above, there Line 1 includes the CSS file. Next, Line 2 includes the one core JavaScript file. Lines 4 through 8 include optional JavaScript files. Lines 9 through 14 include setup for using Syntax Highlighter. The portion requires including the clipboard.swf file.

Usage

I assume you already have formatted code contained within the pre tags, as found in a previous tutorial called 3 Steps to Posting Sample Code on your Website. As of now, all you have is a formatted tag, without the fancy upgrades. All you need to add for fancy code formatting is to add name=”code” class=”html”. Exchange html with the type of file you have included. Click to see sample supported files: http://code.google.com/p/syntaxhighlighter/wiki/Languages. An example:

<pre name="code" class="html">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Victor's Programming Aid&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;This is a test&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>

Conclusion

With that, you now have fancy code formatting on your website. Comment on some examples of where you have used the Syntax Highlighter in action!