Saturday, January 29, 2011

Pre-Loader in Asp.Net with javascript

Preloaders give the viewer something to look at while the Javascript, Css file downloads. Whenever we used javascript files and those file in depended each other or write code on HTML markup of javascript which called the function in those files sometime gives an error or sometime give some ugly shape of loading webpage. In this article I focused to create Pre-loader step by step.
Demo shows in asp.Net, in this demo we create two aspx pages, first page I use as a pre-loader and second page actual page we want to open.click here to download demo
Create a website and then add new aspx page with name of PreLoader.aspx, below code past in markup of it.
        var iLoopCounter = 1;
        var iMaxLoop = 6;
        var iIntervalId;
 
        function BeginPageLoad() {
            // Redirect the browser to another page while keeping focus.
            location.href = "<%=Request.QueryString[“Path”] %>";
            // Update progress meter every 1/2 second.
            iIntervalId = window.setInterval("iLoopCounter=UpdateProgressMeter(iLoopCounter,iMaxLoop);", 500);
        }

BeginPageLoad function called on body load event. Actual page Url pass by Query string. Window.setInterval function start calling UpdateProgressMeter function with initial parameters
<body onload="BeginPageLoad();" >
      function UpdateProgressMeter(iCurrentLoopCounter, iMaximumLoops) {
            var progressMeter = document.getElementById("ProgressMeter")
 
            iCurrentLoopCounter += 1;
            if (iCurrentLoopCounter <= iMaximumLoops) {
                progressMeter.innerHTML += ".";
                return iCurrentLoopCounter;
            }
            else {
                progressMeter.innerHTML = "";
                return 1;
            }
        }        
 
Above function shows the updated the progress of page loads.
            function EndPageLoad() {
            window.clearInterval(iIntervalId);
 
            // Find the object that represents the progress meter.
            var progressMeter = document.getElementById("ProgressMeter")
            progressMeter.innerHTML = "Page Loaded - Now Transferring";
        }

Above method call when page loaded completely. 
<body onload="BeginPageLoad();" onunload="EndPageLoad();">
You can test the pre-loader is working fine or not simple run application like as,
http://localhost/Preloader/PreLoader.aspx?Path=http://www.Choicepk.com



Summary,
We avoid loading page ugly still or old style and don’t want to display the page before loading all javascirpt and Css files. Pre-loader help us to achieve these tasks.
 

Saturday, January 1, 2011

C# 4.0 Dynamics vs. Reflection

Hi!

Ah ha, today I already wrote two more blog now I am moving on third one, I found new feature in C# 4.0 is Dynamics its to cool. its much faster than reflection mechanism also cached objects,
Simply I show demo given below,
First I create a Console Application then create a class name is myTestingClass

Snippet
public class myTestingClass

{
public string myProperty
{
get;
set;
}
}


Now I am going to call the above class by reflection and Dynamics.

Snippet
static void Main(string[] args)

{
Stopwatch mystopWatch = new Stopwatch();
long rang = 1000000;
var myTestType = new myTestingClass();
var property = typeof(myTestingClass).GetProperty("myProperty");
mystopWatch.Start();

for (int i = 0; i < rang; i++)
{
property.SetValue(myTestType, "Hello World", null);
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " First Time Result");

dynamic myTestDynamic = new myTestingClass();
mystopWatch.Reset();
mystopWatch.Start();
for (int i = 0; i < rang; i++)
{
myTestDynamic.myProperty = "Hello World";
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " First Time Result");
// Console.ReadLine();
mystopWatch.Reset();
mystopWatch.Start();

for (int i = 0; i < rang; i++)
{
property.SetValue(myTestType, "Hello World", null);
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " Second Time Result");

myTestDynamic = new myTestingClass();
mystopWatch.Reset();
mystopWatch.Start();
for (int i = 0; i < rang; i++)
{
myTestDynamic.myProperty = "Hello World";
}
mystopWatch.Stop();
Console.WriteLine(mystopWatch.Elapsed.TotalMilliseconds + " Milliseconds" + " Second Time Result");


Console.ReadLine();
}

Code result shown in below daigram,



above example shown results, first took then second time gave the result in litle time than the first time.

Send email to gmail account in ASP.Net

Hi guys,
I am very excited to share with you how to send email to gmail account in ASP.Net, I will try to explain all attribute,Methods and setting in details.

First of all we are going to configure gmail account setting in webconfig file,


Attribute

  • deliveryMethod: Specifies the delivery method for e-mails.
  • from: Specifies the from address for e-mails
  • configsource: I will explain in last paragraph of this article.
  • port: specifies the port of the server.
deliveryMethod:
  • specifiedPickupDirectory: Configures the local directory for a Simple Mail Transport Protocol (SMTP) server.
  • network: Configures the network options for an external SMTP server.

we must set Port "587" in webconfig and also set User Id and password of gmail account which you want to send email, Host name should be "smtp.gmail.com".

Second step you need to write code of server side in any events.


Snippetyou also need to declared System.Net.Mail namespace by using keyword.
I want to share one more interesting thing in Configuration section. you can create a separate file of Smpt.config ( you can use another name of .config file). you can plug that file in web.config file by
configsource attribute in smtp tag.

RegisterClientScriptBlock Vs RegisterStartupScript

Hi Guys!

Since few month ago I very confused about RegisterClientScriptBlock and RegisterStartupScript, Where i used them and what is the difference between. These thing cleared while I was reading a book of Appress for my exams. Anyways, both Method exists in ScripManager Class in .Net Framework Ajax Extensions Library, and both have same signature and same functionality to register client script into web browser but slightly difference between these functionality. I will try to explain difference.

RegisterClientScriptBlock:

RegisterClientScriptBlock Generate script just after start Forms tag, Sever conrols and other HTML controls render after it(excepted viewstate).

Above code result shown below screenshot

RegisterStartupScript:

RegisterStartupScript generate JavaScript block after rendering all controls and just before End of Forms tag.

Above code result shown below screenshot

Summary,

Whenever we called RegisterClientScriptBlock or RegisterStartupScript both registered JavaScript into web browsers. RegisterStartupScript used when we need to called HTML tag in JavaScript and perform some action into or by that HTML tag. While we will use RegisterClientScriptBlock when no need to any HTML tag. Always remember one thing HTML and JavaScript Interpreted line by line.