Sunday, November 27, 2011

lock Statement (C# Reference)

The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. The following example includes a lock statement.
The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.
As long as the lock statement is present, the statement block is a critical section and balance will never become a negative number. See the following example.

//Account.cs
class Account
{
     decimal balance;
     private Object thisLock = new Object();

     public void Withdraw(decimal amount)
     {
          lock (thisLock)
          {
               if (amount > balance)
               {
                    throw new Exception("Insufficient funds");
               }
               balance -= amount;
          }
     }
}

Sunday, November 20, 2011

Google Static Maps API V2

The Google Static Maps API lets you embed a Google Maps image on your web page without requiring JavaScript or any dynamic page loading. The Google Static Map service creates your map based on URL parameters sent through a standard HTTP request and returns the map as an image you can display on your web page.

The following example contains the URL of a static map image of downtown New York City, which is displayed below:

http://maps.googleapis.com/maps/api/staticmap?center=Williamsburg,Brooklyn,NY&zoom=13&size=500x500&scale=1&format=jpg&maptype=roadmap&language=thai&markers=color:blue|label:S|11206&markers=icon:http://www.iblogseeker.com/favicon.ico|C11211&path=color:0x0000ff|weight:5|40.707255,-73.952193|40.708718,-73.95627|40.710475,-73.955197|40.720949,-73.956614&sensor=false

you can pass the parameters such as center, zoom, size, format, maptype, language, markers, path and sersor.

You can see details here:

http://code.google.com/apis/maps/documentation/staticmaps/index.html

How to find out latitudes and longitudes about your address.

Most people don't speak in latitudes and longitudes; they denote locations using addresses. The process of turning an address into a geographic point is known as geocoding and the Static Maps service can perform geocoding for you if you provide valid addresses. If you have doubt about how an address may geocode, you can test out the address using the follow Geocoding Utility.

http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html

Sunday, November 13, 2011

How to validate start date and end date with javascript


Up until now, We have to use Javascript  for the sake of a simpler code (light weight) and client side script for Accessible Rich Internet Applications (ARIA). Javascript is very useful and it is tedious sometime when you are a beginner at Javascript. The following sample show you how to check Start Date and End Date with ECMA script (well-known dialects such as JavaScript). When Start Date you entered is large than Today Date, it show Alert, and End Datse is also. If End Date you typed is less than Start Date, it show Alert and set the End Date' value to the same Start Date' value. Let's start here.

Check Date with Javascript
Here is the javascript to <head> tag.


<script type="text/javascript">
    function ValidateStartDate() {
        var SDate = document.getElementById('stDateTxt').value;
        var EDate = document.getElementById('enDateTxt').value;
        var stDate = new Date(SDate);
        var enDate = new Date(EDate);
        if (stDate != '' && enDate != '' && stDate > enDate) {
            alert('Start Date is greater than End Date!');
            document.getElementById('stDateTxt').value = EDate;
        }
        else if (stDate != '') {
            var today = new Date();
            if (stDate > today) {
                alert('Select Date is greater than today!');
                document.getElementById('stDateTxt').value = '';
                document.getElementById('stDateTxt').focus();
            }
        }
    }
    function ValidateEndDate() {
        var SDate = document.getElementById('stDateTxt').value;
        var EDate = document.getElementById('enDateTxt').value;
        var stDate = new Date(SDate);
        var enDate = new Date(EDate);
        if (stDate != '' && enDate != '' && enDate < stDate) {
            alert('End Date is less than Start Date!');
            document.getElementById('enDateTxt').value = SDate;
        }
        else {
            var today = new Date();
            if (enDate > today) {
                alert('Select Date is greater than today!');
                document.getElementById('enDateTxt').value = '';
                document.getElementById('enDateTxt').focus();
            }
        }
    }
</script>



Server controls are here in <form> tag.
<form id="form1" runat="server">
<table>
    <tr>
        <td align="right" style="height: 28px; width: 90px;">
            <asp:Label ID="lbDateFrom" runat="server" Text="Date From:"></asp:Label>
        </td>
        <td style="width: 290px; height: 28px;">
            <asp:TextBox ID="stDateTxt" runat="server" Width="85px" Test="" MaxLength="10"></asp:TextBox>
            MM/dd/yyyy
        </td>
    </tr>
    <tr>
        <td align="right" style="height: 28px; width: 90px;">
            <asp:Label ID="lbDateTo" runat="server" Text="Date To:"></asp:Label>
        </td>
        <td style="width: 290px; height: 28px;">
            <asp:TextBox ID="enDateTxt" runat="server" Width="85px" Text="" MaxLength="10"></asp:TextBox>
            MM/dd/yyyy
        </td>
    </tr>
</table>
</form>

Add the code in code-behind as below.
protected void Page_Load(object sender, EventArgs e)
{
      stDateTxt.Attributes.Add("onblur", "ValidateStartDate();");
      enDateTxt.Attributes.Add("onblur", "ValidateEndDate();");
}

Javascript focus() property don't work in Firefox. That's it!

Saturday, November 12, 2011

Sample Google Style Search Box (with watermark)

In .html or .aspx page,

<form id='i-search-box' target='_blank' action='http://www.iblogseeker.com/search.aspx'>

<input type='text' name='query' size='30' width='155px'

style='border: 1px solid rgb(126, 157, 185);

padding: 2px;

background: rgb(255, 255, 255) url(http://www.iblogseeker.com/images/link/iblog-search.gif)no-repeat scroll left center;

-moz-background-clip: border;

-moz-background-origin: padding;

-moz-background-inline-policy: continuous;' />

<input type='submit' name='sa' value='Search' />

<form>

<script type='text/javascript' src='http://www.iblogseeker.com/js/extSearch.js'><script>

In extSearch.js,

(function () {

var f = document.getElementById('i-search-box');

if (!f) {

f = document.getElementById('searchbox_demo');

}

if (f && f.query) {

var q = f.query;

var n = navigator;

var l = location;

if (n.platform == 'Win32') {

q.style.cssText = 'border: 1px solid #7e9db9; padding: 2px;';

}

var b = function () {

if (q.value == '') {

q.style.background = '#FFFFFF url(http://www.iblogseeker.com/images/link/iblog-search.gif) left no-repeat';

}

};

var f = function () {

q.style.background = '#ffffff';

};

q.onfocus = f;

q.onblur = b;

if (!/[&?]q=[^&]/.test(l.search)) {

b();

}

}

})();

It press “Search” button, now it redirect to url like this “http://www.iblogseeker.com/search.aspx?query=aspmemo&sa=Search”. You can change the watermark image and action url as you want. You can also use in Blog or any other page as "link us" search box.