Page Validation and Submit problems in ASP.NET

A

Arun K

Hi,

I am creating a simple .aspx page to add some fields with validation.

I have used different .NET validations like REquiredFieldValidator,
RegularExpressionValidator and showed the summary in the bulleted list
on top. I have 3 text boxes, and two RadioButtonList. and 3 buttons. One
for Submit, Reset and Exit.

If submit is pressed page should be validated and submit (insert into
DB) , Reset- page shoule be re-loaded, Exit - go to next page.

I have tried many options and i don't know which way is the best one.

a) when i submit the page .. all validatations are working perfect, but
how should i reset the page ? (again it is doing all the validations ).
I have used javascript too but for that i remove <%@ Page Language="vb"
AutoEventWireup="false" Codebehind="AddUsers.aspx.vb"%> then only it
works. But i have to use code behind and if remove this line for sake of
JS i cannot use code behind.

b) then i thought of using my own validations in function and for that i
believe if i use <asp:textbox and on click if of submit if i write
txtFullName.value does not work .. again i need to change every tag to
input type .. is this the only way.

Please suggest me the best way.

Code I am using is as follows :-


<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="AddUsers.aspx.vb"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language="VB" runat="server">
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Response.write ("Arun Submit")
End Sub

Sub ResetBtn_Click(sender As Object, e As EventArgs)
Response.write ("Arun Reset")

End Sub

</script>


<title>AddModSysUsers</title>
<link href="incStyles.css" rel="stylesheet">
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table border="1" cellpadding="0" cellspacing="4" width="100%"
align="center">
<tr>
<td colspan="2" class="clsFont" align="middle">
<asp:panel runat="server" id="ValidationPanel" visible="false">
<asp:ValidationSummary ID="validSummary" runat="server"
HeaderText="Check the following fields:" DisplayMode="BulletList" />
</asp:panel>
</td>
</tr>
<tr>
<td colspan="2" align="middle" class="clsFont" height="30">
<font color="red">*</font>&nbsp;<B>Required</B>
</td>
</tr>
<tr>
<td class="clsFont" align="right" width="50%">
<font color="red">*</font>&nbsp;<B>Full Name(last, first) :</B>
&nbsp;
</td>
<td class="clsFont" align="left">
&nbsp;<asp:TextBox id="txtFullName" runat="server" Columns="30"
MaxLength="20"></asp:TextBox>
<asp:RequiredFieldValidator id="ValidateFullName"
ControlToValidate="txtFullName" ErrorMessage="Full Name must be filled
in" Display="None" runat="server"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="clsFont" align="right" width="50%">
<font color="red">*</font>&nbsp;<B>User Name (8-15 characters)
:</B> &nbsp;
</td>
<td class="clsFont" align="left">
&nbsp;<asp:TextBox id="txtUserName" runat="server" Columns="30"
MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator id="validateUserName"
ControlToValidate="txtUserName" ErrorMessage="User Name must be filled
in" Display="None" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="ValRegUserName"
ControlToValidate="txtUserName" ErrorMessage="User Name Should be in
8-15 Characters." ValidationExpression=".{8,15}" Display="None"
runat="server"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="clsFont" align="right">
<font color="red">*</font>&nbsp;<B>Password (8-15 characters)
:</B> &nbsp;
</td>
<td class="clsFont" align="left">
&nbsp;<asp:TextBox id="txtPassword" runat="server" Columns="30"
MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator id="ValidatePassword"
ControlToValidate="txtPassword" ErrorMessage="Password must be filled
in" Display="None" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="ValRegPassword" runat="server"
ControlToValidate="txtPassword" ErrorMessage="Password Should be in 8-15
Characters." ValidationExpression=".{8,15}"
Display="None"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="clsFont" align="right">
<font color="red">*</font>&nbsp;<B>User Level :</B> &nbsp;
</td>
<td class="clsFont" align="left">
<ASP:RadioButtonList id="radUserLevel" RepeatLayout="Table"
runat="server" RepeatDirection="Horizontal" CssClass="clsFont">
<asp:ListItem>General</asp:ListItem>
<asp:ListItem>Omega</asp:ListItem>
<asp:ListItem>Super</asp:ListItem>
</ASP:RadioButtonList>
<asp:RequiredFieldValidator id="ValidateUserLevel" runat="server"
ControlToValidate="radUserLevel" ErrorMessage="User Level must be
selected. " Display="None" InitialValue=""></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="clsFont" align="right">
<font color="red">*</font>&nbsp;<B>Status :</B> &nbsp;
</td>
<td class="clsFont" align="left">
<ASP:RadioButtonList id="radStatus" RepeatLayout="Table"
runat="server" RepeatDirection="Horizontal" CssClass="clsFont">
<asp:ListItem>Active&nbsp;&nbsp;</asp:ListItem>
<asp:ListItem>Inactive</asp:ListItem>
</ASP:RadioButtonList>
<asp:RequiredFieldValidator id="ValidateStatus" runat="server"
ControlToValidate="radStatus" ErrorMessage="Status must be selected. "
Display="None" InitialValue=""></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td align="middle" colspan="2">
&nbsp;&nbsp;
<asp:Button id="btnSubmit" Runat="server" text="Submit" OnClick =
"SubmitBtn_Click"></asp:Button>
&nbsp;&nbsp;
<asp:Button id="btnReset" runat="server" text="Reset" OnClick =
"ResetBtn_Click" ></asp:Button>
&nbsp;&nbsp;
<input type = "reset" id="Button1" runat="server" value="reset">
&nbsp;&nbsp;
<asp:Button id="btnExit" Runat="server" text="Exit Without
Saving"></asp:Button>
</td>
</tr>
</table>
</form>
</body>
</HTML>
 
H

Hermit Dave

i had a similar issue when i was doing some stuff earlier.... i just moved
to server validation only...
but i know its not a perfect solution... here is what i am going to try
use registerclientscriptblock to register a javascript funcation say
'CancelMe( )'
all it does is history.back( )
and associate that function with onclick on the cancel button.
The problem is that we get so used to server side handling of events that we
forget the basic thing... ie client side validation is executed when you
post that form.... no matter what... ie if you want to cancel something...
dont try to submit the form... rather just bypass it...

hope this help....

HD

PS: Just gave it a try and it works... though instead of LinkButton you have
to settle for Hyperlink object

on aspx page
<td colspan="3">
<asp:LinkButton id="lnkUpdate"
runat="server">Update</asp:LinkButton>&nbsp;&nbsp;
<asp:HyperLink id="lnkCancel" onclick='CancelMe();' runat="server"
NavigateUrl="#">Cancel</asp:HyperLink>
</td>

in code behind .cs file within Page

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
// Form the script that is to be registered at client side.
String scriptString = "<script language=JavaScript> function CancelMe()
{";
scriptString += "window.history.back(); }</script>";

if(!this.IsClientScriptBlockRegistered("clientScriptCancel"))
this.RegisterClientScriptBlock("clientScriptCancel", scriptString);

if(!Page.IsPostBack)
...............
 
P

Peter Blum

If I understand you correctly, you are attempting to allow validation when
the Submit button is pressed but prevent it when Reset or Exit are pressed.
Buttons have a property called CausesValidation. Set it to false on the
Reset and Exit buttons.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top