Ajax with field validator

  • Thread starter Thread starter goscottie
  • Start date Start date
G

goscottie

How do I prevent field validator which resides outside of UpdatePanel
from firing off? The label will update correctly without
RequiredFieldValidator via Ajax. TIA.

Code below....

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:TextBox ID="TextBox1" runat="server"></
asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Required" ControlToValidate="TextBox1">*</
asp:RequiredFieldValidator>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></
asp:Label><br />
<asp:Button ID="Button1"
runat="server" Text="Button" onclick="Button1_Click1" /</ContentTemplate>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>

--==========
Code behind

protected void Button1_Click1(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
 
You can group the fields via the validationgroup property, although this
doesn't exactly make sense because if you're trying to validate something
then it has to be fired with your button that's inside your updatepanel.
Another possibility is to disable the client-side validation on the
requiredfieldvalidator. This is good form anyways as you should never really
perform client-side validation since the validation script can be
circumvented on the browser.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
 
Thanks for replying and sorry for late post.

I resolved it by, like you said, putting and grouping
validationgroup. The sample I gave was for simplicity. My original
requirement was to have two drop down lists and have one fire off and
populate the second one. For example, first one is country and second
is the state. So on selectedindexchanged from first one, second will
populate states based on country selection. Thanks again.
 
Back
Top