Thursday, May 5, 2011

Loading image while waiting

Sometimes we want to show kind of a "waiting" image while a page is been rendered or while we are waiting for a server response.
It's easy, we just need: some css style, a .gif image, a javascript.
Following code is supporting cross-browsing:

The css classes:

.loadingDiv 
{
z-index: 1000;
position:relative;
top:1200px;
float: left;
width:100%;
background-color:White;
opacity: 0.7;
padding-top:300px;
margin-top:-1200px;
}

.loadingImage
{
margin-left: auto;
margin-right:auto;
padding-bottom:600px;
}


Here are the div and the img element. Place them in the top of the page inside the body tag:

<div id="loading" class="loadingDiv"> 
<img id="imgMainLoadImage" src="../images/loading.gif" border="0" alt="" class="loadingImage" />
</div>


Here is the script. Place it just below the div, no matter if you have any other block of scripts in other sections in the page.

<script> 
var ld = (document.all);
var imgL = (document.all);
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
if (ns4) {
ld = document.loading;
imgL = document.imgMainLoadImage;
}
else if (ns6) {
ld = document.getElementById("loading").style;
imgL = document.getElementById("imgMainLoadImage").style;
}
else if (ie4) {
ld = document.all.loading.style;
imgL = document.all.imgMainLoadImage.style;
}

function init() {
if (ns4) {
ld.visibility = "hidden";
}
else if (ns6 || ie4) {
ld.display = "none";
}
}

function ShowLoadingImage() {
if (ns4) {
ld.visibility = "visible";
}
else if (ns6 || ie4) {
ld.display = "block";
}
}
</script>


The last and not least part is to call the javascript function in the "onload" event in the body.
I have used this in masterpages as well as content pages.

<body onload="init();"> 

Wednesday, February 9, 2011

IsNumeric in C#

I can still remember when I used to use the Visual Basic function (was it a function?)
IsNumeric(...)
in which I was able to know if an entry data was or not a numeric value.
Well, we are not going to find such as kind of function in any library in C# (at least until framework 4), so I wrote a quick version in C#.
Here is the utility function:


        private bool IsNumeric(string strEntry) 
{
if (strEntry == null)
{
return false;
}
else
{
Regex obj = new Regex("[^0-9]");
return !obj.IsMatch(strEntry) && (strEntry != string.Empty);
}
}




Don't forget to import the following library:


using System.Text.RegularExpressions; 

Read Simple Text from .txt

Recently I was asked to create a portion on a web application. The basic requirement was to make the app able to read simple text from a text file (.txt) in a configurable location. The path would be located in the web.config and might be both a Web (URL) or physical (windows-style) address. In addition the file may or not exist... so here is the code I wrote:


        private string GetTextFromFile() 
{
string returnValue = string.Empty;

try
{
string path = Convert.ToString(ConfigurationManager.AppSettings["keyTextFile"]);
StringBuilder sb = new StringBuilder();

// make the difference between Url and Windows-style Path
if (path.Contains("http") || path.Contains(Path.AltDirectorySeparatorChar))
{
path = path.TrimEnd('/');
path = sb.Append(path).Append("//").Append("theFile.txt").ToString();

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(path);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
returnValue = reader.ReadToEnd();
}
else
{
path = Path.Combine(path, "theFile.txt");

if (File.Exists(path))
{
using (StreamReader sr = new StreamReader(path))
{
returnValue = sr.ReadToEnd();
}
}
}
}
catch
{
returnValue = string.Empty;
}

return returnValue;
}