Tutorials in ‘WEB Programing’ Category

How to force the browser to reload CSS and JavaScript files

When editing the CSS and JavaScript of a site, there is a slight chance that the user may get the old versions, which at best will cause a lot of complains and at worst will cost you a lot of lost customers. The reason for this is that browsers cache static content like CSS and JavaScript. However, telling a browser to reload them is not that hard to do. All it takes is a little trick that will force our friendly browsers into thinking that the CSS and JavaScript are dynamic content.

Before we go on, let us take a look at how we specify the CSS and JavaScript to be used within HTML. Normally we have something in the line of:

<link rel="StyleSheet" type="text/css" href="/static/style.css">
<script src="/static/js/script.js" type="text/javascript"></script>

Where style.css and script.js are our files. Naturally CSS files are specified with the href attribute of the link tag, while JavaScript files are included using the script tag. Now, let’s take a look how we can make our browser reload CSS and JavaScript every time:

<link rel="StyleSheet" type="text/css" href="/static/style.css?version=15">
<script src="/static/js/script.js?version=15" type="text/javascript"></script>

“Now what is that “version”?”, you are probably thinking. Well it’s nothing. No really, it does absolutely nothing. You can use ?favouritesite=onlinehowto.net15 if you want. It would have the absolutely same effect.

“But what does it do?”, you are probably thinking. “This looks just like passing GET variables.” Exactly!

The browser cache uses a pretty complicated mechanism, but there is one thing that you should know for sure index.html?version=14 and index.html?version=15 are two different pages for the browsers, therefore it can’t rely on its cache, therefore it must get them again. But what happens on the server side? Well those arguments are ignored by our server and it simply supplies style.css and script.js.

Whatever language, technology, etc. you are using, you always keep a header file which is included pretty much everywhere so changing the version=<number> part after each revision is pretty easy. You can even make it a part of your site’s logic.

In conclusion, this trick may save you a lot of headaches related to cached CSS and JavaScript, and if you believe you may have some issues, we greatly encourage you to use it!

VN:F [1.9.18_1163]
Rating: 10.0/10 (2 votes cast)

Working with PHP functions

Many times we need to use code to do one thing with different input values. If put this code at each place where we need this funtionality files in our PHP project will grow up with more lines of code. On this we say code mess.

For this we have to use PHP Functions. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 3.5/10 (2 votes cast)

AJAX Control ToolKIT ConfirmButton Extender Tutorial in ASP.NET 4.0 C#

Hi were you looking for an AJAX tutorial? Specifically on the ConfirmButton Extender Control?
Are you sure about that? Get the theme here?

Well it’s all about confirming today, and we are absolutely sure you have experienced this great and very useful control before. We are also quite positive you have also experienced an event where you wished this Control was enabled on an application or website.

Ever found yourself filling out a questionnaire form or application or profile, you just typed in what seems to be the longest questionnaire of all time to only have hit the delete button by mistake or the back button on your browser by mistake? And it prompted you with Are you sure you want to leave this page? If you do everything you just wrote will be lost in the internet forever.

Well if that has happened to you then you are lucky that it was using a ConfirmButton or at least a variation of it. So in short we will show you how to use AJAX ConfirmButton Extender, how to enable it and have it show a result or warning or whatever you may want to have it say.

Also if you’re ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Now that I gave you the simple user guide explanation I will also give you a reference or more semi technical explanation.

The ConfirmButton is a simple extender that catches clicks on a button and displays a message to the user. If the “OK” button is clicked, the button or link functions normally. If not, the click is trapped and the button will not perform its default submit behavior; optionally, a client script is executed if the OnClientCancel property is set. This is useful for delete links or anything else that requires confirmation from the user.

The properties in italics are optional.


<ajaxToolkit:ConfirmButtonExtender ID ="cbe" runat ="server" TargetControlID ="LinkButton1" ConfirmText ="Are you sure you want to click this?" OnClientCancel ="CancelClick" />

• TargetControlID – The ID of the Button control to extend.
• ConfirmText – The confirmation text to display. HTML entities can be used, such as
for a newline character.
• OnClientCancel – The client script to execute when the Cancel button is clicked in the confirm dialog box.
• ConfirmOnFormSubmit – true if the confirm dialog box should not be displayed until just before the form is submitted. This is useful if the page contains ASP.NET validator controls and the confirm dialog box should be displayed only after all validation checks pass.
• DisplayModalPopupID – Specifies the ID of a ModalPopup control to use to display the confirm dialog box instead of the default window.confirm window. When you use the DisplayModalPopupID property, the following conditions must be met:
• The ModalPopup control must be configured with the same TargetControlID value as the ConfirmButton extender. (It will work properly if the ConfirmButton extender is disabled.)
• The ModalPopup control must specify the OkControlID or the CancelControlID properties in order to identify the buttons that correspond to the the OK and Cancel buttons in the confirm dialog box.
• The ModalPopup must not specify a OnOkScript or OnCancelScript property.

ConfirmButton Client Reference
Sys.Extended.UI.ConfirmButtonBehavior Class
• showing(handler) – Adds or removes an event handler for the showing event.
• Parameters – A function that represents the event handler.
• hidden(handler) – Adds or removes an event handler for the hidden event.
• Parameters – A function that represents the event handler.

• initialize() – Initializes the ConfirmButton behavior.
• dipose() – Disposes the ConfirmButton behavior.
• raiseShowing(eventArgs) – Raises the showing event.
• Parameters – A Sys.EventArgs object that represents event arguments for the showing event.
• raiseHidden(eventArgs) – Raises the hidden event.
• Parameters – A Sys.EventArgs object that represents event arguments for the hidden event.

• OnClientCancel – Gets or sets a string that contains client script that executes when the Cancel button is clicked in the confirm dialog box.
• ConfirmText – Gets or sets the confirmation text to display.
• Remarks – HTML entities can be used, such as
for a newline character.
• ConfirmOnFormSubmit – Gets or sets a Boolean value that specifies that the confirm dialog box should not be displayed until just before the form is submitted. This is useful if the page contains ASP.NET validator controls and the confirm dialog box should be displayed only after all validation checks pass.
• displayModalPopupID – Gets or sets a string that contains the ID of a ModalPopupBehavior control to use instead of the default window.confirm dialog box.
• postBackScript – Gets or sets a string that contains script to run in order to initiate a postback.

Sys.Extended.UI.ConfirmButtonHiddenEventArgs Class
• Summary – Defines arguments for the hidden event.
• Parameters – Boolean confirmed

• confirmed – Gets a Boolean value that indicates whether the user clicked OK.

Got that? Are you sure?

Did You Know?
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

As you can see we built a simple example of a confirm button
</script></head><body> <form id="form2" runat="server"> <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </cc1:ToolkitScriptManager> <asp:Label ID="Label1" runat="server" Text="Click this button to open AJAX Confirm box:" Font-Bold="true" Font-Size="16px"></asp:Label><br /><asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Button ID="btnConfirm" runat="server" Text="Confirm" OnClick="btnConfirm_Click" /> <cc1:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" TargetControlID="btnConfirm" ConfirmText="Are you sure?
You want to run the server code." OnClientCancel="onCancel" ConfirmOnFormSubmit="false"> </cc1:ConfirmButtonExtender> <asp:Label ID="lblMessage" runat="server"></asp:Label><br /> </ContentTemplate></asp:UpdatePanel> </form></body></html>

We stand behind Server Intellect and their support team. They offer dedicated servers , and they are now offering cloud hosting!

We set the logic to pop up a label message in a modal window.
protected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You clicked the <b>OK</b> button of AJAX confirm"; } Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 2.0/10 (15 votes cast)

How to Count Distinct Values in a Table In SQL Server

This tutorial will show how to create a query that retrieves a count of unique values in a column of a table. To make this query work, we will use the COUNT function and the DISTINCT statement. The COUNT function returns the number of rows that matches a specified definition and the DISTINCT statement can be used to return only distinct values. This tutorial will show how to use the COUNT function and DISTINCT statement in separate queries and then combine both in a single query to retrieve a count of unique values in a column.

Setting Up

We need to create a table called Employee with columns of EmployeeId, LastName, Country, and DepartmentId. We can create the layout of the Employee table with the CREATE TABLE statement.

Create the Employee table.
CREATE TABLE Employee( EmployeeId int null, LastName nvarchar(30) null, Country nvarchar(30) null, DepartmentId int, null)
Next we need to insert values into the table. We can do this by using the INSERT INTO statement.

Insert values into Employee table.
INSERT INTO (EmployeeId, LastName, Country, DepartmentId)SELECT 123, ”Penn”, ”Australia”, 31UNION ALLSELECT 124, ”Fitch”, ”Australia”, 33UNION ALLSELECT 145, ”Jackson”, ”Australia”, 33UNION ALLSELECT 201, ”Shields”, ”United States”, 34UNION ALLSELECT 305, ”Pierre”, ”Germany”, 34UNION ALLSELECT 306, ”Velasquez”, ”Germany”, nullUNION ALLSELECT 310, ”Lawal”, ”United States”, 31UNION ALLSELECT 311, ”Bell”, ”China”, 35UNION ALLSELECT 312, ”George”, ”Japan”, 36
We will run a quick query to see the results of the table, we simply type "SELECT * FROM Employee" for all columns and values in the table to be outputted upon execution of the query.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect”s help, we were able to avoid any headaches!
The COUNT Function

First let”s use the COUNT function to get acquainted with it. For this example we will use the COUNT function to count the number of EmployeeIds in the table. By adding EmployeeId in parenthesis next to COUNT we are telling SQL Server to call the function on EmployeeId.

Use the COUNT function to count the number of employees in the table.
SELECT COUNT(EmployeeId) AS NumberOfEmployeesFROM Employee
Execute the query and it will retrieve the number of EmployeeIds and output them in a column called NumberOfEmployees.

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The SELECT DISTINCT statement selects only different values in a table, meaning it will not list duplicates. In this example, we will retrieve and list the distinct countries in the Country column.

Select distinct countries in the Employee table.
Execute the query and it will list the five distinct countries in the table, regardless if a country appears more than once.

If you”re ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Count Distinct Values in a Table

In this example we will combine both the COUNT function and SELECT DISTINCT statement to count the number of distinct countries in the table. We simply write a query with the COUNT function as we did earlier, but this time, in the FROM clause we will write ”SELECT DISTINCT Country FROM Employee” as this lets SQL Server know to count only the distinct values in the Country column.

Count the distinct values in the Country column.
SELECT COUNT(Country) AS NumberOfCountriesFROM (SELECT DISTINCT Country FROM Employee) AS D;
Execute the query and the NumberOfCountries column will appear with the number of distinct values in it.

Thanks for reading and make sure to download the source files to get a better understanding of how the code works. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.0/10 (10 votes cast)

Introduction to JavaScript Pop Up Boxes

Boxes of joy
As annoying as they may be, JavaScript popup boxes are a good way to alert users to some relevant information, confirm a user’s decision, or collect some text input from a user. JavaScript offers three popup boxes to help us accomplish this. They are the alert box, the confirm box, and the prompt box which prompts the user to input some text. Let’s take a closer look at what these boxes can do for us.

The alert box
If you want to be absolutely sure that some information gets through to a user, you may want to try using an alert box. This is a simple (slightly annoying) but effective way of getting your message across. The syntax for an alert box is alert(“……..”); and is very easy to implement in your code. Have a look at the following code:

<html><head><title> Alert Boxes </title><script type="text/javascript">function showAlertBox(){alert(" Yay! My first alert box ") };</script></head><body><input type="button" onclick="showAlertBox();" value="Show Alert box" /></body></html>

As you can see above, we’ve created a function called ShowAlertBox(). All this function will do is make an alert box popup with the text that we specify in our alert() method. With an alert box, the user will have to click ok to proceed. Save the above code to your desktop as alertboxes.html, load it up into your browser, and click the button. You should see an alert box appear in the center of the screen. Fun, right? Let’s move on to our next oh so amazing box, the confirm box.

The Confirm Box
Similar to the alert box, the confirm box will pop up in the center of the screen once it is called. The main difference is that, unlike the alert box, the confirm box allows a user to select yes or no, and will return a value of true or false based on which option they’ve selected. Combining this with the alert box and an if statement, our next code example will popup a confirm box for the user,and then alert them to which option they’ve chosen. Here’s what the code looks like:

<html><head><title>Confirm Boxes </title><script type="text/javascript">function showConfirmBox(){var c = confirm("Would you like to continue"); if (confirm == true) { alert ("You clicked Ok"); } else { alert("Ahhh, you clicked Cancel"); }};</script></head><body><input type="button" onclick="showConfirmBox()" value="Show Confirm box" /></body></html>
As you can see, this is very similar to the alert() function, except, once again, it gives the user an option to select either OK or Cancel.

The Prompt Box

The prompt box creates a popup box that a user may enter text into. If we set the value of the prompt box to a variable, we can take the text that the user provides and use that to manipulate our program. In this example, we’ll prompt the user to enter their name. Once that’s done, we’ll take the name and write it back out to the screen with a warm and fuzzy greeting using document.write(). Let’s take a look at the code to see how it works:

<html><head><title>Prompt Boxes </title><script type="text/javascript">function getName(){ var name = prompt("What is your name"); var theDiv = document.getElementById(”greeting”); theDiv.innerHTML = "Hello, " + name + " How are you today?";};</script></head><body><div id="greeting"> </div><input type="button" onclick="getName()" value="Enter your name" /></body></html>

Running this script and pressing the “enter your name” button will result in an oh-so-awesome prompt box being presented to you, and if you enter your name, you’ll be cordially greeted by the browser. Two quick things to point out. One is that I’ve used the getElementById() method on the document. This tells JavaScript to scan the entire current document and find any element with the ID that I’ve specified. Get used to this one, you’ll use it a lot. In our case, the method returns the div element with the ID of greeting. Once JavaScript finds the right div, it takes the name from our prompt box and injects it, along with some other friendly text, into the div via the innerHTML attribute.

Final thoughts

We’ve now seen how we can alert a visitor to something important in our program, how to confirm that they’d like to perform an action, and how to accept a text input from them. These tools will come in plenty handy in the future, and are just plain fun to mess around with anyway. Now, I think it’s a good time to learn about unobtrusive javascript. This notion basically states to you should never use JavaScript to be a jerk. Repeated pop ups and alerts will only shy your users away, so be sure to implement these techniques only when needed. Join me in my next tutorial for a more thorough look at JavaScript functions, what they accomplish, and how to write your own. See you then! Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.1/10 (11 votes cast)

Using JavaScript Popup Boxes

You’ve seen them all around the Web. Little popup boxes that pop right inside your browser. If you are wondering “How do websites do that thing?”, then wonder no more. It’s all a matter of a simple JavaScript command. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 3.0/10 (1 vote cast)

Comparison and Logical Operators in JavaScript

Even the simplest program uses comparison and logical operators to implement its own logic. Logical and comparison operators in JavaScript are pretty much standard and provide all the functionality that is expected from a programming language. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)

Sending Mail Via Google SMTP Server Using PHP

We will create a simple php application that uses PHPMailer to connect to Google\”s smtp server to send out an email. You can use this for any email provider who allows SMTP access. This is useful from an e-commerce point of view, to send out emails that will not go straight into the recipients junk mail inbox. This method is preferred over sendmail which uses and SMTP relay. If you are hosting your application on a shared host, using PhpMailer will get around email limits set for sendmail by your hosting provider. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 7.0/10 (1 vote cast)

Event Calendar for ASP.NET MVC with dhtmlxScheduler

This tutorial describes the steps required to integrate a Google-like event calendar in your ASP.NET MVC application, using dhtmlxScheduler – an open source JavaScript calendar control. You will learn how to add an Ajax-based event calendar on a web page, load events from .NET sever side, and update them in the database when user makes changes in the browser. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.1/10 (11 votes cast)

Circular Buffer Algorithm: A Definitive Guide

Circular Buffers aren\”t difficult to implement as an extension of Queues and are particularly useful for processing arbitrarily large of infinite amounts of data such as data streams. If you like Algorithms, try out the B-Tree Data Structure. Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 1.0/10 (1 vote cast)