Archive

Archive for October, 2008

Hover effect on input type=image element

October 30, 2008 Imran Akram Leave a comment

I was thinking about putting a hover effect on ASP .NET’s Imagebutton Control and well, first thing I did is that I found out what it gets rendered into. So I found out that it gets rendered into an input type=image Html tag. I used a technique of putting a hover effect with sprite images. It’s just that you put an image that actually has two images for the two states of the button, one for the hover mouse state and one for the normal state. And through CSS you just change the value of margin-top value of the div that contains that input tag and it gives the effect of a hovering image. Below is the CSS, the sample image and the HTML code for you.

#backbutton
{ cursor:pointer;
height:34px;

_height:34px; /*for IE 7 only*/

overflow:hidden;
width:99px;
}

#backbutton:hover input
{
margin-top:-33px;
}
#nextbutton
{
cursor:pointer;
height:33px;
overflow:hidden;
width:99;
}

#nextbutton:hover input
{
margin-top:-33px;
}
#nextbutton:hover img
{
margin-top:-33px;
}


<table cellspacing=”5″>
<tr>
<td>
<div id=”backbutton”>
<input type=”image” src=”images/backonoff.jpg” />
</div>
</td>
<td>
<div id=”nextbutton”>
<%–<input type=”image” src=”images/nextonoff.jpg” />–%>
<img src=”images/nextonoff.jpg” id=”Button2″ alt=”Next” onclick=”SaveFinalCard();” />
</div>
</td>
</tr>
</table>


http://blog.josh420.com/archives/2008/07/using-sprite-images-with-input-typeimage-for-hover-effect.aspx

A lost bet — Response.BinaryWrite Spoils the day!

October 28, 2008 Imran Akram 1 comment

Two days ago I lost a bet. My friend Jubair was generating a bar code image and he said how can I get it to display on the page without saving it as a file and giving its source to the image tag. I said you can’t do it because I thought it would be possible in desktop applications only. So we made a deal that if I lose I will write a blog entry for him. So here it is. Anyways, he did it in a manner similar to this one.

dim omemorystream as new memorystream()
img1.save(omemorystream,imagetype)
Response.binarywrite(omemorystream.Buffer)

how to convert an integer to string and left pad it with zero’s?

October 28, 2008 Imran Akram 2 comments

Here’s a quick and simple way of converting an integer to string (varchar) and left pad it with leading zeros like 00012. This example will get the result in 5 digits

select right(‘00000′ + cast(yourfield as varchar(5)), 5)

How to display an icon of your webpage in the address bar, tabs and in favorites

October 22, 2008 Imran Akram Leave a comment

Method 1

This is the easiest method to implement and it will work regardless of the particular page on your site users choose to add to their favorites list. Don’t worry if you don’t have access to your web site root; take a look at the next method.

If you have access to the root of your web site, simply save your icon file as “favicon.ico” there. For example, if your web site is “www.imak47.wordpress.com”, your icon file should be available at “www.imak47.wordpress.com/favicon.ico”. The web browser will look for favicon.ico whenever your site is added to the favorites list and if it is found at the root of your web site, the icon will appear next to the link to your site.

Method 2

If you don’t have access to the root of your web site, you have to add the following tag to your web page so that the browser will know where to look for your icon. Unlike before, this time you can save the icon under any name ending with “.ico” We’ll use the name “logo.ico” and assume that your web site is under the directory “~your_directory”.

<LINK REL=”SHORTCUT ICON”
HREF=”/~your_directory/logo.ico”>

Above tag should be inserted in-between the <HEAD> and </HEAD> tags.

By the way, you can specify multiple logos for multiple pages using the second method. Simply save your icons using unique names, such as logo1.ico, logo2.ico, logo3.ico for example, and replace “logo.ico” in the above HTML code with the name of the icon you want to use for any particular page.

How to make a case sensitive comparision of strings in SQL Server

October 21, 2008 Imran Akram 2 comments

String in SQL Server are by default case insensitive. That means that “SQL” is equivalent to “sql” and it causes problems when you’re making a password authentication mechanism which is supposed to be case senstive. Here’s an example stored procedure in which I’m making a case sensitive string comparison for the password field using Collations.

CREATE PROCEDURE [dbo].[proc_AuthenticateUser]
(
@UserName varchar(50),
@Password varchar(50)
)
AS
BEGIN

SET NOCOUNT ON
DECLARE @Err int

SELECT     dbo.[User].Id
FROM         dbo.[User]
WHERE     (dbo.[User].Password = @Password  COLLATE SQL_Latin1_General_CP1_CS_AS) AND (dbo.[User].Username = @UserName)
SET @Err = @@Error

RETURN @Err
END

jQuery effects not working after UpdatePanel Asynchronous request is over

October 18, 2008 Imran Akram 23 comments

A few days ago I was working on a project that involves a really good looking design and it had some fancy effects on a page with the help of jQuery library

The fancy effect was about showing a button over the picture with the title “View larger” when the mouse hovered over it and it went off and on with the fade effect and some good looking collapsible panels. The items were shown on the basis of a filtering criteria given by the user and I put that section in an UpdatePanel control. On first page load every thing was working A-OK but as soon as there was any Asynchronous Post back, whether from a drop down list or from any check box, all the jQuery effects just vanished.

So the problem was quite vague when I first looked at it, it wasn’t clear as to why the application is behaving this way so I started testing every peace of javascript I could find and mind you I didn’t know any thing about jQuery by that time so when I came up to this I started googling it too, studied how it worked.

Then I came to know that there’s an event called the Ready event, which is fired once to DOM is fully loaded. and I noticed in the code that there were lots of things that were getting initialized in this event which was found in three different files in my case. So I took out the code in the “ready” events and put them in 3 separate JavaScript functions, called them from the ready event AND then did the part that ACTUALLY solved the issue. WHAT WAS THE ISSUE?? Well, the issue was that after an async post back the ready event isn’t fired. So I had to find a way to actually call a JavaScript function after ANY Async post back. After a little Googling I came up to this fine link

http://zeemalik.wordpress.com/2007/11/27/how-to-call-client-side-javascript-function-after-an-updatepanel-asychronous-ajax-request-is-over/

where Zeeshan Malik explains how to do that. So I did it like this:

I wrote this function in the master page

function load(){
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

function EndRequestHandler(){

ReadyFunction1();

ReadyFunction2();

ReadyFunction3();

}

and called this in the onload event the body tag  as

<body onload=”load()”>

And the problem got knocked out!

For your convenience, here are some useful links.

http://www.sitepoint.com/article/ajax-jquery/

http://docs.jquery.com/Tutorials:AJAX_and_Events

Happy Programming Folks!

Shared joy is a double joy; shared sorrow is half sorrow

October 17, 2008 Imran Akram 2 comments

Read this from someone who’s coming back from a beating. Just gotta say: it’s absolutely beautiful. I just love watching people who never quit and never say die. I hope and I pray that she’ll find her way out of her misery; as a matter of fact I’m quite sure she will God willingly

Shared joy is a double joy; shared sorrow is half sorrow.
–Swedish proverb

A very rigid Regular expression for US Phone number format

October 17, 2008 Imran Akram Leave a comment

Used this regular expression for a very rigid sort of a criteria for US Phone number format

\((?\d{3})\)\s*(?\d{3}(?:-|\s*)\d{4})

How to get and cancel the enter/return key in Javascript?

October 8, 2008 Imran Akram 1 comment

There was a problem I was facing with a web form I made, and that was that whenever the user pressed the enter key while typing in a text box, he was taken to the previous page. Obviously it was very irritating so I thought about making up a javascript routine to eat away the Enter key. At first I made up a function using window.event and that was working perfectly fine until I discovered that it wasn’t working in Firefox 3. So I starting googling things out and came up with this function. This baby works fine in both browsers.

function eatenter(e) {
if (window.event) { // IE
if (e.keyCode == 13) {
//override default value so you know it was caused by input

return false;
}
}
else if(e.which == 13) { // Netscape/Firefox/Opera

return false;
}
}

Since this was an ASP .NET application and I was working with asp:Textbox control so I added this function to the textbox using this statement.

txtBox.Attributes.Add(“onkeydown”, “return eatenter(event);”);

This would look something like that in HTML

<input type=”text” onkeydown=”return eatenter(event);” />

Hope that helps,

Happy Programming Folks!

How to get the URL on which the application is hosted

October 7, 2008 Imran Akram Leave a comment

string part_path = @”http://” + Request.Url.Authority + “/” + Request.ApplicationPath + @”/print/”;

This gave me the path such as http://localhost/myVirtualDirectory/Print/

when I deployed the application it also worked by giving the appropriate website name instead of localhost.