Archive

Archive for October, 2011

AJAX Modal Popup with Validation Summary

October 28, 2011 1 comment

Hi all,

try this example to use AJAX Modal Popup with Validation Summary

— CSS :-


.modalPopup {
 background-color:#FFFFFF;
 border-width:3px;
 border-style:solid;
 border-color:Gray;
 padding-bottom:10px;
 position: absolute;
 
}

.hidelbl{display:none;}

— Javascript function :-

<script type="text/javascript">
    //--- To Show Modal Popup of ValidationSummary
    function ShowModalDialog(group) {
        var g = '';
        g = group;
        var vs = document.getElementById('<%=ValidationSummary1.ClientID %>');
        vs.validationGroup = g;
        var x = $find('<%= mdl_Validation.ClientID %>');
        Page_ClientValidate(g);
        if (!Page_IsValid)
            x.show();
    }
</script>

— ASPX Page  :-


<cc1:ModalPopupExtender ID="mdl_Validation" runat="server"
    TargetControlID="lblHidden"
    PopupControlID="SummaryDiv"
    BackgroundCssClass="modalBackground" DynamicServicePath="" Enabled="True" CancelControlID="btnclose" >
</cc1:ModalPopupExtender>
<div id="SummaryDiv" runat="server" style="display:none"
    <table >
        <tr>
        <td valign="top">
            <asp:Image ID="img_Validation" runat="server" ImageUrl="~/images/warning.gif" />
            <asp:Label ID="LabelMsg" runat="server" Text="Please Check the following :-" ></asp:Label>
          </td>
        </tr>
        <tr>
            <td colspan="2" >
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="i" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Button ID="btnclose" runat="server" Text="Close"  />
            </td>
        </tr>
    </table>     
</div>
<asp:Label ID="lblHidden" runat="server" CssClass="hidelbl"></asp:Label>

<table style="width: 45%;">
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="First Name :-"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_FirstName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rv_FirstName" runat="server"
                    ControlToValidate="txt_FirstName" ErrorMessage="Enter First Name"
                    ValidationGroup="ii">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Email Address :-"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_Email" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rv_Email" runat="server"
                    ControlToValidate="txt_Email" Display="Dynamic"
                    ErrorMessage="Enter Email Address" ValidationGroup="ii">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="rev_Email" runat="server"
                    ControlToValidate="txt_Email" Display="Dynamic"
                    ErrorMessage="Enter Valid Email Format"
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                    ValidationGroup="ii">*</asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center">
                <asp:Button ID="btn_Save" runat="server" Text="Submit" ValidationGroup="ii" />
            </td>
        </tr>
</table>

and in code behind in Page Load add this code :-

btn_Save.OnClientClick = "ShowModalDialog('ii')";

Hope this helps

Good Luck

Categories: AJAX, ASP.Net

Creating Image Thumbnails in ASP.NET

October 28, 2011 Leave a comment

Hi all ,

try this example to Creating image thumbnails in ASP.NET

1) Open VS2008 and create a new website and add new Web page

2) Add new folder inside project and name it Upload which will upload image in it

3) From Toolbox add Fileupload and button and two Images controls

<div>
        Choose an image to upload
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btn_Upload" runat="server" onclick="btn_Upload_Click" Text="Upload" />
   
    </div>
    <p>
        Original image</p>
    <p>
        <asp:Image ID="Image1" runat="server" />
    </p>
    <p>
        Thumbnail image</p>
    <p>
        <asp:Image ID="Image2" runat="server" />
    </p>

and in code behind :-

    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        string target = Server.MapPath("~/Upload");
        System.Drawing.Image.GetThumbnailImageAbort thumbnailImageAbortDelegate = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(System.IO.Path.Combine(target, FileUpload1.FileName));
            using (System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(FileUpload1.PostedFile.InputStream))
            {
                using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(80, 80, thumbnailImageAbortDelegate, IntPtr.Zero))
                {
                    thumbnail.Save(System.IO.Path.Combine(target, "thumb_" + FileUpload1.FileName));
                }
            }
            Image1.ImageUrl = "Upload/" + FileUpload1.FileName;
            Image2.ImageUrl = "Upload/" + "thumb_" + FileUpload1.FileName;
        }       
    }
    public bool ThumbnailCallback()
    {
        return false;
    }

Hope this helps
Good Luck

Categories: ASP.Net

Enable and Disable of Textbox through a Checkbox

October 15, 2011 Leave a comment

Hi all,

try this example to Enable and disable of text box through a checkbox using Javascript

<head runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
    function enable_text(status)
    {
    status=!status;
    document.getElementById('<%= txt_Name.ClientID %>').disabled = status;
    document.getElementById('<%= txt_Name.ClientID %>').value='';
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:CheckBox ID="CheckBox1" runat="server" onclick="javascript:enable_text(this.checked);" />Others
    <asp:TextBox ID="txt_Name" runat="server"></asp:TextBox>
    </form>
</body>

and in code behind we will add code to disable TextBox till Checkbox checked

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txt_Name.Attributes.Add("disabled", "disabled");
        }
    }

Hope this helps

Good Luck

Categories: ASP.Net, Javascript

Create and send HTML Formatted Email

October 8, 2011 1 comment

Hi all,

Try this example to create and send HTML Formatted Email using ASP.Net

To know how to send Email using Gmail Account from here

1) Open VS 2008 and create a new Web Site

2) Add New HTML Page and inside it add the Body of your Email name it “EmailTemplate.htm”

<!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><title></title></head>
<body>
<img src = "http://www.mywebsite.com/images/Logo.png" /><br /><br />
<div style = "border-top:3px solid #22BCE5">&nbsp;</div>
<span style = "font-family:Arial;font-size:10pt">
Hello <b>{UserName}</b>,<br /><br />
Welcome in our Web Site.<br /><br />
<a style = "color:#22BCE5" href = "{Url}">{Title}</a><br />
<br /><br />
Thanks<br />
Website Admin
</span>
</body>
</html>

3) Add new Web Page

add Button Control and in code behind add this Code :-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void SendEmail(object sender, EventArgs e)
    {
        string body = this.PopulateBody("Yasser Zaid",
            "Check Our Website",
            "http://www.mywebsite.com/Default.aspx");
        this.SendHtmlFormattedEmail("recipient@gmail.com", "Message from Admin!", body);
    }

    private string PopulateBody(string userName, string title, string url)
    {
        string body = string.Empty;
        using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
        {
            body = reader.ReadToEnd();
        }
        body = body.Replace("{UserName}", userName);
        body = body.Replace("{Title}", title);
        body = body.Replace("{Url}", url);
        return body;
    }

    private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
    {
        using (MailMessage mailMessage = new MailMessage())
        {
            mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = true;
            mailMessage.To.Add(new MailAddress(recepientEmail));
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ConfigurationManager.AppSettings["Host"];
            smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
            NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            smtp.Send(mailMessage);
        }
    }
}

Hope this helps

Good Luck

Categories: ASP.Net

Solve Database Backup Problem

October 2, 2011 1 comment

Hi all,

In this post i will show how to solve the error when try to backup database

Error Message :- System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing ‘dbName’ database. (Microsoft.SqlServer.Smo) The error Message below : Restore failed for Server ‘SQL Server name’. (Microsoft.SqlServer.Smo)

So to solve the problem, you can use the overwrite the existing database option while you’re restoring.

1) On Restore Database, select Options tab on the left menu. Then, check Overwrite the existing database on Restore options.
Note: This option will overwrite any existing data on the targeted database.

2) Try to restore the database, the problem should be gone now.
Note: you can also delete the targeted database before perform restore a database. This way also gives the result as same as the solution above.

Hope this helps

Good Luck

Categories: SQL Server

Validate Number of Checked items in CheckListBox using Javascript

October 1, 2011 1 comment

Hi all ,

try this example to Validate Number of Checked items in CheckListBox using Javascript

try this Javascript function :-


<script language="javascript" type="text/javascript">
        var atLeast = 1
        function Validate() {
            var CHK = document.getElementById("<%=CheckBoxList1.ClientID%>");
            var checkbox = CHK.getElementsByTagName("input");
            var counter = 0;
            for (var i = 0; i < checkbox.length; i++) {

                if (checkbox[i].checked) {
                    counter++;
                   
                }
            }
            if (atLeast > counter) {
                alert("Please select atleast " + atLeast + " item(s)");
                return false;
            } 
            return true;
        }
</script>

The HTML Page will be :-

<asp:CheckBoxList ID="CheckBoxList1" runat="server"  >
    </asp:CheckBoxList>
        <input id="Button1" type="button" value="button" onclick="javascript:Validate();" />

Hope this helps

Good Luck

Categories: ASP.Net, Javascript