samedi 18 avril 2015

How to display an error message within the form

I have the following ASP.net page:



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tks.aspx.cs" Inherits="Dr.Tks" ValidateRequest="true" %>

<asp:TextBox ID="tbComments" ClientIDMode="Static" CssClass="tbTech" runat="server" TextMode="MultiLine" Columns="30" Rows="15"></asp:TextBox>
<asp:Button ID="SubmitForm" ClientIDMode="Static" runat="server" Text="Submit" OnClick="ValidateForm" CssClass="btnFancy orange logBtn btnLogIn lightLinks" UseSubmitBehavior="false" />


C#:



public void ValidateForm(object sender, EventArgs e)
{
try
{
string strTheBody = HttpUtility.HtmlEncode(tbComments.Text);
}
catch (Exception)
{
}
}


If I enter <script... in the textbox above, I get the following error:



Server Error in '/' Application.
--------------------------------------------------------------------------------
A potentially dangerous Request.Form value was detected from the client (tbComments="<script...").


How can I validate the textbox as I type, rather than display the default error message from ASP.net (which is not user friendly)


Aucun commentaire:

Enregistrer un commentaire