Wednesday, 6 March 2013

File Upload in Update Panel




We know that we can upload file to server using File upload control. However, if we use file upload control inside update panel, then it doesn't work. The reason behind this is FileUpload control does not work with asynchronous postback.

Step1 :
Place ScriptManager control in .aspx page.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

Step 2:
Write the following source code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
  <asp:Button ID="BtnSubmit" runat="server" Text="Submit"  OnClick="BtnSubmit_Click" />
</ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="BtnSubmit" />
        </Triggers>
    </asp:UpdatePanel>

Step 3:
Add C # code
protected void BtnSubmit_Click(object sender, EventArgs e)
    {
if (FileUpload1.HasFile)
        {
            string imagename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(Server.MapPath("Images/" + imagename));
            string imagepath = Server.MapPath("Images/" + imagename);
            MySqlCommand cmd = new MySqlCommand("insert into table (Image)values('" + imagepath + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
           
        }
}

Step 4:
Add form Encryption Type to:

<form id="Form1" method="post" enctype="multipart/form-data" runat="server">

 You can also Use following code to set Encryption type by code behind:

C# code:
this.Page.Form.Enctype = "multipart/form-data";

Adding a light box effect to or image set



Most of the light box jquery effects are very heavy weight with lots of lines of code. I have come across a light box plugin Lightbox JS v2.0 , which is very easy to use and has very few lines of code.

11)      Download the plugin LightBox JS v2.0 on your systems.
22)      Add the following 4 lines in your <head> tag

        <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
        <script src="js/prototype.js" type="text/javascript"></script>
       <script src="js/scriptaculous.js?load=effects,builder" type="text/javascript"></script>
        <script src="js/lightbox.js" type="text/javascript"></script>

33)      add rel=”lightbox” to the link tag where you what the effect to appear. Eg

<a href="images/image-1.jpg" rel="lightbox" title="my caption">image #1</a>
      
44)      if you have a image set which you want to group,add an attribute name with lightbox in square brackets:
   <a href="images/image-1.jpg" rel="lightbox[roadtrip]">image #1</a>
<a href="images/image-2.jpg" rel="lightbox[roadtrip]">image #2</a>
<a href="images/image-3.jpg" rel="lightbox[roadtrip]">image #3</a>

And you are done… J