ValidationControls in a User Control

J

Jonathan

Hi everyone,
I am new to asp.net and here is my problem. I create a UserControl
included in my main file (index.aspx). Depending on witch parameter i
have pass, i "execute" a UserControl part with a SelectCase.
Here's my code:

/***************************
index.aspx
***************************/
<%@ Register TagPrefix="Acme" TagName="Content" Src="Content.ascx" %>
<body>
<form method="post" runat="server">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<a href="?link=mylink1">Text</a>
</td>
</tr>
<tr>
<td>
<a href="?link=mylink2">Text2</a>
</td>
</tr>
<tr>
<td><Acme:Content runat="server"/></td>
</tr>
<tr>
<td>Some text here</td>
</tr>
</table>
</form>
</body>

/***************************
my user control (Content.ascx)
***************************/
<script language="VB">
Sub Register_click( ByVal Sender as Object, ByVal e as
ImageClickEventArgs )
If Page.IsValid
/*execute some code here*/
End If
End Sub
</script>

<table border="0" cellpadding="0" cellspacing="0">
<%select case request.querystring("link")%>
<%case "mylink1"%>
<tr>
<td><asp:textbox id="MyField1" runat="server" /></td>
<td><asp:RequiredFieldValidator ControlToValidate="MyField1"
Display="Dynamic" errormessage="Required!" runat="server" />
</td>
</tr>
<%case "mylink2"%>
<tr>
<td><asp:textbox id="MyField2" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ControlToValidate="MyField2"
Display="Dynamic" errormessage="Required!" runat="server" />
</td>
</tr>
<tr>
<asp:imageButton id="Register"
ImageURL="./Image.gif" onclick="Register_click"
runat="server"></asp:imageButton>
</tr>
<%end select%>
</table>

For example, if i select the hyperlink "Text2", "MyField2" will be shown
with the "Register" button. If i click on it, it seems as if both
FieldValidators are executed because the page won't ever be validated.
Can somebody help me up with that?

Plus, i think my UserControl ain't the real appropriate way of doing what
i am doing (Multiple links on my index.aspx page that change a cells
content). Any other suggestions (ex: include a file like old Asp,
CustomControl, etc.)????

Thanks a lot in advance!

Jonathan
 
P

Peter Blum

Hi Jonathan,

You have a situation where you want validators to fire based on different
submit buttons/links. ASP.NET doesn't handle this situation automatically.
You have to use a clever hack that turns off client-side validation:

1. Set EnableClientScript=false on each validator.
2. Set CausesValidation=false on each button that submits the page. This
prevents the Page.Validate() method from begin called automatically by the
button.
3. In your click event method, you will call the Validate() method on each
validator that is associated with the submit button. Then you will test the
IsValid property to be true on each validator before saving.

ASP.NET 2.0 will introduce a better solution called "Validation groups". It
allows you to assign a group name to the button and the validators it fires.
It will work with client-side validation. Since ASP.NET 2.0 is due in the
first half of 2005, this is probably not a good option.

My "Professional Validation And More"
(http://www.peterblum.com/vam/home.aspx) is a replacement to Microsoft's
validators that overcomes numerous limitations and greatly expands the
concept of validation. It includes validation groups today. It also provides
client-side validation on far more browsers than Microsoft, which only
supports IE. Even if you are not interested in switching (for which I
provide a conversion utility), I've put together a list of the limitations
I've found here: http://www.peterblum.com/vam/valmain.aspx. Use it to better
plan your validation effort.

--- 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