Monday, December 27, 2010

Looking for SMTP service in IIS 7


I was looking to install SMTP service and could not find under the "web server" role.   Here are the instructions in the help file.  Hope this helps.
How to Add Features to Your ServerIn Windows Server "Longhorn" Beta 2, you can add most available features to your server by using the Add Features Wizard.
Adding Features to Your Server by Using the Add Features Wizard
Adding Features to Your Server by Using the Add Features Wizard
You can add the following features by using the Add Features Wizard.
  • Windows Activation Service (WAS)
  • Background Intelligent Transfer Service (BITS) Server Extensions
  • Desktop Experience
  • Windows Clustering
  • Windows Server Backup
  • Line Print Remote Port Monitor
  • Microsoft Message Queuing (MSMQ) Services
  • Windows Network Load Balancing
  • Remote Access Service (RAS) Client
  • Remote Assistance
  • Remote Procedure Calls over HTTP Proxy
  • Simple Mail Transfer Protocol (SMTP) Server
  • Storage Manager for Storage Area Networks
  • Simple TCP/IP Services
  • Subsystem for UNIX-based Applications
  • Telnet Client
  • Telnet Server
  • Windows Internal Database
  • Windows Internet Name Service (WINS)
  • Windows System Resource Manager (WSRM)
  • Windows Foundation Components for WinFX
  • Wireless Networking
  • SQL Server 2005 Embedded Edition (Windows)
  • iSNS Server
  • Windows Search Service
  • Indexing Service
  • Bitlocker Drive Encryption
Open the Add Features Wizard in one of the following two ways.
To start the Add Features Wizard
In the Features Summary area of the Server Manager main window, click Add Features.
-- or --
In the Customize this server area of the Initial Configuration Tasks window, click Add Features.
Note
The Initial Configuration Tasks window opens by default when a member of the Administrators group logs on to the computer.
Server Manager opens when the Initial Configuration Tasks window is closed. You can also open Server Manager by using shortcuts on the Start menu or in Administrative Tools.

Introduction To JQuery For ASP.NET Developers


If you are keeping yourself updated with the latest in the .NET sphere, you are probably aware that Microsoft has provided an inbuilt support for jQuery in Visual Studio 2010. Though it was possible to use jQuery with ASP.NET even before VS 2010, formally including jQuery as a part of website created using VS2010 means that more and more developers are going to learn and use it. If you haven't tried jQuery yet this article series will teach you everything needed to master jQuery and use it in ASP.NET applications.

What is jQuery?

The official website for jQuery defines jQuery as follows:
"jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript."
Let's try to understand this description of jQuery in a bit detail.

jQuery is a JavaScript library
As a ASP.NET developer you must have used JavaScript in one way or the other. The standard JavaScript no doubt helps you code rich and responsive web pages but the amount of code that you need to write is often too much. For example, if you wish to write a fancy popup menu from ground up using JavaScript it might be a time consuming task. To simplify such development and make you more productive several JavaScript libraries are available and jQuery is one of them. There are others such as Mootools, Prototype and Dojo. The fact that Microsoft is supporting jQuery in its products and investing resources into it clearly indicates its popularity. As you would expect jQuery is cross-browser and supports all leading browsers including IE 6+, FF 2+, Chrome, Safari 3+ and Opera 9+.

jQuery is fast and concise
jQuery is highly optimized library. Moreover it is compact. The production version of jQuery 1.4.3 is just 26 KB and development version 179 KB. This compactness means less data to be downloaded at client side without compromising stunning UI effects.

Scope of jQuery
jQuery simplifies HTML DOM navigation considerably. For example, document.getElementById("Text1") becomes just $("#Text1"). Simple. Isn't it? Most of the JavaScript functionality goes in client side event handlers. jQuery is handy when it comes to event handling. If you are thinking about your AJAX functionality don't worry. jQuery allows you to make AJAX calls to ASP.NET Web Services, WCF services or even page methods. If needed jQuery can be used along with ASP.NET AJAX.

jQuery is designed to change the way that you write JavaScript
That's true! jQuery dramatically changes the way you write JavaScript code. Initially you may find syntax of jQuery bit odd but once you get hang of it you will probably never look at any other library (or at least to the traditional way of writing JavaScript). For example, a common JavaScript file contains too many small to huge functions and you keep calling them individually whenever required. With jQuery the "chain" of operations makes your code compact and handy. 
Ok. Enough of introduction. Now, let's complete the "hello world" ritual :-) In next section you will build a simple ASP.NET webform with some server controls on it that perform a trivial job of displaying "Hello world".

Download jQuery

Before you start any development with jQuery you need to download its latest version. You can do so by visiting http://jquery.com and then downloading "Development" version. If you are using VS2010 then you need not download anything because when you create a new website by default jQuery library is already added for you (see screenshot below)
In the above screenshot jquery-1.4.1.js is the development version, jquery-1.4.1.min.js is minified production version and jquery-1.4.1-vsdoc.js is the VS2010 IntelliSense file that enables IntelliSense for jQuery (see below).
In the remainder of this article I will be using VS2008. If you are using VS2010 then just cleanup the default website by removing master page and other unwanted stuff.

Design a simple web form

Create a new website in VS2008 and create a new folder named Scripts. Copy the downloaded jQuery file in the Scripts folder. Though you can place it anywhere inside your website it is recommended to keep all JavaScript files under one folder typically named Scripts. The default name for the jQuery file is jquery-1.4.3.js but you can change it to something else if you so wish.
Now open the default web form and add a <script> tag in its <head> section as shown below :
<script src="Scripts/jquery-1.4.3.js" type="text/javascript"></script>
Now place a TextBox and a Button web control on the web form. Switch back to HTML source view and add another <script> block as shown below:
<script type="text/javascript">

$(document).ready(OnPageReady);

function OnPageReady() 
{
  $("#Button1").click(OnButtonClick)
}

function OnButtonClick(event) 
{
  alert("Hello world from jQuery!");
  event.preventDefault();
}
</script>
The first line is where jQuery magic starts. The $ sign is a shortcut to base object jQuery. So, $(...) is actually same as jQuery(...). If you ever coded in ASP.NET AJAX this concept should be familiar to you. The ready() is an event that fires when the wen page under consideration is fully loaded and its various elements are ready to be accessed. The event handler for ready event is supplied in the parenthesis as OnPageReady. OnPageReady() is a normal JavaScript function that wires an event handler for the client side click event of the button. It does so again by using $ shortcut. This time, however, ID of the button control is specified prefixing it with #. The click is an event and you specify its handler as OnButtonClick. The event handler receives an event object giving more information about the event. The OnButtonClick() is another function that simply displays "Hello World from jQuery!" using JavaScript alert. The OnButtonClick function also calls event.preventDefault() method so as to prevent web form postback that normally would have happened due to Button web server control.
Ok. If you run the web form you should see something like this :
Easy! Isn't it?
Now let's modify the above code as shown below:
$(document).ready(
 function() {
   $("#Button1").click(function(event) {
    alert("Hello world from jQuery!");
    event.preventDefault();
   }
  )
 }
)
This is a compact version of the code that achieves the same functionality. Here, instead of defining separate functions you have written all the code there itself. You may compare this code with anonymous methods of C#.

Adding something more...

Now that the "Hello world" ritual is over let's add some extra features to our code. Begin by defining the following CSS class in your web form:
<style type="text/css">
        .NoFocus
        {
            background-color:White;
            border:solid 1px gray;
        }

        .Focus
        {
            background-color:silver;
            border:solid 3px gray;
        }

    </style>
The CSS class NoFocus will be applied to the textbox when it doesn't have focus whereas CSS class Focus will be applied when the textbox receives the focus. To accomplish this change the preceding code (compact version) as shown below:
$(document).ready
        (
            function() 
            {
                $("#Button1").click(
                function(event) 
                {
                    alert($("#TextBox1").val());
                    return false;
                }
                )
                
                ,
                $("#TextBox1").addClass("NoFocus")
                ,
                $("#TextBox1").focus(
                function(event)
                {
                   $("#TextBox1").removeClass("NoFocus");
                   $("#TextBox1").addClass("Focus");
                }
                ),
                
                $("#TextBox1").blur(
                function(event)
                {
                   $("#TextBox1").removeClass("Focus");
                   $("#TextBox1").addClass("NoFocus");
                }
                )
            }
        )
Notice the lines marked in bold letters. The click event handler of the button now displays whatever has been entered in the textbox. To retrieve textbox value you use val() method. Initially the textbox won't have focus and its CSS class should be NoFocus. This is done using addClass() method. The focus() and blur() event handlers simply add and remove the appropriate classes using addClass() and removeClass() methods respectively.
The following figure shows a sample run of the web page with the modified code. 
That's all for this part. In the next part you will learn about jQuery Selectors and how to use their power while selecting certain elements from a web page.http://jquery.com

ASP.net Communication Between Parent And Child Windows

Introduction

Recently for one my asp project, I have to open pop windows from the parent window and have to set values from child window to parent and vice versa. Of course, doing this kind of things is very simple. But, I did not found enough tutorial on this so thought of investing some time to write tutorial on this topic.
In this tutorial their will be a textbox and button controls on the parent and child windows, if we enter a value and press button, the value in the text box will be displayed/updated in the other window.
Parent window have a textbox and two button control, where one button control open the child window and other button updates the text box value to the child window (with the caption update the child window). Now in the page load function, we will add code to call the javascipt functions as follows:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.    this.OpenChildWindowButton.Attributes.Add("onclick", openChildWindow());
  4.  
  5.    this.UpdateChildWindowButton.Attributes.Add("onclick", UpdatechildWindow());
  6.  
  7. }
Child windows also have one textbox and one button controls. And we are adding following code on the page_load event:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.    this.UpdateParentwindowButton.Attributes.Add("onclick","UpdateParentWindow()");
  4. }
As you observed there are three functions, openChildWindow(), UpdatechildWindow(), UpdateParentWindow(), these functions are coded in the javascirpt. I will explain them in the following sections:
  • Open the child window
  • Update the child window text box from the parent window
  • Update the Parent window text box from the child window

Open the child window

We called a java script function in the parent window to open the child window, that is openChildWindow() as follows:
  1. <script language=javascript>
  2.  
  3. Var childwindow;
  4.  
  5. function OpenChildWindow()
  6. {
  7. Childwindow = Window.open(“Childwindow.aspx);
  8. }
  9.  
  10. </script>
Window.open javascript function opens the child window. Here we opened child window with ChildWindow.aspx.

Update the child window text box from the parent window

To update the child window from the parent, we need access to the child window and as well, access to child window controls. We can do that as follows:
  1. <script language=javascript>
  2.  
  3. Var childwindow;
  4.  
  5. function UpdatechildWindow()
  6. {
  7.    childwindow.form1.textbox1.value = document.getElementById("TextBox1").value;
  8.  
  9. }
  10.  
  11. </script>
We are access the child window using the variable “childwindow” variable which we initialized when are opening the child window in the OpenChildWindow () function.

Update the Parent window text box from the child window

When we open the child window from the parent using the window.open, a new property is created in the child that is window.opener property. With this property we can access the parent window or the creator window from the child window.
  1.  
  2. <script language=javascript>
  3. function UpdateParentWindow()
  4. {
  5. Window.opener.textbox1.value = document.getElementById("TextBox1").value;
  6.  
  7. }
  8. </script>
  9.  

Dynamically Loading and Accessing the Properties of the User Control

Introduction

User control are powerful, they can divide the code and helps us to reuse them. Normally we can use the UserControl’s in two ways:
  1. Placing the user control by dragging and dropping it to a page at the design time:  If you drag and drop the user control, it adds following two important lines to your code:    <%@ Register TagPrefix=”uc1″ TagName=”HeaderUserControl” Src=”example.ascx” %>   This includes the example user control from the example.ascx.    The example control is then positioned on the page using the following tag:
    1.  
    2. <uc1:ExampleUserControl id="Example1" runat="server"></uc1:ExampleUserControl>
  2. Other is, loading them dynamically when ever needed:
    Some times you may need to load the user control at runtime or dynamically. For this purpose there is a method called LoadControl.  This function has a straightforward syntax - it takes a single argument - the virtual path to the user control page.In this tutorial I will try to explain in detail about loading user controls dynamically.
Loading the user controls is simple but, it is not easy to make the dynamically loaded controls working properly, if we missed some key points. So, in this tutorial, I will try to explain the things by dividing it into following sections:
  • Loading the user Controls Dynamically
  • Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need
  • Accessing the properties of the dynamically loaded User Control

Loading the user Controls Dynamically

As I said, there is method called LoadControl(), which loads the usercontrols dynamically as follows:
  1.  
  2. ExampleUserControl uc = LoadControl("ExampleUserControl.ascx");
  3.  
LoadControl takes just single argument that is the virtual path of the user control. Some people asked me on forums like, why cannot we use new operator to create an instance of the user control? The answer is simple, if we create an instance of the user control only an instance of the control is created but, not all the child controls on the UserControl.
Once, we load the user control, it should be added to page control collection as follows:
  1.  
  2.   Controls.Add(uc);
  3.  

Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need

Above section, helped us to load the control and add it to the controls collectins. But, the drawback is that, we cannot control, where the UserControl actually appear on the page. So, I prefer, always using PlaceHolders and Panel controls. We can add them to the page by just dragging and dropping it on page. Adding the loaded control to them is simple as follows:
  1.  
  2. PlaceHolderright.Controls.Add(uc);
  3.  
  4. PanelRight.Controls.Add(uc);
  5.  
  6.  

Accessing the properties of the dynamically loaded User Control

I accept that, their will be multiple ways do to a same job. Here, I am accessing the properties of the UserControl by casting the UserControl to its class as follows:
  1.  
  2.  Control uc = LoadControl("ExampleUserControl.ascx");
  3.  
  4.  ExampleUserControl example1 = (ExampleUserControl)uc;
  5.  
  6.  example1.xyzproperty = true;
  7.  
  8.  PlaceHolderright.Controls.Add(example1);
  9.  
Note that, the above code should be placed in the code-behind file of web form.
Above code snippets, first loads the usercontrol, casts the loaded usercontrol generic one to the example control and then, we are accessing the properties of controls. At last, we are adding the example1 to controls collection.

Different Ways of Generating Random Passwords in Asp.net (We Can Use Same for Creating Captchas)

1.Using the NewGuid function
2.Generate the random password in our own way.


Introduction

How to generate random passwords, I came across this question many times in forums. Recently again I came across this question, so I decided to write this article. This may help the required ones. Actually it is a simple task; usually there are two best or common methods to generate the random passwords as follows:
  • Using the NewGuid function
  • Generate the random password in our own way

Using the NewGuid function

NewGuid method is a member of the System.Guid structure, this generates a 32 bit hexadecimal string and it will globally unique. If you are familiar with the components then, you can refer this to the CLSIDs.
  1.  
  2. ‘ VB.NET
  3. Dim Result as String = System.Guid.NewGuid().ToString()
  4.  
  5. // C#
  6. string Result = System.Guid.NewGuid().ToString();
  7.  
The result will be as follows:
  1.  
  2. 63dac3e4- 63b9-4057-862b-7d28e2868389
  3.  
Now, you can say like, how to use this crazy thing as the password. Let me explain it, every time when the new GUID is generated it will be unique. So, our passwords will be unique for all the users, so I think this is some what good option, right!! But, the problem is, the string is lengthy.
We will solve this problem by taking the required amount of characters from the generated GUID as follows:
  1.  
  2. public string GenerateRandomPasswordUsingGUID(int length)
  3. {
  4.   // Get the GUID
  5.   string Result = System.Guid.NewGuid().ToString();
  6.  
  7.   // Remove the hyphens
  8.   Result = Result.Replace("-"string.Empty);
  9.  
  10.   // Make sure length is valid
  11.   if (length <= 0 || length > Result.Length)
  12.     throw new ArgumentException("Length must be between 1 and "+ guidResult.Length);
  13.  
  14.   // Return the first length bytes
  15.   return guidResult.Substring(0, length);
  16. }
  17.  
Now, if the length is 8 , the return password from this function will be as follows:
  1.  
  2. 63dac3e4
  3.  
It is a good and healthy password, cheers.

Generate the random password in our own way

Generating passwords with GUID is simple and effective but, some times their will be other things to consider like, if our password needs only alphabets, then GUID will not be the answer. Like this kind of custom rules, we need our own function to generate passwords. There are many ways to write this function, I will show an example how to write this:
Condition like, includes like no numbers, but only, combination of capital and small letters as follows:
  1.  
  2. string alphabets = "abcdefghijklmnopqrstuvwxyz";
  3.  
  4. StringBuilder password = new StringBuilder();
  5.  
  6. Random r = new Random();
  7.  
  8. for(int j=0; j<=20;j++)
  9. {
  10. for(int i=0;i<=5;i++)
  11. {
  12. password.Append(alphabets[r.Next(alphabets.Length)]);
  13. string temp = alphabets[r.Next(alphabets.Length)];
  14. password.Append(temp.ToUpper);
  15.  
  16. }
  17. Response.Write(password.ToString());
  18. password.Remove(0,password.Length);
  19.  
  20. }
  21.  
We are using the string builder to reduce the memory usage and append the password string. In the for loop, we are taking a random alphabet from the string and appending to the password and then, we are taking the random alphabet from the string and converting it to upper case and appending to string.