server side validation in form using panel controls

A

amessimon

Hi

I have a long form - around 80 fields - which i have decided to break up
into
more manageable sections using the <asp:panel> control.

Basically i have three panels (stages) containing various form elements
which i show
and as the user progresses through the form.

I need to use server side validation (client side is not an option) on some
of the fields in the form
dive used an <asp:RequiredFieldValidator> as an example here.

I cant seem to get the validation to take effect - you'll see the
<asp:RequiredFieldValidator> in <asp:panel Id="stage1">
as an example - if the user doesn't enter a value of some kind then i'd like
the same stage returned
not allowing the user to bypass it.

Thanks in advance...

Simon Ames

Here's the code.......

<script language="vb" runat="server">
Sub Page_load
fnHideAllPanels
If not IsPostback then
' show stage 1 on loading page for first time
stage1.Visible = True
End If
End Sub

Sub Button_Click (sender As System.Object, e As
system.Web.UI.WebControls.CommandEventArgs)
If e.CommandName = "move_to_stage_2" Then
If page.isvalid then
' if user has entered a value
stage2.Visible = True
Else
' if field is empty
stage1.Visible = True
End If
ElseIf e.CommandName = "move_to_stage_3" Then
stage3.Visible = True
ElseIf e.CommandName = "move_to_stage_1" Then
stage1.Visible = True
End If
End Sub

Function fnHideAllPanels
stage1.Visible = False
stage2.Visible = False
stage3.Visible = False
End Function
</script>

<form runat="server">
<asp:panel Id="stage1" RunAt="server" Wrap="">
<h2>Stage One</h2>
<asp:RequiredFieldValidator Id="val_username" RunAt="server"
ControlToValidate="username" EnableClientScript="false" Text="enter a
username" />
<asp:textbox id="username" runat="server"/> * required
<asp:Button Id="move_to_stage_2" CommandName="move_to_stage_2"
OnCommand="button_click" RunAt="server" Text="next"/>
</asp:panel>

<asp:panel Id="stage2" RunAt="server" Wrap="">
<h2>Stage Two</h2>
<asp:Button Id="move_to_stage_3" CommandName="move_to_stage_3"
OnCommand="button_click" RunAt="server" Text="next"/>
</asp:panel>

<asp:panel Id="stage3" RunAt="server" Wrap="">
<h2>Stage Three</h2>
<asp:Button Id="move_to_stage_1" CommandName="move_to_stage_1"
OnCommand="button_click" RunAt="server" Text="next"/>
</asp:panel>
</form>
 
A

amessimon

Hi Peter

I'm pretty new to .NET and probably misunderstanding your instruction.

Do i test for the page.isvalid condition in the Button_Click subroutine or
somewhere else?
An example would be handy if you could.

Also would this involve using setting EnableClientScript="true" in the
<asp:RequiredFieldValidator> control. I wanted to avoid performing the
validation client side if i could.

Thanks in advance
 
P

Peter Blum

Test Page.IsValid within your button click subroutine. Button_Click
subroutines (or "post back event handlers") are always called on post back.
You still have to check the conditions of your page before you take any
action within them.

The .net documentation on the Page.IsValid property shows an example. If you
don't have the docs, start at msdn.microsoft.com/library.

This problem has nothing to do with the client side. So you can leave
EnableClientScript=false if you like.

The server side Button class automatically invokes validation through
Page.Validate() for you. If you set its CausesValidation property to false
or do not get a post back due to a button, you must manually call
Page.Validate() on post back, before checking Page.IsValid.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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