Wednesday, 28 November 2012

POP UP DIV USING JAVASCRIPT

 <html>
<head>
<%-- JAVASCRIPT CODING  --%>  
 <script type="text/javascript" >
            function openpopup(id) {
                //Calculate Page width and height
                var pageWidth = window.innerWidth;
                var pageHeight = window.innerHeight;
                if (typeof pageWidth != "number") {
                    if (document.compatMode == "CSS1Compat") {
                        pageWidth = document.documentElement.clientWidth;
                        pageHeight = document.documentElement.clientHeight;
                    } else {
                        pageWidth = document.body.clientWidth;
                        pageHeight = document.body.clientHeight;
                    }
                }
                //Make the background div tag visible...
                var divbg = document.getElementById('bg');
                divbg.style.visibility = "visible";

                var divobj = document.getElementById(id);
                divobj.style.visibility = "visible";
                if (navigator.appName == "Microsoft Internet Explorer")
                    computedStyle = divobj.currentStyle;
                else computedStyle = document.defaultView.getComputedStyle(divobj, null);
                //Get Div width and height from StyleSheet
                var divWidth = computedStyle.width.replace('px', '');
                var divHeight = computedStyle.height.replace('px', '');
                var divLeft = (pageWidth - divWidth) / 2;
                var divTop = (pageHeight - divHeight) / 2;
                //Set Left and top coordinates for the div tag
                divobj.style.left = divLeft + "px";
                divobj.style.top = divTop + "px";
                //Put a Close button for closing the popped up Div tag
                if (divobj.innerHTML.indexOf("closepopup('" + id + "')") < 0)
                    divobj.innerHTML = "<a href=\"#\" onclick=\"closepopup('" + id + "')\"><span  class=\"close_button\">X</span></a>" + divobj.innerHTML;
            }
            function closepopup(id) {
                var divbg = document.getElementById('bg');
                divbg.style.visibility = "hidden";
                var divobj = document.getElementById(id);
                divobj.style.visibility = "hidden";
            }
</script>

 <%-- CSS CODING  --%>  
 <style type="text/css">
.popup {
      background-color: #DDD;
      height: 600px; width: 500px;
      border: 5px solid #666;
      position: absolute; visibility: hidden;
      font-family: Verdana, Geneva, sans-serif;
      font-size: small; text-align: justify;
      padding: 5px; overflow: auto;
      z-index: 999;
}
.popup_bg {
      position: absolute;
      visibility: hidden;
      height: 100%; width: 100%;
      left: 0px; top: 0px;
      filter: alpha(opacity=70); /* for IE */
      opacity: 0.7; /* CSS3 standard */
      background-color: #999;
      z-index: 1;
}
.close_button {
      font-family: Verdana, Geneva, sans-serif;
      font-size: small; font-weight: bold;
      float: right; color: #666;
      display: block; text-decoration: none;
      border: 2px solid #666;
      padding: 0px 3px 0px 3px;
}
body { margin: 0px; }
</style>
</head>
<body>
<%--CALLING THE OPENPOPUP FUNCTION  --%>
 <a href = "#" onclick="openpopup('popup1')"> CLICK HERE TO OPEN POPUP DIV </a>
 <div id="popup1" class="popup">
    HI, DUDE...
    WELCOME TO OUR POP UP DIV
 </div>
<div id="bg" class="popup_bg"></div> 
</body>
</html>

RETRIEVING THE IMAGE FROM DATABASE AND DISPLAYING IN DATALIST

CREATE TABLE

CREATE TABLE Tablename
(
[ImageID] [int] IDENTITY(1,1) NOT NULL,
[Images] [image] NULL,
[ImageName] [varchar](max) NULL,
)

IN THE ASPX

<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" DataKeyField="ImageID" CellPadding="15" Width="500px">
       <ItemTemplate>
                <div>
                      <asp:Image ID ="img" Width ="120px" Height="120px"  runat="server" 
ImageUrl ='<%#   "Handler.ashx?ImageID= " + Eval ("ImageID") %> '></asp:Image>
               </div>
</br>
                <div>
                   <asp:Label ID ="lblName" ForeColor="#FFFFFF"  runat="server"
Text ='<%# Eval ("ImageName") %>'></asp:Label>
 <br />
       </ItemTemplate>
</asp:DataList>

IN THE HANDLER PAGE 

Add New Item -> Select Generic handler -> Add the below coding in the Handler.ashx

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public class Handler : IHttpHandler {
    static string constr = ConfigurationManager.ConnectionStrings["addname"].ConnectionString;
    SqlConnection con = new SqlConnection(constr); //Configuring using connection string from the  webconfig
    
    public void ProcessRequest (HttpContext context)   
    {
        string imgID = context.Request.QueryString["ImageID"].ToString();
        SqlCommand cmd = new SqlCommand("select * from Tablename where ImageID=" + imgID, con);
        con.Open();
        SqlDataReader Dr = cmd.ExecuteReader();
        Dr.Read();    
        context.Response.BinaryWrite((byte[]) Dr["Images"]);
        con.Close();
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}

IN THE ASPX.CS

//These namespaces should be included
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default : System.Web.UI.Page
{
    static string constr = ConfigurationManager.ConnectionStrings["giricon"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);//Calling connection string from the web.config
    SqlDataAdapter adp = new SqlDataAdapter();
    DataTable dt = new DataTable();
 protected void Page_Load(object sender, EventArgs e)
    {
     if (!Page.IsPostBack)
        {
           binding();
        }
    }
 private void binding()
    {
        adp = new SqlDataAdapter("Select * from tablename", con);
        adp.Fill(dt);
        adp.Dispose();
        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
}

GRIDVIEW MAIL USING ASP.NET C#

 IN THE ASPX PAGE

  <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
       </asp:GridView>
    </div>
        <asp:Button ID="btnSendMail" runat="server" Text="Send Gridview Mail"
        onclick="btnSendMail_Click" Width="123px" />
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>

IN THE ASXP.CS PAGE
//Name space to be used
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class Default : System.Web.UI.Page
{
    static string strcon = ConfigurationManager.ConnectionStrings["SQLCON"].ConnectionString;
    //create new sqlconnection and connection to database by using connection string from web.config file
    SqlConnection con = new SqlConnection(strcon);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }

    // This method is used to bind gridview from database
    protected void BindGridview()
    {
      
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tablename", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
//Send mail button click event
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        string to = TextBox1.Text;//Gets the TO id
        string From = "fromid@gmail.com";
        string subject = "Mark Detail";
        string Body = "HI ,<br>  Check d Attachment <br><br>";
        Body += GridViewToHtml(GridView1);
        Body += "<br><br>Regards,<br>sathya";
        bool send = send_mail(to, From, subject, Body);
        if (send == true)
        {
            string CloseWindow = "alert('Mail Sent Successfully!');";
            ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
        }
        else
        {
            string CloseWindow = "alert('Problem in Sending mail');";
            ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
        }

    }
//Mail function
    public bool send_mail(string to, string from, string subject, string body)
    {
        MailMessage msg = new MailMessage(from, to);
        msg.Subject = subject;
        AlternateView view;
        SmtpClient client;
        StringBuilder msgText = new StringBuilder();
        msgText.Append(" <html><body><br></body></html> <br><br><br>  " + body);
        view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html");

        msg.AlternateViews.Add(view);
        client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential("fromid@gmail.com", "fromidpassword");
        client.EnableSsl = true; //Gmail works on Server Secured Layer
        client.Send(msg);
        bool k = true;
        return k;
    }
//calling the html of the gridview
    private string GridViewToHtml(GridView gv)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        gv.RenderControl(hw);
        return sb.ToString();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
    }
}

Tuesday, 20 November 2012

JQUERY FOR GO TO TOP SCROLL

<html>
<head >

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type='text/javascript'>
         $(function () {
             $.fn.scrollToTop = function () {
                 $(this).hide().removeAttr("href");
                 if ($(window).scrollTop() != "0") {
                     $(this).fadeIn("slow")
                 }
                 var scrollDiv = $(this);
                 $(window).scroll(function () {
                     if ($(window).scrollTop() == "0") {
                         $(scrollDiv).fadeOut("slow")
                     } else {
                         $(scrollDiv).fadeIn("slow")
                     }
                 });
                 $(this).click(function () {
                     $("html, body").animate({
                         scrollTop: 0
                     }, "slow")
                 })
             }
         });
         $(function () {
             $("#hb-gotop").scrollToTop();
         });
    </script> 
<style type="text/css">
 #hb-gotop
  {
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  width:80px;
  background-color: #fff198;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#99EEEEEE',EndColorStr='#99EEEEEE');
  text-align:center;
  padding:5px;
  position:fixed;
  bottom:10px;
  right:10px;
  cursor:pointer;
  color:#444;
  text-decoration:none;
  border:1px solid #C9C9C9;
  }
 </style>  


</head >
<body>
<form id="form1" runat="server">

    </form>

        <a href='#gv' id='hb-gotop' style='display:none;'>Go to Top</a>


</body>
</html>

Monday, 19 November 2012

IMAGE UPLOAD INTO SQL DATABASE

CREATE A TABLE IN SQL

Create table uploadimage
(

  [ImageID] [int] IDENTITY(1,1) NOT NULL,
[Images] [image] NULL,
[ImageName] [varchar](max) NULL,

)

IN THE ASPX PAGE

   <form id="form1" runat="server">
        <div>
        <asp:Label ID="lblfu" runat="server" Text="Image Upload"></asp:Label>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
     
        <asp:Label ID="lblimgname" runat="server" Text="Image Name" ></asp:Label>
        <asp:TextBox ID="txtimgname" runat="server"></asp:TextBox>
        <br />
   
        <asp:Button ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click" />
        <asp:Label ID="lblmsg"  runat="server" Text=""></asp:Label>
        <br/>
     
    </div>
    </form>

IN THE ASPX.CS PAGE(In the Upload button click event)

    static string constr = ConfigurationManager.ConnectionStrings["stringname"].ConnectionString;
    SqlConnection con = new SqlConnection(constr); //configuring connection string from the webconfig


 protected void btnupload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile myimg = FileUpload1.PostedFile;
            myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);
            SqlCommand cmd = new SqlCommand("insert into pimage(Images,ImageName) values          (@images,@imageName)", con);


            SqlParameter uploadimg = new SqlParameter("@images", SqlDbType.Image);
            uploadimg.Value = img;
            cmd.Parameters.Add(uploadimg);

            SqlParameter imgname = new SqlParameter("@imageName", SqlDbType.VarChar, 100);
            imgname.Value = txtimgname.Text;
            cmd.Parameters.Add(imgname);


            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Text = "Image uploaded";
            txtimgname.Text = "";
     
        }
        else
        {
            lblmsg.Text = "Image upload unsuccessfull";
        }
    }


back to top