Thursday 26 May 2016

Load the popup on just the first time page request only


protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            ClientScript.RegisterHiddenField("isPostBack", "1");
        }
}
<script type="text/javascript" >

        $(document).ready(function() {

            var isPostBackObject = document.getElementById('isPostBack');
            if (isPostBackObject == null) {
                var id = '#dialog';
                //modal: true
                //Get the screen height and width
                var maskHeight = $(document).height();
                var maskWidth = $(window).width();

                //Set heigth and width to mask to fill up the whole screen
                $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

                //transition effect  
                $('#mask').fadeIn(1000);
                $('#mask').fadeTo("slow", 0.9);

                //Get the window height and width
                var winH = $(window).height();
                var winW = $(window).width();

                //Set the popup window to center
                $(id).css('top', winH / 2 - $(id).height() / 2);
                $(id).css('left', winW / 2 - $(id).width() / 2);

                //transition effect
                $(id).fadeIn(2000);

                //if close button is clicked
                $('.window .close').click(function(e) {
                    //Cancel the link behavior
                    e.preventDefault();

                    $('#mask').hide();
                    $('.window').hide();
                });

            }
<style type="text/css">
  #mask {
  position:absolute;
  padding-left:60px;
  padding-top:80px;
  padding-bottom:80px;
  padding-right:50px; /*-- These Padding is for the border of jquery Pop-up --*/
  left:0;
  top:0;
  z-index:9999;
  background-color:#000000;
  display:none;
}  
#boxes .window {
  position:absolute;
  left:0;
  top:0;
  width:330px;
  height:147px;
  display:none;
  z-index:9999;
  padding:20px;
  background-color:#000000;  
  border: 20px solid #79BBFD;  
  border-radius: 10px;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;

}
#boxes #dialog {
  padding:10px;
  background:url('images/localOffers-bg.jpg'); 
  width:330px; height:147px; 
  background-repeat:no-repeat; 
  margin-top:20px;
  
}

<div id="boxes">
<div id="mask" >
<div id="dialog" class="window">
<span class="LocalOffersHeading">Find Local Offers                                     
<a href="#"  style="float: right; margin: -45px -45px 0 0;"  class="close"> 
<img src="Images/close_pop.png" alt="CLOSE" /></a> </span> <br /> <br />
Finding local offers has never been easier. Just select your city location from
the drop down and find the most recent local discounts in your area. We've also
made simple to stay in touch with you favorite merchants but signing up for the
email discounts! <br /><br />
<asp:DropDownList ID="ddlj" runat="server"  style="Width:228px;  Height:20px;" 
 OnSelectedIndexChanged="popup_SelectedIndexChanged" >
</asp:DropDownList>
                                     
<asp:ImageButton ID="jqbutton" runat="server" ImageUrl="~/images/bt-go.jpg"
 OnClick="imgBtnGo_Click"
 CausesValidation="false" Style="height: 16px;"  />
                                       
</div>
<!-- Mask to cover the whole screen -->
<%--<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;" id="mask">
</div>--%>
                                </div>
                            </div>

How to send email Asynchronously in ASP.Net using Background Thread

HTML Markup

<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 80px">
            To:
        </td>
        <td>
            <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Subject:
        </td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td valign = "top">
            Body:
        </td>
        <td>
            <asp:TextBox ID="txtBody" runat="server" TextMode = "MultiLine" Height = "150" Width = "200"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            File Attachment:
        </td>
        <td>
            <asp:FileUpload ID="fuAttachment" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Email:
        </td>
        <td>
            <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Password:
        </td>
        <td>
            <asp:TextBox ID="txtPassword" runat="server" TextMode = "Password"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button Text="Send" OnClick="SendAsyncEmail" runat="server" />
        </td>
    </tr>
</table>



Namespaces
You will need to import the following namespaces
C#

using System.IO;
using System.Net;
using System.Net.Mail;
using System.Threading;

VB.Net

Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports System.Threading


Sending email asynchronously in background using Thread


C#

protected void SendAsyncEmail(object sender, EventArgs e)
{
    string to = txtTo.Text;
    string from = txtEmail.Text;
    string password = txtPassword.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
    HttpPostedFile postedFile = fuAttachment.PostedFile;
    Thread email = new Thread(delegate()
    {
        SendEmail(to, from, password, subject, body, postedFile);
    });
 
    email.IsBackground = true;
    email.Start();
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
 
private void SendEmail(string to, string from, string password, string subject, string body, HttpPostedFile postedFile)
{
    using (MailMessage mm = new MailMessage(from, to))
    {
        mm.Subject = subject;
        mm.Body = body;
        if (postedFile.ContentLength > 0)
        {
            string fileName = Path.GetFileName(postedFile.FileName);
            mm.Attachments.Add(new Attachment(postedFile.InputStream, fileName));
        }
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(from, password);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
    }
}

VB.Net

Protected Sub SendAsyncEmail(sender As Object, e As EventArgs)
    Dim [to] As String = txtTo.Text
    Dim from As String = txtEmail.Text
    Dim password As String = txtPassword.Text
    Dim subject As String = txtSubject.Text
    Dim body As String = txtBody.Text
    Dim postedFile As HttpPostedFile = fuAttachment.PostedFile
    Dim email As New Thread(Sub() SendEmail([to], from, password, subject, body, postedFile))
    email.IsBackground = True
    email.Start()
    ClientScript.RegisterStartupScript([GetType](), "alert", "alert('Email sent.');", True)
End Sub
 
Private Sub SendEmail([to] As String, from As String, password As String, subject As String, body As String, postedFile As HttpPostedFile)
    Using mm As New MailMessage(from, [to])
        mm.Subject = subject
        mm.Body = body
        If postedFile.ContentLength > 0 Then
            Dim fileName As String = Path.GetFileName(postedFile.FileName)
            mm.Attachments.Add(New Attachment(postedFile.InputStream, fileName))
        End If
        mm.IsBodyHtml = False
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.EnableSsl = True
        Dim NetworkCred As New NetworkCredential(from, password)
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        smtp.Send(mm)
    End Using
End Sub