Hi,
In this article I’ll start a list a few Common Errors about ASP.NET Developers are confronted and give solutions.
1 - Are you missing a using directive or an assembly reference ?
When using Inline Code in ASPX Page we often got this Exception. This Exception occurs when you don’t Import Namespace as you do with “using” statement in Code-Behind.
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'string' does not contain a definition for 'RemoveExtraHyphen' and no extension method 'RemoveExtraHyphen' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 9: <form id="form1" runat="server">
Line 10: <div>
Line 11: <%= "Hello--World--!".RemoveExtraHyphen()%>
Line 12: </div>
Line 13: </form>
Solution : Add a Namespace Reference in the ASPX Page :
<%@ Import Namespace="Sb2.Core.Extensions" %>
2 - A potentially dangerous Request.Form value was detected from the client
This Exception occurs when “EnableEventValidation” directive in WebForm is set as “True” and when you tries to POST some potentially dangerous values.
Server Error in '/' Application.
A potentially dangerous Request.Form value was detected from the client (TextBox1="<!=").
Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. You can disable request validation by setting validateRequest=false in the Page directive or in the configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case.
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (TextBox1="<!=").
Source Error:
Solution : Set EnableEventValidation directive a “False” in WebPage or in Web.config
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sb2.Web.Tests._Default" %>
<pages enableEventValidation="false">
3 – Usage of IsPostBack
Now a pretty common mistake when working with ASP.NET …
Assuming the following ASP.NET WebForm
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sb2.Web.Tests._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
<asp:Button ID="btSave" runat="server" Text="Save" onclick="btSave_Click" />
</div>
</form>
</body>
</html>
And the Associated Code-Behind
[Serializable]
public class Customer
{
public String Email { get; set; }
}
public partial class _Default : Page
{
public Customer CurrentCustomer
{
get { return ViewState["Customer"] as Customer; }
set { ViewState["Customer"] = value; }
}
protected override void OnLoad(EventArgs e)
{
// Load Customer Data
LoadData();
base.OnLoad(e);
}
private void LoadData()
{
if (CurrentCustomer != null)
{
this.txtEmail.Text = CurrentCustomer.Email;
}
}
protected void btSave_Click(object sender, EventArgs e)
{
if (CurrentCustomer == null)
{
CurrentCustomer = new Customer();
}
CurrentCustomer.Email = this.txtEmail.Text;
// Reload Data
LoadData();
}
}
What I do here ?
- On Page Load, i retrieved the Customer Data (Email Address) and Display it in my WebForm TextBox.
- I modify the Email Address on the Web Page and I Click “Save” button and then Reload Data in order to verify my changes
- BUT I saw that the Email Address is always the previous value and not my modified value .. WHY ?
When passing in Debug Mode we see that the PageLoad is called before Enter the Button Click Event, so the Email Address is replaced by the Previous Value of Customer.Email and then when Button Click Event fired it saves the previous value again ..
Solution : Just Modify the OnLoad method like that
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
{
// Load Customer Data
LoadData();
}
base.OnLoad(e);
}
By this, when we are in a PostBack Event (Save Button Click), we don’t call LoadData on PageLoad ! So we keep the modified value in the TextBox.
Hope this help’s!
Views(1779)

