Error Handling with asp.net

HellHello firends today u will learn how can u make enumeration and handle ervery page error or exception with handsome way.

In this article you will learn use of css and web user control

Step1 make a new web site and add a web user control in that

and write down given code into source of control or design view

<style type=”text/css”>
body
{
font-family: Arial, Helvetica, sans-serif;
font-size: 13px;
}
.info, .success, .warning, .error, .validation
{
border: 1px solid;
margin: 10px 0px;
background-repeat: no-repeat;
background-position: 10px center;
}
.info
{
color: #00529B;
background-color: #BDE5F8;
background-image: url(‘images/info.png’);
}
.success
{
color: #4F8A10;
background-color: #DFF2BF;
background-image: url(‘images/success.png’);
}
.warning
{
color: #9F6000;
background-color: #FEEFB3;
background-image: url(‘images/warning.png’);
}
.error
{
color: #D8000C;
background-color: #FFBABA;
background-image: url(‘images/error.png’);
}
.ServerError
{
color: #D8000C;
background-color: #FFDEAD;
background-image: url(‘images/server.gif’);
}
.container
{
}
.info p, .success p, .warning p, .error p
{
padding: 0px 50px;
}
.info a, .success a, .warning a, .error a
{
float: right;
padding: 10px;
cursor: pointer;
}
.container img
{
border: none;
}
</style>
<body>
<div>
<asp:Panel ID=”MessageBox” runat=”server”>
<asp:HyperLink runat=”server” ID=”CloseButton”>
<asp:Image ID=”Image1″ runat=”server” ImageUrl=”~/images/close_icon.gif” AlternateText=”Click here to close this message” />
</asp:HyperLink>
<p>
<asp:Literal ID=”litMessage” runat=”server”></asp:Literal></p>
</asp:Panel>
</div>
</body>

Step 2: now friends add add given below image into image folder these can be download from google.

Step 3:   Now write these line of code into code behind file of web user control

public partial class WebUserControl : System.Web.UI.UserControl
{

#region Properties
public bool ShowCloseButton { get; set; }
#endregion
#region Load
protected void Page_Load(object sender, EventArgs e)
{
if (ShowCloseButton)
CloseButton.Attributes.Add(“onclick”, “document.getElementById(‘” +
MessageBox.ClientID + “‘).style.display = ‘none'”);
}
#endregion
#region Wrapper methods
public void ShowError(string message)
{
Show(MessageType.Error, message);
}
public void ShowInfo(string message)
{
Show(MessageType.Info, message);
}
public void ShowSuccess(string message)
{
Show(MessageType.Success, message);
}
public void ShowWarning(string message)
{
Show(MessageType.Warning, message);
}
public void ShowServerError(string message)
{
Show(MessageType.Warning, message);
}
#endregion
#region Show control
public void Show(MessageType messageType, string message)
{
CloseButton.Visible = ShowCloseButton;
litMessage.Text = message;
MessageBox.CssClass = messageType.ToString().ToLower();
this.Visible = true;
}
#endregion
#region Enum
public enum MessageType
{
Error = 1,
Info = 2,
Success = 3,
Warning = 4,
ServerError=5

}
#endregion

}

Now drag your web user control on default .aspx page and write these line of code into page load

WebUserControl1.ShowCloseButton = true;
WebUserControl2.ShowCloseButton = true;
WebUserControl3.ShowCloseButton = true;
WebUserControl4.ShowCloseButton = true;
WebUserControl1.ShowInfo(“This is an information. This MessageBox works!”);
WebUserControl2.Show(WebUserControl.MessageType.Success, “Your profile has been changed”);
WebUserControl3.ShowError(“This is an error message”);
WebUserControl4.ShowWarning(“This is a warning!”);
WebUserControl5.ShowServerError(“server error”);
WebUserControl5.ShowCloseButton = true;

and Run your page and get result ,I hope you will understand to this and will be helpfull for you in your projects
ouput will be like this

Be happy in programming world.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s