RSS

Monthly Archives: April 2011

jQuery Cookies (Yummy…)

I found many people get too comfortable with server-side script that even for setting up cookies, they use server-side script. True, some visitors may turn off javascript. But, if your application required javascript, why not setting up cookies with client-side script? It gives you better performance anyway.

Of course, jQuery is the way to go here. The plugin can be downloaded here (The author is carhartl)

http://plugins.jquery.com/project/Cookie

After download, you will need to reference to it:

<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="js/jquery/plugins/jquery.cookies.js"></script>

Set A Cookie

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter");

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value in which the cookie expires in 5 days and available for the current path level.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter", { expires: 5 });

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value and available for all paths within your domain.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter", { path: '/' });

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value in which the cookie expires in 3 days and available for ‘/kitchen’ path level.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter", { expires: 3, path: '/kitchen' });

Get A Cookie

alert($.cookie("YummyCookie"));
var _myCookie = $.cookie("YummyCookie");

Delete A Cookie

$.cookie("YummyCookie", null);
Advertisements
 
Leave a comment

Posted by on April 21, 2011 in General

 

Tags: ,

Javascript Return False Doesn’t Prevent Postback?

So, I have the following javascript function:

function SaveForm()
{
    // Check the required fields
    if (!formValidated()) {
        alert('Please complete the required fields.');
        return false;
    }
}

This function is called from HTML control (or ASP.Net Server control):

<input id="btnSave" name="btnSave" type="button" value="Save" onclick="SaveForm();" />

As you can see, the javascript supposed to check if the form validated and return false when the form is invalid. However, no matter what I do, the form will always submit and do a postback.

Well, in the case where we want to prevent form from submitting using javascript, not only do we need to “return false” in our javascript function, but we also need to add “return” keyword in our call to the function. So, to fix that, here’s my modified HTML control:

<input id="btnSave" name="btnSave" type="button" value="Save" onclick="return SaveForm();" />

Notice the “return” keyword before calling “SaveForm” function.

However, the “return” keyword (both in javascript function and HTML control) is not necessary if we use javascript “confim()” function:

function DeleteItem() {
    if (confirm("Are you really sure?")) {
        DeleteItemForSure();
    }
}
<input id="btnDelete" name="btnDelete" type="button" value="Delete" onclick="DeleteItem();" />
 
1 Comment

Posted by on April 21, 2011 in General

 

Tags: , ,

Relative Path for ASP.Net

Following ASP.NET MVC Relative Path post, this is relative path that works for ASP.Net

  1. For custom user control, use the following code in .ascx page to achieve relative path:
    <script type="text/javascript" src="<%= ResolveClientUrl("~/javascripts/jquery.js") %>"></script>
    
 
Leave a comment

Posted by on April 21, 2011 in General

 

Tags: , ,

WCF Request Error: Entities Participate in the Relationship

When accessing database from WCF through Entity Framework, I got this error message:

This error is due to the primary key of the table is not specified as identity. In SQL Server, this is mandatory for primary key column.

To specify a primary as identity, right click on the table > Design > Click on primary key column. On the bottom, there is Column Properties. Scroll down to Identity Specification and expand it. The (Is Identity) should be set to Yes.

On different case, I also got “An error occurred while updating entries.”-request error. This is caused by the same problem: not specify the primary key as identity in SQL Server database.

Do remember that you do not need to refresh Entity Framework after specify the primary key as identity.

 
Leave a comment

Posted by on April 19, 2011 in General

 

Tags: , , ,

WCF Internal Server Error: Cannot Read Configuration File

The error message is clear enough to let us know that there is permission issue to the configuration file.

On the detailed error information, it says:

Config Error: Cannot read configuration file due to insufficient permissions.

This is caused by not having the read permission to Web.config file. The best workaround is to give everyone read access to the folder that contains the Web.config file.

To do this: right click on the folder > Properties > Security tab.

 
Leave a comment

Posted by on April 19, 2011 in General

 

Tags: ,

Principles of Human-Readable Code

No matter what language you are using, it’s very important, again, VERY IMPORTANT, to write a human-readable or human-friendly code. This simply means, the code will need to be readable NOT just by machine, but also by human being.

  1. Do not write for yourself, consider others.
  2. Do not assume your peer / successor will figure out how your code work.
  3. Use indentation to separate block of code.
  4. Include necessary, descriptive, plain English (no heavy-technical buzz words), and straight-forward comments.
  5. Use already-established, easy-to-understand, widely-known naming convention, and standard, and stick with it!
  6. Do not create your own design pattern, unless you are 100% scientist. There are many well-known and widely-accepted patterns out there.
  7. Do not write complicated, more-than-necessary, excessively-repetitive-loop of workflow.
Updates are coming…
 
Leave a comment

Posted by on April 19, 2011 in General

 

Tags: ,

List of Nullable Types in C#

Basically, all value types in C# can’t contain null. However, with nullable types features, these types can be nullable. These types are:

bool
byte
char
decimal
double
enum
float
int
long
sbyte
short
struct
uint
ulong
ushort

 
Leave a comment

Posted by on April 19, 2011 in General

 

Tags: ,

 
%d bloggers like this: