Introduction
Most of the articles here are focusing on the C# code running on the server, but that's, of course, natural, the site is called C# corner! But hey, we shouldn't forget what we can do with some old school HTML, JavaScript, and CSS. Here comes two, hopefully, useful tip celebrating the client-side.
Sometimes you're able to achieve pretty neat things with just some simple HTML and JavaScript. It could actually spice up that old web page quite rapidly! Please, feel free to download the solution file and change it to suit your needs. First, we look at a simple div acting as a modal dialog and then we create a simple JavaScript slideshow.
Disabling the background

- <div id="overlay">
- <div class="info_div">
- <table width="100%">
- <tr>
- <td align="right">
- <img src="x.gif" onclick="display_overlay(null);" />
- </td>
- </tr>
- </table>
- <div class="inner_div">Here you can place whatever you want.</div>
- </div>
- </div>
The div named overlay is the div making the background appear to be "disabled". This by having the height and the width properties set to 100% and the background image set to the small 1x1 png image with 50% transparency. This creates a div that covers the whole browser window and the background image of it makes the page look as it was disabled. These attributes are set in the CCS code as follows.
We set the visibility to hidden by default so that the div doesn't show when loading the page from the start and we set the position to fix. The height and width are set to 100%, the background image to our transparent png image and the z-index to 1000 which places the div in the top most position on the page.
The div named info_div is the one that acts like a dialog and inside that div you could put whatever you like, the CSS for that div looks like follows.
The only thing we need now is a function that displays the overlay div. We use the getElementById to get a reference to the overlay div and then we just check whether it's visible or not by setting the visibility property of the div object.
- #overlay
- {
- visibility: hidden;
- position: fixed;
- left: 0px;
- top: 0px;
- width:100%;
- height:100%;
- background-image:url('overlay.png');
- z-index:1000;
- }
- .info_div
- {
- padding:5px;
- margin:0px;
- background: #ffffff;
- border: solid 2px #333333;
- position: absolute;
- width: 350px;
- left: 250px;
- top: 50px;
- }
- <script type="text/javascript">
- function display_overlay(div_element)
- {
- el = document.getElementById('overlay');
- el.style.visibility = (el.style.visibility == "visible") ? "hidden" :
- "visible";
- }
- </script>
- <a href="javascript:void(0);" onclick="display_overlay('info_div');">Show DIV</a>
Enhancements
Image slideshow using JavaScript
- <table>
- <tr>
- <td style="height:210px;width:210px;border: solid 1px #999999;" align="center" valign="middle">
- <asp:Image ID="img" runat="server" ImageUrl="Images/htc_1.jpg" ImageAlign="Middle" />
- </td>
- </tr>
- <tr>
- <td align="center"><asp:Label runat="server" ID="lblImgHeader"></asp:Label>
- </td>
- </tr>
- </table>
Creating the array with images
- string[] images = System.IO.Directory.GetFiles(Server.MapPath("Images"));
- foreach (string image in images)
- {
- System.IO.FileInfo fi = new FileInfo(image);
- script.Append("image[" + index.ToString() + "] = 'Images/" + fi.Name + "';");
- index++;
- }
- if (images.Length != 0)
- {
- HtmlGenericControl js = new HtmlGenericControl("script");
- js.Attributes.Add("type", "text/javascript");
- // Create a StringBuilder object for the JavaScript
- StringBuilder script = new StringBuilder();
- // Use the setTimeout function to call the function swithImg approx each fourth second.
- script.Append("setTimeout('switchImg()',4000);");
- // Create an array to hold the files an assign the total number if pics
- script.Append("var image = new Array();");
- script.Append("var total = " + images.Length.ToString());
- // The number variable is used to keep track of where we are in the loop
- script.Append("var number = 0");
- // An int to keep track of the array elements
- int index = 0;
- // Loop thru the images in the folder
- // These links could come from a database, as well...
- foreach (string image in images)
- {
- // As we get them from disk, the image string contains of the
- // full file directory path. We use the FileInfo class to change it
- // to map the Images folder in our project.
- System.IO.FileInfo fi = new FileInfo(image);
- script.Append("image[" + index.ToString() + "] = 'Images/" + fi.Name + "';");
- index++;
- }
- // Set the first image to be displayed
- string img_url = "Images/" + new FileInfo(images[0]).Name;
- img.ImageUrl = img_url;
- lblImgHeader.Text = img_url;
- // Create a function displaying each file in the image array
- script.Append("function switchImg() {");
- // Get the image object and the label object
- script.Append("var img = document.getElementById('img');");
- script.Append("var imgHeader = document.getElementById('lblImgHeader');");
- // Increase the current index for the image by one
- script.Append("number = parseInt(number) + 1;");
- // Check wether thats larger than the total amount of images
- script.Append("if(number != total)");
- // If not, display that images url that correspond with the image[index]
- script.Append("{img.src = image[number];}");
- // If number == total, we need to start from scratch again. image [0]
- script.Append("else");
- script.Append("{number = 0; img.src = image[0];}");
- // Make sure to display the current image name in the label
- script.Append("imgHeader.innerHTML = image[number];");
- // Start the switchImg loop by calling setTimeout
- script.Append("setTimeout('switchImg()',4000);");
- script.Append("}");
- // Set the script text
- js.InnerText = script.ToString();
- // Add the control to the header
- Page.Header.Controls.Add(js);
- }
It might be hard to follow, but if you look at the result of the code above it comes much clearer.
- <script type="text/JavaScript">
- setTimeout('switchImg()',4000);
- var image = new Array();
- var total = 4
- var number = 0
- image[0] = 'Images/htc_1.jpg';
- image[1] = 'Images/htc_2.jpg';
- image[2] = 'Images/htc_3.jpg';
- image[3] = 'Images/htc_4.jpg';
- function switchImg()
- {
- var img = document.getElementById('img');
- var imgHeader = document.getElementById('lblImgHeader');
- number = parseInt(number) + 1;
- if(number != total)
- {
- img.src = image[number];
- }
- else
- {
- number = 0;
- img.src = image[0];
- }
- imgHeader.innerHTML = image[number];
- setTimeout('switchImg()',4000);
- }
- </script>

Mahesh ChandPosted Apr 3, 2011, 8:06 PM
Great to see some goodies. Thank for sharing Micke. Nicely written!
Karthikeyan AnbarasanPosted Mar 31, 2011, 12:18 PM
Nice article and well done!!!