I am trying to use ASP.NET wit C# for form validation. The problem is that I cannot seem to get any validation controls to prevent anything. I can get them to show the validation text, but it does not seem to prevent the form from "submitting". I have seen exmples in books and online showing how the validation controls should prevent something from happening... usually an OnClick event associated with the button. But, when I try them the event always happens, even if though the validation error may show up. I have never got an example to work right, even when I copy and paste the code, could there be an IIS or web.config problem? Maybe I am misunderstanding what these controls should do. I have an example below of a test form... when I click either button, both feedback labels change. How can I check for validation?
<%@ Page Language="C#" Debug="true" ContentType="text/html" ResponseEncoding="iso-8859
-1" %>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if (IsPostBack && SomeMoreFeedback.Text=="on
e") {
SomeMoreFeedback.Text="the
other";
} else{
SomeMoreFeedback.Text="one
";
}
}
</script>
<script runat="server">
void btnSubmit_OnClick(Object Src, EventArgs E)
{
SomeFeedback.Text="Button 1 Clicked";
}
void btnSubmit2_OnClick(Object Src, EventArgs E)
{
SomeFeedback.Text="Button 2 Clicked";
}
</script>
<head>
<title>Validation Test</title>
</head>
<body>
<form name="testform" id="testform" runat="server">
<asp:textbox ID="TestBox" runat="server" TextMode="SingleLine"/>
<asp:requiredfieldvalidato
r ControlToValidate="TestBox
" ErrorMessage="Required!!!"
ID="valName" runat="server" />
<asp:button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_OnClick
"/>
<asp:button ID="btnSubmit2" runat="server" Text="Submit" OnClick="btnSubmit2_OnClic
k"/>
</form>
<asp:label ID="SomeFeedback" runat="server" Text="" Visible="true"></asp:label
><br />
<asp:label ID="SomeMoreFeedback" runat="server" Text="Form Not Submitted" Visible="true"></asp:label
>
</body>
</html>
Start Free Trial