Aug 10, 2011

How to use Send Email Concepts in Asp.net application

The MailMessage and SMTP are classes defined in the .NET Framework Class Library's System.Web.Mail namespace. It is due to a security change made to asp.net just before it shipped, you need to set SMTPMail's, SMTPServer property to localhost even though localhost is the default. In addition, you must use the IIS configuration applet to enable localhost to relay message through the local SMTP Service.


Example:
        MailMessage message = new MailMessage ();
        message.From = <from@testmail.com>;
        message.To = <to@testmail.com>;
        message.Subject = "Welcome to Asp.net Example";
        message.Body = "Free Subscribe to the Asp.net Example";
        SmtpMail.SmtpServer = "localhost";
        SmtpMail.Send (message);

Aug 7, 2011

How to use Calendar with Literal Controls in Asp.net

(i) .ASPX File Content
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Calendar with Literal Controls Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Calendar Runat="server" ID="Cal" BorderStyle="Solid" BackColor="White" Width="162px" ForeColor="Black" 
     CellSpacing="1" Height="113px" Font-Size="9pt" Font-Names="Verdana" 
     BorderColor="Black" PrevMonthText="Prev" NextMonthText="Next" ShowGridLines="True"
     TitleFormat="Month" SelectionMode="DayWeekMonth" OnSelectionChanged="Cal_SelectionChanged" />
     <Br>
     <asp:Literal ID="Literal1" Runat="server" />
    </div>
    </form>
</body>
</html>
(ii) .ASPX.CS File Content
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{   
    
    protected void Cal_SelectionChanged(object sender, EventArgs e)
    {
        try
        {            
            foreach (DateTime date in Cal.SelectedDates)
            {
                Literal1.Text = " Selected Date is  :" + date.ToString("d");
            }
        }
        catch (Exception )
        {
            
            throw;
        }
    }
}
(iii) Output

How to Add Two numbers using TextBox Control in asp.net

(i) .ASPX File Content
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Adding Two Number using TextBox Control Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0" width="300px">
         <tr>
          <td><asp:Label ID="Label1" runat="server" Text="First Number"></asp:Label></td>
          <td><asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
          <td><asp:Label ID="Label2" runat="server" Text="Second Number"></asp:Label></td>
          <td><asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox></td>
         </tr>
         <tr>
         <td></td>
         <td><asp:Button ID="btnCalculate" runat="server" Text="Submit" OnClick="btnCalculate_Click" /></td>
         </tr>
         <tr>
          <td><asp:Label ID="Label3" runat="server" Text="Result is"></asp:Label></td>
          <td><asp:TextBox ID="txtResult" runat="server"></asp:TextBox> </td>
         </tr>
        </table>    
    </div>
    </form>
</body>
</html>
(ii) .ASPX.CS File Content
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    //Declare variables
    int num1, num2, total = 0;
    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            //Adding two numbers
            num1 = int.Parse(txtFirstNumber.Text.ToString().Trim());
            num2 = int.Parse(txtSecondNumber.Text.ToString().Trim());
            total = num1 + num2;
            txtResult.Text = total.ToString();
        }
        catch (Exception)
        {
            throw;
        }
    }
}
(iii) Output