ASP.NET C# How to send mail with SMTP Authetication.

To send email from ASP.NET hosting account it is necessary to use credentials for smtp authentication.
Here is a way to do this with help function for sending mail.

private void sendEmail(string strTo, string strFrom,
string strSubject, string strBody, Attachment item, bool isHTML)

MailAddress maFrom = new MailAddress(strFrom, "sender");
MailAddress maTo = new MailAddress(strTo);
MailMessage mail = new MailMessage(maFrom, maTo);
mail.IsBodyHtml = isHTML;
mail.Subject = strSubject;
mail.Body = strBody;
SmtpClient sc = new SmtpClient();
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("email account", "password");

sc.Credentials = basicAuthenticationInfo;
sc.Host = "";
sc.Port = 25;

Email account you can register with the hosting control panel (if you don't have already). The host is usually mail.+ domain name.

ASP.NET Globalization, using UTF-8 or any other encoding

There are some difficulties by setting globalization of a web site. By default .Net framework use unicode. Therefore it is not necessary to set this. If you have to use different encoding you can set this in web.config:
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="bg-BG" uiCulture="bg-BG">

Check the following links for more information:

And here is a explanation of the Visual Studio behavior. VS may cause some problems if you don't know these issues.

The last link helped me to decide my problem. I had to save all my files with utf-8 encoding to get Cyrillic encoded response.

ASP.NET How to use nested DataList

<asp:DataList ID="dlParent" runat="server">
<asp:Label ID="lbl" runat="server"></asp:Label>
<asp:DataList ID="dlNested" runat="server">
<asp:Label ID="lbl" runat="server"></asp:Label>


Private Sub dlParent_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles

If Not e Is Nothing Then
Dim lbl As Label
Dim dlNested As DataList
lbl = CType(e.Item.FindControl("ParentLabel"), Label)
dlNested = CType(e.Item.FindControl("dlChild"), DataList)

lbl.Text = "content"

AddHandler dlNested .ItemDataBound, AddressOf dlNested_ItemDataBound

dlNested.DataSource = AnyDataSource
End If
End Sub

Private Sub dlNested_ItemDataBound(ByVal sender As Object, ByVal e As
Dim lbl As Label

If Not e Is Nothing Then
lbl = CType(e.Item.FindControl("lbl"), Label)
lbl = DataBinder.Eval(e.Item.DataItem, "LabelContent").ToString()
End If

End Sub

The key moment is adding new Handler for nested DataList.

Note: AnyDataSource must have field (column) "LabelContent".

XHTML Special Characters

They are well known, but I often forget the syntax of some of them.

ASP.NET AJAX - Validators doesn't work after postback by UpdatePanel

There is a tedious problem with validation control when AJAX Update panel is used.
A solution at the moment is this extension of validator controls including script manager.

It is pretty simple to integrate - just follow the instructions of Matt Gibbs.

ASP.NET/Javascript - Handling Enter click for submitting the active form.

There are some ways to do this.
The most simple way is to use the default submit button - this is by default the first button in the which performs postback (link button, image button, button). If the button you would like to use by default is not the first you can add one more button on the top of page (for example image button with 1x1 pixels picture - invisible). This guaranties that the pressing enter will cause postback independant of the browser - it works with every browser (that I know :-)).

However, we can use simple javascript code to handle the event of pressing enter button:
<body onkeydown="if(event.keyCode == 13){document.getElementById('" + ImageButton1.ClientID + "').click();}">

One more way: DataList1.Attributes.Add("onkeypress", String.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')", ImageButton1.ClientID)) - this performs postback when the focus is on the datalist1. Of course we can use every control that has child controls.
But my experience shows that this doesn't work good with Firefox.

Here is also way to add javascript event dynamically:

<script type="text/javascript">
addEvent(window, 'load', function() {

function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;

C#, VB.NET - How to convert string to Guid

string str="565G6B7D-8H7D-8J7D-A0C6-908H7D868H7D";
Guid g= new Guid(str);

Dim str As String="565G6B7D-8H7D-8J7D-A0C6-908H7D868H7D";
Dim g As System.Guid = New Guid(str);