473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ValidationControls in a User Control

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




Nov 18 '05 #1
1 1335
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Jonathan" <he**@help.com> wrote in message
news:Xn***********************@206.167.113.5...
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



Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
4
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
0
by: davidr | last post by:
Hi, I have a panel that I load user Control in no problem. The problem arrises when I do a post back on one of these user controls. I have button it does a click event. In this click event I...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.