Search Web

Tuesday, September 13, 2011

4 Small But Important Tips For Fresher of Asp.net.




Asp.net is a awesome framework developing web sites and web applications.with visual studio asp.net become cooler and cooler. I have wrote an article Visual Studio 2010 New Tips and Shortcut previously. now I thought to write for asp.net tips and tricks because as the way you are using the framework it’ll be hard or easier to understand and do some work with minimum codes.
Ok here we go we’ll see what are the cool features offer asp.net for .net web developers.

1. Maintain the position of scrollbar on postback : since asp.net 1.1 it was a very hard to maintain the position of scrollbar when doing postback. it’s was very necessary feature when we edit some data and after submit find position that we were editing. In asp.net 2.0 you can simply add the MantainScrollPositionOnPostBack attribute to the page directive.

<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeFile="..." Inherits="..." %>

2. Set Default Button for form when user hit the enter key : In asp.net 1.1 required to write javascript to set default key for when form submit after hit Enter key. fortunatly you can use HtmlForcontrol’s DefaultButton property to set which button should be clicked when the user hit the enter


<form id="frm" DefaultButton="btnSubmit" runat="server">

  ...

</form>


3. Set the default focus to control when page loads : as mention above also we can set default focus for page load like below




<form id="frm" DefaultFocus="txtUserName" runat="server">

  ...

</form>


4. Strongly type access to the cross-page postbacks : asp.net 2.0 introduced the concept of cross page post back where one page could post back information to another page.if you add public property in to code behind page that initiate the postback operation, you can access property in strong type manner by adding PreviouPageType directive in to target page of the post back.
<%@ PreviousPageType VirtualPath="Default.aspx" %>

by adding this directive to pageyou can access the TextBox defind in previous page in strongly type manner.

TextBox tb = PreviousPage.SearchTextBox;

Monday, September 5, 2011

Show/Hide ModalPopup Using Javascript.

The Asp.net Markup :

<asp:panel id="pnlDemo" runat="server" style="display: none;">
 <h1> This panel is a test demo</h1> </asp:panel>


 <cc1:modalpopupextender backgroundcssclass="modalBlockBackground" behaviorid="ModalPopupExtender" dynamicservicepath="" enabled="True" id="ModalPopupExtender" popupcontrolid="pnlDemo" runat="server" targetcontrolid="linkButton1"> </cc1:modalpopupextender>

If we want to use the javascript to control the popup, we can just simply to write the code like this:


  if(!show)
    {
      $find('ModalPopupExtender').hide();
    }
    else
    {
        $find("ModalPopupExtender").show();
       }


There is one thing need to be taken care which is the BehavirID, the code in the Javasctipt will look for the BehaviorID property for the ModalPopupExtender.

I hope this can be helpful for you.

Thursday, September 1, 2011

3 Things Must You Know About C# Code.



1. Null Coalescing Operator (??)

A new operator can be used which can be shortcut of ternary operator to check the null value:

Ternary operator (?:):

string empName = (value != null) ? value : string.Empty;

we can write it using null-coalescing operator (??):

string empName = value ?? string.Empty;

2. The As Cast

we might have seen the code something like:
if (customer is DeluxCustomer)
{
var dCust = (DeluxCustomer)customer
// ...
}

This is redundant because we are checking the type twice. Once in the is check and once in the cast.

We can use as operator in place. This will cast the type if the types are compatible, or return null if not:
var dCust = customer as DeluxCustomer;
// ... we can write if condition to check for null
}

we can avoid double-checking the type.

3. Auto-Properties

This is the new concept to define the properties in C#: we can define the auto-property without much code:
public class Name

{
public int name { get; set; }
public int name { get; set; }
}

Friday, August 19, 2011

How To Read Local Drive Information With Asp.net Application

Some time we need to interact with the local file system, reading structure of the Directories, reading and writing files, or performing many other jobs.

In this Application we need use System.IO Namespace for working with file system directories. System.IO Contain Driveinfo class.


Driveinfo class is used to get information such us the name,type, and status of each drive.

DriveInfo DriveDetails= new DriveInfo(@"C:\");

DriveDetails.Name : it is used to get the name of the specified drive.

DriveDetails.DriveType : it is used to get type of the specified drive.

DriveDetails.AvailableFreeSpace : it is used to get the available free space in the specified drive.

DriveDetails.TotalFreeSpace :it is used to get the total free space in the specified drive.

DriveDetails.TotalSize :it is used to get the total size in the specified drive.

DriveDetails.DriveFormat : it is used to get the format of the specified drive.

Here I am Getting C drive Information such us Drive Name, Drive Type, Free Space, Drive Format, Total Free Space, Total Size.

Here i am getting drive Size is in bytes. but i want result in Giga Bytes.

1024 Bytes = 1 KiloByte(KB)
1024 KiloBytes(KB) = 1 MegaByte(MB)
1024 MegaBytes(MB) = 1 GigaByte(GB)
1024 GigaBytes(GB) = 1 TeraByte(TB)

I am converting Bytes to Giga bytes.

double db = Convert.ToDouble(lblTotalSize.Text);
double db1 = db / 1024;
double db2 = db1 / 1024;
int db3 = Convert.ToInt32(db2) / 1024;


protected void Page_Load(object sender, EventArgs e)
    {
        DriveInfo DriveDetails= new DriveInfo(@"C:\");
        lblDriveName.Text = DriveDetails.Name;
        lblDriveType.Text = DriveDetails.DriveType.ToString();
        lblFreeSpace.Text = DriveDetails.AvailableFreeSpace.ToString();
        lblDriveFormat.Text = DriveDetails.DriveFormat.ToString();
        lblTotalFreeSpace.Text = DriveDetails.TotalFreeSpace.ToString();
        lblTotalSize.Text = DriveDetails.TotalSize.ToString();
        double db = Convert.ToDouble(lblTotalSize.Text);
        double db1 = db / 1024;
        double db2 = db1 / 1024;
        int db3 = Convert.ToInt32(db2) / 1024;
        lblTotalSizeInGb.Text = db3.ToString() + " GB";

        double TS = Convert.ToDouble(lblTotalFreeSpace.Text);
        double TS1 = TS / 1024;
        double TS2 = TS1 / 1024;
        int TS3 = Convert.ToInt32(TS2) / 1024;
        lblTotalFreeSpaceGB.Text = TS3.ToString() + " GB";

    }

Now you can understand about How to read Drive information from local System using Asp.Net Web application AND to calculate Bytes to Giga bytes in Web Application

If you find a bug and have a fix for it: please let me know and I'll be happy to update the code I am hosting here.

Any suggestions will be appreciated greatly.