Sunday, November 27, 2011

JavaScript: Alert.Show(”message”) from ASP.NET code-behind

using System.Web;
using System.Text;
using System.Web.UI;

///
/// A JavaScript alert
///

public static class Alert
{

///
/// Shows a client-side JavaScript alert in the browser.
///

/// The message to appear in the alert.
public static void Show(string message)
{
// Cleans the message to allow single quotation marks
string cleanMessage = message.Replace("'", "\\'");
string script = "";

// Gets the executing web page
Page page = HttpContext.Current.CurrentHandler as Page;

// Checks if the handler is a Page and that the script isn't allready on the Page
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
}
}
}
Demonstration

That class of only 30 lines of code enables us to add a JavaScript alert to any page at any time. Here is an example of a Button.Click event handler that uses the method for displaying status messages.

void btnSave_Click(object sender, EventArgs e)
{
try
{
SaveSomething();
Alert.Show("You document has been saved");
}
catch (ReadOnlyException)
{
Alert.Show("You do not have write permission to this file");
}
}

Tuesday, November 22, 2011

Upload file in asp.net

public string UploadImage(FileUpload image, int height, int width)
{
string strProfilePicture = "";

if (image.HasFile == true)
{
string ext = System.IO.Path.GetExtension(image.FileName).ToLower();

string[] allowedtypes = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".JPEG", ".jpg", ".bmp" };

if (allowedtypes.Contains(ext))
{
System.Drawing.Image userimage = System.Drawing.Image.FromStream(image.PostedFile.InputStream);

if (userimage.Height <= height || userimage.Width <= width)
{
if (image.PostedFile.ContentLength <= 512000)
{
string[] mimetypes = new string[] { "image/png", "image/jpeg", "image/pjpeg", "image/gif", "image/bmp" };
string filetype = image.PostedFile.ContentType;
if (mimetypes.Contains(filetype) == false)
{
//lblMassage.Text = "Image is not in the correct format, Please ensure the image is either a JPEG, GIF or PNG";
// return;
}
else
{
string filename = Path.GetFileName(image.FileName);
filename = "PP" + DateTime.Now + ext.ToString();
filename = filename.Replace(":", "");
filename = filename.Replace(" ", "");
filename = filename.Replace("/", "");

image.SaveAs(Server.MapPath("~/UserProfileImage/") + filename);



string strpath = "~/UserProfileImage/" + filename;
strProfilePicture = strpath;

}
}
else
{
//lblMassage.Text = "File is larger than 250kb. Please reduce the Size";
// return;
}
}
else
{
//lblMassage.Text = "The Image is larger than " + width + "x" + height + "px. Please Resize it";
//return;
}




userimage.Dispose();
//
}

}

else if (image.HasFile == false)
{

}

else
{
// error = "Image is not in the correct format, Please ensure the image is either a JPEG, GIF or PNG";
//return;
}


//txtError.Text = error;
//txtSuccessMessage.Text = success;
return strProfilePicture;

}

Warning message in asp.net gridview

write this code block within a span
onclick="return confirm('Are you sure to Delete?')"