Wednesday, March 7, 2007

MutiView Control

You can think of MultiView as more advanced panel that let you switch between groups of controls on a page.

MutliView
Essentially, the MultiViewgives you a way to declare multiple views and show only one at a time.
It 's really simple to use that, just put it from the Toolbox, then add one or more View control inside this.Now you can have you user interface design in all the views, separately.
By default the ActiveViewIndex, the property which determine which of the view control 's has to be shown, is set to -1.
So you can just set the index of your desire.
It 's also a method called SetActiveView which can set the active view control by it 's reference.
It the sample that I 've been created.I set the ActiveViewIndex to 0 in Page_Load event, when it 's not post backed.

Masoud_TB

Tuesday, March 6, 2007

Sending email in ASP .NET 2

In asp.net application it may needed to send an email to specefic email address. In ASP .Net 1 and 1.1 it was so simple to send an email, as it 's in ASP .NET 2.0.

In ASP .NET 1.1 we just create a MailMessage object from the System.Web.Mail namespace setting the properties and send that mail. Just like this:

MailMessage m = new MailMessage();
m.Body = “Mail Body Text”
m.BodyEncoding = System.Text.Encoding.UTF8;
m.BodyFormat = MailFormat.Html;
m.To = "SbElse@AnotherWebSite.com";
m.Subject = ”Mail Subject”;
m.From = "Somebody@AWebSite.com";
m.Priority = MailPriority.High;


SmtpMail.Send(m);


ASP .NET team in the 2.0 version decided to move the System.Net.Mail instead of System.Web.Mail.
Actually System.Net.Mail is a new namespace which contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery. (as Microsoft Says)

Anyway, for sending mail in ASP .NET 2.0 , I just create a MailMessage and set the properties of that, and at the end I use a SmtpClient object to send the mail message, like this:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("From@WebSite.com","To@WebSite.com");
message.Subject = "Here is the subject";
message.Body = "Body of the message";
message.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnFailure;
message.Priority = System.Net.Mail.MailPriority.High;


System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Send(message);


I used the UseDefaultCredentials becuase for some SMTP servers it requires to authenticate for sending mail. So this information can be setted in web.config file when UseDefaultCredentials is true.

Finally, I set these configuration settings on the web.config file like this:

<system.net>
<mailSettings>
<smtp from="user@host.com">
<network userName="user@host.com" password="pass" host="localhost">
</smtp>
</mailsettings>
</system.net>

notice that the host attribute of the network entity is the SMTP mail server address. Which I used localhost in this sample.

I hope it can help you.
Masoud_TB

Input Language

While you have a multilingual application you may intended to set the current input language (Keyboard language) in your application. There is a class called InputLanguage in System.Windows.Forms namespace, which Provides methods and fields to manage the input language (as Microsoft says)

In this Windows C# sample I 'm going to list all the InputLanguages installed on the current windwos and set the InputLanguage which the user desired.

For instance I have installed English and Farsi keyboard installed on my windows xp. So a comboBox list these two languages and whenever the user changes the languages on it, the system input language will change to the selected one. On the other hand I have two specefied textbox which I want whenever the user tries to type something on these textboxed change the keyboard language to farsi or english as desired.


Masoud_TB

Monday, March 5, 2007

Uploading Images to Server

Last week I was working on a project and in web form we wanted to ask people to upload their images to the server, and fill the application form.

So I tried to use the FileUpload Control which is a new WebControl in ASP.NET to upload the images to the server. The point was that I wanted to restrict people to only upload images with file extension "jpg" and "gif", so because the FileUpload control by default allow all types of file format to be uploaded, I used a regular expression to extract the file extension from the file name.
Another important thing for me was the file size of the images. I just want to allow users to upload images with max size of 50Kb, so I used the Length on FileContent property of the FileUpload, and at the end of my code, I used the SaveAs method to save the file in a specific way.

Notice that while your working with web application you have to get the physical path of your web application, not the http://www.yoursite.com/ so I used the Server.MapPath(Request.ApplicationPath) to get the physical path.

Masoud_TB