Thursday, 28 February 2013

CSS Tricks

1. Avoid CSS hacks, use future proof method

We should avoid using hacks unless there are no other ways to fix it. Because, we will not able to know when those hacks will stop working. The most common way to tackle with different version of IEs is using the if else statement:

2. Use group selector
Using group selector is a good practise to minimize your coding effort and also save a few bytes out of your stylesheet. We can group the selector to avoid repeating declaring the same properties for different elements
h1, h2, h3, h4, h5, h6 {
font-family:arial;
margin:0.5em 0;
padding:0;
}

3. Center elements
It's easy to center an element, for firefox and safari, we only need to specify the width and margin left and right set to auto. However, you will need to specify text-align to center in the parent element for IE.
body {
text-align:center; /* for ie */
}

#container {
width:800px;
margin:0 auto;
text-align:left;
}

4. CSS Positioning

This is something I've just discovered it few weeks ago. Set the abolute position within a container. #item will appear inside the #container exactly 200px from the left and 50px from the top.
#container {
position: relative;
width:500; height:300;
}

#item {
position: absolute;
left: 200px;
top: 50px;
}

5. Text transform

This is something I discovered long time ago, CSS has the ability to transform text to lowercase, uppercase and capitalized the first character of a word. w3schools CSS - Text transform
h1 {
text-transform:uppercase;
}
h2 {
text-transform:capitalize;
}
p {
text-transform:lowercase;
}

6. Remove links or textbox hightlight border

When you click on textbox or link, you will able to see a border highlighting the element. It's even more obvious if you are using mac os. Luckily, you can solve it using outline property. I used it when I set the indentation of a link text to -999em and also when I'm building a textfield with rounded border.
a, input {
outline:none;
}

7. Hidden text

I think the correct way to do it is using text-indent. And also, you'd want to apply outline:none to hide the border. We use hidden text when we're using images to replace text but we want to make sure search engine can crawl the keyword.
a {
text-indent:-999em;
outline:none;

background: url(button.jpg) no-repeat 0 0;
width:100px; height:50px;
display:block;
}

8. Keep consistent with SUP and SUB

I have a chance to work on one website that uses ® and ™ massively (bad... bad experience). The problem I was facing is, the sup and sub being rendered differently in different browsers, so, I found this and it solved my problem. 
sup,
sub {
height: 0;
line-height: 1;
vertical-align: baseline;
_vertical-align: bottom;
position: relative;

}

sup {
bottom: 1ex;
}

sub {
top: .5ex;
}

9. CSS Transparency Setting for all browsers

Yes, I can never able to remember it, so I guess it's a must to list it out here for future reference.
.transparent_class {  
filter:alpha(opacity=50); /* ie */
-moz-opacity:0.5; /* old mozilla browser like netscape */
-khtml-opacity: 0.5; /* for really really old safari */
opacity: 0.5; /* css standard, currently it works in most modern browsers like firefox, */
}

10. PNG Fix for IE6

Yes, this is the best thing ever to fix ie6 annoying short coming (it doesn't work with background-position). However, if you want a better solution to could fix all the png images in your css files, you can try this IE PNG Fix from twinhelix and the new version support background position!
.png_hack{
background-image: url(../img/the_image.png) !important;
background-image: none;
filter: none !important;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/the_image.png');
}

11. Prevent line break

This is a simple css tips, but some of us might not know about it. It forces the text display in one line.
h1{
white-space:nowrap;
}

12. Force page breaks when printing your document

When you're creating a printer friendly webpages, you want to know about these property. More about printing CSS property, visit W3C CSS Print Reference and also the CSS3 stardard
h1{
page-break-before:always;
}

h2{
page-break-after:avoid;
}

h2{
page-break-inside:always;
}

13. Remove vertical textarea scollbar in IE

Remember that annoying textarea scrollbar that display by default in IE? Yes, we can remove it.
textarea{
overflow:auto;
}

14. 3D push button effect

You can create a 3D button effect easily using CSS. The most important thing is the border, set light color on top and left border, and dark color on bottom and left (shadow).
a {
display: block;
border: 1px solid;
border-color: #aaa #000 #000 #aaa;
width: 8em;
background: #fc0;
}

a:hover
{
position: relative;
top: 1px;
left: 1px;
border-color: #000 #aaa #aaa #000;
}

15. CSS Tooltips

So, you must be thinking, we will need javascript to make tooltips! :) I got this trick from Kollermedia.
a:hover {
background:#ffffff; /*BG color is a must for IE6*/
text-decoration:none;
}

a.tooltip span {
display:none;
padding:2px 3px;
margin-left:8px;
width:130px;
}

a.tooltip:hover span{
display:inline;
position:absolute;
background:#ffffff;
border:1px solid #cccccc;
color:#6c6c6c;
}
16. CSS links sequence
Apparently, the sequence of the links is very important, read more. Honestly, I don't use this much, but hey, good for future reference! :)
a:link {
color: #000;
text-decoration: underline
}
a:visited {
color: #666;
}
a:hover {
color: #333;
text-decoration: none;
}
a:active {
color: #333;
text-decoration: none;
}

17. CSS Shorthands!

Last but not least, here are the CSS shorthands that will make our life easier!
/* MARGIN */
h1 {margin:1em 0 2em 0.5em;}

h1 {margin-top:1em;
margin-right:0;
margin-bottom:2em;
margin-left:0.5em;
}

/* BORDER */
h1 {border:1px solid #000;}

h1 {border-width:1px;
border-style:solid;
border-color:#000;
}

/* BORDER WIDTH */
h1 {border-width:1px 2px 3px 4px;}

h1 {border-top-width:1px;
border-right-width:2px;
border-bottom-width:3px;
border-left-width:4px;
}

/* BACKGROUND */
div {background:#f00 url(background.gif) no-repeat fixed 0 0;}

div {background-color:#f00;
background-image:url(background.gif);
background-repeat:no-repeat;
background-attachment:fixed;
background-position:0 0;
}

/* FONT */
h1 {font:italic small-caps bold 1em/140% "Lucida Grande",sans-serif;}

h1 {font-style:italic;
font-variant:small-caps;
font-weight:bold;
font-size:1em;
line-height:140%;
font-family:"Lucida Grande",sans-serif;
}

/* LIST STYLE */
ul {list-style:square inside url(image.gif);}

ul {list-style-type:square;
list-style-position:inside;
list-style-image:url(image.gif);
}

/* OUTLINE */
h1 {outline:#f00 solid 2px;}

h1 {outline-color:#f00;
outline-style:solid;
outline-width:2px;
}

References:

Upload Multiple Images

<%@ Page Language="C#" AutoEventWireup="true" Codevirtual="add multiple images.aspx.cs"
Inherits="add_multiple_images" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
font-size: x-large;
}
.style3
{
width: 173px;
}
</style>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>

<table class="style1" style="background-color: #FFCCFF">
<tr>
<td colspan="2">

<span class="style2">Upload Multiple Images</span></td>
</tr>
<tr>
<td class="style3" rowspan="10"
style="background-image: url('Images/where r u.jpg')">
&nbsp;</td>
<td>
&nbsp;<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload2" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload3" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload4" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload5" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload6" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload7" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload8" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload9" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload10" runat="server" />
</td>
</tr>
<tr>
<td class="style3">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text="Upload Images" />
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<asp:Image ID="Image1" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image2" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image3" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image4" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image5" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
</div>
<br />
<div id="div4" runat="server" visible="false">
<asp:Image ID="Image6" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image7" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image8" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image9" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
<asp:Image ID="Image10" runat="server" Height="100" Width="100" ImageUrl="~/Image.gif" />
</div>

</tr>
</table>

</div>
</form>
</body>
</html>




.CS CODE:


protected void Button1_Click1(object sender, EventArgs e)
{
HttpFileCollection uploadFilCol = Request.Files;
for (int i = 0; i < uploadFilCol.Count; i++)
{
HttpPostedFile file = uploadFilCol[i];
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);
if (fileName != string.Empty)
{
try
{
if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" ||
fileExt ==
".jpeg" || fileExt == ".png")
{
file.SaveAs(Server.MapPath("~/Images/") + fileName);
if (i == 0)
{
Image1.ImageUrl = "~/Images/" + fileName;
}
if (i == 1)
{
Image2.ImageUrl = "~/Images/" + fileName;
}
if (i == 2)
{
Image3.ImageUrl = "~/Images/" + fileName;
}
if (i == 3)
{
Image4.ImageUrl = "~/Images/" + fileName;
}
if (i == 4)
{
Image5.ImageUrl = "~/Images/" + fileName;
}
if (i == 5)
{
Image6.ImageUrl = "~/Images/" + fileName;
}
if (i == 6)
{
Image7.ImageUrl = "~/Images/" + fileName;
}
if (i == 7)
{
Image8.ImageUrl = "~/Images/" + fileName;
}
if (i == 8)
{
Image9.ImageUrl = "~/Images/" + fileName;
}
if (i == 9)
{
Image10.ImageUrl = "~/Images/" + fileName;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}

Downloading a FIle using .Net


Write Code in .CS File:
public void Download()
{

string filename = "Socha Na Tha ";
if (filename != "")
{
string path = Server.MapPath(filename);
System.IO.FileInfo file = new System.IO.FileInfo("D://Socha Na Tha.avi");
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-disposition", "attachment;
filename="
+ filename);
Response.AddHeader("content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";//
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("this file does not exist");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Download();

}

Rotate An Image


.ASPX CODE:

<asp:Image ID="Image1" runat="server" Height="297px"
ImageUrl="~/images/image.bmp" Width="343px" />

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Rotate Image" />

.CS CODE:

protected void Button1_Click(object sender, EventArgs e)
{
// get the full path of image url
string path = Server.MapPath(Image1.ImageUrl);

// creating image from the image url
System.Drawing.Image i = System.Drawing.Image.FromFile(path);

// rotate Image 90' Degree
i.RotateFlip(RotateFlipType.Rotate90FlipXY);

// save it to its actual path
i.Save(path);

// release Image File
i.Dispose();

// Set Image Control Attribute property to new image(but its old path)
Image1.Attributes.Add("ImageUrl", path);
}

How to add Flash file in Website in ASP.NET


Add flash image in the website, makes attractive. In ASP.NET it can be add easily. For this add any Flash (.swf) file extension file to solution explorer of the website.
Add write the code inside Form tag in source page as given below-

<asp:Panel ID="Panel1" runat="server">
<param name="movie" value="Vinod.swf" />
<param name="scale" value="exactfit" />
<param name="AllowScriptAccess" value="always" />
<embed src="Vinod.swf" width="750" height="96"
scale="exactfit" allowscriptaccess="always" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" />
</asp:Panel>

RegularExpressionValidator control


It is used to validate the user input based on the validation expression.
Meta characters used to prepare the expression for single digit
\d
Accepts a single digit
/D
Accepts a single character
\w
Accepts any character other than a white space (spacebar)
\s
Accepts a space
[A-Za-z]
Accepts upper or lower case characters
[0-9]
Accepts a numerical value
^[0-9]
Accepts any characters other than numeric value
Occurrences:
It is used to specify the occurrence of the meta characters within the expression.
{number}
Accepts the input if the length of the expression is equal to the specified number
E.g.: \d{5}  à accepts 5 digits number
{Minnumber, }
Accepts the input if the length after expression is greater than or equal to the specified Minnumber.
E.g: [A-Za-z0-9_]{6, }

{Minnumber, Maxnumber}
Accepts the input if the length of the expression is between the specified range.
e.g.: \D{6,8}

Modes:
It is used to specify the occurrence of the meta characters within the expression.
Mode
MinOccurrence
MaxOccurrence
?
0
1
. or *
0
Any
+
1
Any
.ASPX CODE
    
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
 ControlToValidate="TextBox1" ErrorMessage="plese enter email ID"
ToolTip="plese Enter email id" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

Output:

Introduction of ASP.NET



ASP.NET is the next generation ASP, but it's not an upgraded version of ASP. ASP.NET is an entirely new technology for server-side scripting. ASP.NET is a part of the Microsoft .NET framework, and a powerful tool for creating dynamic and interactive web pages. ASP.NET is a server side scripting technology that enables scripts (embedded in web pages) to be executed by an Internet server
  • ASP.NET is a Microsoft Technology.
  • ASP stands for Active Server Pages
  • ASP.NET is a program that runs inside IIS.
  • IIS (Internet Information Services) is Microsoft's Internet server.
  • IIS comes as a free component with Windows servers.
  • IIS is also a part of Windows 2000 and XP Professional.

ASP.NET File

  • An ASP.NET file is just the same as an HTML file
  • An ASP.NET file can contain HTML, XML, and scripts
  • Scripts in an ASP.NET file are executed on the server
  • An ASP.NET file has the file extension ".aspx"
Working of ASP.NET
  • When a browser requests an HTML file, the server returns the file.
  • When a browser requests an ASP.NET file, IIS passes the request to the ASP.NET engine on the server.
  • The ASP.NET engine reads the file, line by line, and executes the scripts in the file.
  • Finally, the ASP.NET file is returned to the browser as plain HTML.
Difference Between ASP and ASP.NET
  1. Code behind in asp. net allows separation of business logic from UI which is not possible in asp.
  2. ASP. Net uses ADO .Net objects which are represented using XML and hence they are lightweight and can travel through firewalls. ASP uses ADO record sets which are binary COM objects heavier than ADO.Net counterparts and cannot travel through firewalls.
  3. ASP.Net supports object oriented programming. ASP is procedural.
  4. ASP.Net is compiled and hence performs better. ASP is pure scripting and hence interpreted at the time of page load.
  5. ASP.Net has caching and exception handling which is not available with ASP.
  6. ASP.NET has better language support, a large set of new controls, XML-based components, and better user   authentication.
  7. ASP.NET provides increased performance by running compiled code.
  8. ASP.NET code is not fully backward compatible with ASP
Advantage of ASP.NET
  1. ASP.NET drastically reduces the amount of code required to build large applications.
  2. With built-in Windows authentication and per-application configuration, your applications are safe and secured.
  3. It provides better performance by taking advantage of early binding, just-in-time compilation, native optimization, and caching services right out of the box.
  4. The ASP.NET framework is complemented by a rich toolbox and designer in the Visual Studio integrated development environment. WYSIWYG editing, drag-and-drop server controls, and automatic deployment are just a few of the features this powerful tool provides.
  5. Provides simplicity as ASP.NET makes it easy to perform common tasks, from simple form submission and client authentication to deployment and site configuration.
  6. The source code and HTML are together therefore ASP.NET pages are easy to maintain and write. Also the source code is executed on the server. This provides a lot of power and flexibility to the web pages.
  7. All the processes are closely monitored and managed by the ASP.NET runtime, so that if process is dead, a new process can be created in its place, which helps keep your application constantly available to handle requests.
  8. It is purely server-side technology so, ASP.NET code executes on the server before it is sent to the browser.
  9. Being language-independent, it allows you to choose the language that best applies to your application or partition your application across many languages.
  10. ASP.NET makes for easy deployment. There is no need to register components because the configuration information is built-in.
  11. The Web server continuously monitors the pages, components and applications running on it. If it notices any memory leaks, infinite loops, other illegal activities, it immediately destroys those activities and restarts itself.
  12. Easily works with ADO.NET using data-binding and page formatting features. It is an application which runs faster and counters large volumes of users without having performance problems.