Using The Post Method In Ajax

22 09 2008

In the Rasmus’ 30 Second Ajax Tutorial, we’re using the get method to send our request:

function sndReq(action) {
     http.open('get', 'rpc.php?action='+action);
     http.onreadystatechange = handleResponse;
     http.send(null);
}

But what if we want to use the post method? Well, let’s modify that function to do just that:

function sndReq(action) {
    http.open('post', 'rpc.php', true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", action.length);
    http.setRequestHeader("Connection", "close");
    http.onreadystatechange = handleResponse;
    http.send(action);
}

That’s it, that’s all you need to modify. Of course, you’d need to modify your rpc.php page to receive requests using the post method.

Remember, this is one way to do it.

Hope this helps.



Denying Directory Listing With htaccess

24 04 2008

Just type:

Options -Indexes

That’s it!



Convert Pixels to Points to Ems

14 01 2008

I was looking for a way to convert pixels to ems for a website project I was working on, and came across this conversion table which, I’m sure, will be a useful reference to you if you ever need to convert pixels to points or ems (or percentage).

In case you don’t know, using pixel units to set font sizes is of bad practice; use ems or % units instead to display fonts on the monitor, and points for printing.

HTH



Retrieving The Value Of A Flash DataGrid Cell

10 01 2008

Well, kinda late, but I wish you all a Happy New Year!

This year, I want to blog more. And to accomplish this, I’ll be managing my time better. It won’t be easy but I’ll do it.

Okay, now on to today’s tip.

Because I’m kinda rusty in ActionScript, one of my goals is to develop more Flash applications.

Anyway, here I’ll give you a very simple example on how you can retrieve the value of a Flash DataGrid cell. I had to do something like this at my job, and I had to spend quite some time doing some research to figure this out. This tip, hopefully, will save you some time.

Ah! I’m assuming you already have some flash/actionscript experience, so I’ll keep this as short as possible. Read the rest of this entry »



Cryptographically Random Unique String

15 08 2007

Looking for a way to create a Random Password Generator, I came across the following code written by Peter Bromberg (I thought to post it here in case it saves someone some time). This code will generate a cryptographically random unique string of any length you want.

Ah! BTW, I’ve made a very small modification to the original code

using System.Security.Cryptography;
using System.Text;
 
namespace UniqueKey
{
    public class KeyGenerator
    {
 
        public string GetUniqueKey()
        {
 
            int maxSize = 8;
            char[] chars = new char[62];
 
            chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[1];
 
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
 
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length - 1)]);
            }
 
            return result.ToString();
        }
    }
}

Thanks Peter for the code!



System.Web.HttpException: The file ‘/path/to/userControl.ascx’ does not exist.

26 01 2007

If you’re working with User Controls (or other server side controls) and your file structure looks something like this:

[] = directory

[root]
     [userControls]
          ucTest.ascx
     .....
     ucContainer.aspx
     Web.config

And in your ucContainer.aspx file you have something similar to this:

< %@ Register Src="userControls/ucTest.ascx" TagName="UserControl" TagPrefix="uc1" %>
...
...
<uc1:UserControl id="ucUserControl" runat="server" />
...
...

And from your code behind, you are loading the control like this:

ucUserControl.LoadControl("userControls/ucTest.ascx");

It will compile with no problems, BUT at run time it will give you this nasty error message:

Exception Details: System.Web.HttpException: The file '/path/to/ucTest.ascx' does not exist.

This happens because when you load your control, you need to do it like this:

ucUserControl.LoadControl("~/userControls/userControl.ascx");

Also, make sure you add the tilde (~) character in your .aspx Register directive, Like so:

< %@ Register Src="~/userControls/ucTest.ascx" TagName="UserControl" TagPrefix="uc1" %>

The ~ (tilde) character is the root path reference syntax which lets you add a reference to your controls, pages, etc. without having to hard code relative paths into your URLs like so:

ucUserControl.LoadControl("../../userControls/userControl.ascx");

Using this syntax will let you move your controls to other sub directories (if you ever need to) without having to worry about going back to your ucContainer.aspx.sc file and change the ‘hard coded’ path to the UC’s new location.

Of course, if the .ascx file and its container reside in the same directory, you don’t need to include the tilde (~) character into your URL’s path.

Hope this helps :)



Parser Error Message: Could not load type ‘_Default’

20 12 2006

While developing a .Net web app in C# with Visual Studio 2005, I got this error:

Parser Error Message: Could not load type ‘_Default’

The reason: My website configuration was using the wrong .NET framework version (1.1.4322).

The solution: Change it to use a .NET 2.0 version. In my case, 2.0.50727

The result: a nicely displayed page :)

If you’re experiencing something similar, I hope this sheds some light ;)






LiveSTRONG