472,959 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

DropDownList validation (separate from page)

Liz
I have a page with several dropdownlists, several text boxes and
several buttons which perform calculations. I need to validate one
dropdownlist (not the whole page) with the click of one button. I have
a separate submit button which should validate the other fields.
This page on MSDN does what I want - almost!

http://msdn.microsoft.com/library/de...ationTopic.asp

I added a dropdownlist, customvalidator and another asp:button, but the
validation control does not work when I click on the button. (it works
when I change the value in the dropdown)
Can anyone help please?

Many thanks.

----------------------WEB FORM-----------------------------
<html>
<head>
<script LANGUAGE="javascript">
function isItemSelected(source, arguments){
alert(source,arguments)
if (arguments.Value < 1)
{
arguments.IsValid = false;
}
else
{
arguments.IsValid = true;
}
}
-->

</script>
</head>

<body>

<form RUNAT="server" ID="Form1">

<h3> Button CausesValidation Example </h3>

<table BORDER="1" CELLPADDING="10">
<tr>
<td>
<b>Enter city to query.</b> <br>
<asp:textbox ID="CityTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="CityReqValidator"
CONTROLTOVALIDATE="CityTextBox"
ERRORMESSAGE="<br>Please enter a city."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="CityQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>

<tr>
<td>
<b>Enter state to query.</b> <br>
<asp:textbox ID="StateTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="StateReqValidator"
CONTROLTOVALIDATE="StateTextBox"
ERRORMESSAGE="<br>Please enter a state."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="StateQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>
<tr>
<td><asp:DropDownList id="DropDownList1" runat="server">
<asp:listitem VALUE ="0">Select</asp:listitem>
<asp:listitem VALUE ="1">UK</asp:listitem>
<asp:listitem VALUE ="2">USA</asp:listitem></asp:DropDownList>

<asp:CustomValidator id="CustomValidator1"
runat="server"
ErrorMessage="*"
controltovalidate="DropDownList1"
DISPLAY="Dynamic"
clientvalidationfunction="isItemSelected">
</asp:CustomValidator>
</td>
<td valign="bottom">

<asp:Button id="DropDownButton"
runat="server"
Text="Button"
ONCLICK="DropDownButton_Click"
CAUSESVALIDATION="False"></asp:Button>
</td></tr>

</table>

<br><br>

<asp:label ID="Message"
RUNAT="Server"/>

</form>

</body>
</html>

----------------------CODE BEHIND------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub SubmitButton_Click(ByVal sender As Object, ByVal e As
EventArgs)

' Determine which button was clicked.
Select Case (CType(sender, Button)).ID

Case "CityQueryButton"

' Validate only the controls used for the city query.
CityReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If CityReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following city: " & _
CityTextBox.Text

End If

Case "StateQueryButton"

' Validate only the controls used for the state query.
StateReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If StateReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following state: " & _
StateTextBox.Text

End If

Case "DropDownButton"
CustomValidator1.Validate()

' Take the appropriate action if the controls pass
validation.
If CustomValidator1.IsValid Then

Message.Text = "IsValid"
Else
Message.Text = "InValid"
End If

Case Else

' If the button clicked isn't recognized, erase the
message on the page.
Message.Text = ""

End Select

End Sub

Nov 19 '05 #1
1 6520
It sounds like you want "validation groups", a feature coming in the new
ASP.NET 2.0. It lets you assign a group name to each submit button and the
validators it fires. Please see this thread for details on how you to do it
without ASP.NET 2.0: http://forums.asp.net/817969/ShowPost.aspx

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Liz" <el**************@pigroup.co.uk> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have a page with several dropdownlists, several text boxes and
several buttons which perform calculations. I need to validate one
dropdownlist (not the whole page) with the click of one button. I have
a separate submit button which should validate the other fields.
This page on MSDN does what I want - almost!

http://msdn.microsoft.com/library/de...ationTopic.asp

I added a dropdownlist, customvalidator and another asp:button, but the
validation control does not work when I click on the button. (it works
when I change the value in the dropdown)
Can anyone help please?

Many thanks.

----------------------WEB FORM-----------------------------
<html>
<head>
<script LANGUAGE="javascript">
function isItemSelected(source, arguments){
alert(source,arguments)
if (arguments.Value < 1)
{
arguments.IsValid = false;
}
else
{
arguments.IsValid = true;
}
}
-->

</script>
</head>

<body>

<form RUNAT="server" ID="Form1">

<h3> Button CausesValidation Example </h3>

<table BORDER="1" CELLPADDING="10">
<tr>
<td>
<b>Enter city to query.</b> <br>
<asp:textbox ID="CityTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="CityReqValidator"
CONTROLTOVALIDATE="CityTextBox"
ERRORMESSAGE="<br>Please enter a city."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="CityQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>

<tr>
<td>
<b>Enter state to query.</b> <br>
<asp:textbox ID="StateTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="StateReqValidator"
CONTROLTOVALIDATE="StateTextBox"
ERRORMESSAGE="<br>Please enter a state."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="StateQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>
<tr>
<td><asp:DropDownList id="DropDownList1" runat="server">
<asp:listitem VALUE ="0">Select</asp:listitem>
<asp:listitem VALUE ="1">UK</asp:listitem>
<asp:listitem VALUE ="2">USA</asp:listitem></asp:DropDownList>

<asp:CustomValidator id="CustomValidator1"
runat="server"
ErrorMessage="*"
controltovalidate="DropDownList1"
DISPLAY="Dynamic"
clientvalidationfunction="isItemSelected">
</asp:CustomValidator>
</td>
<td valign="bottom">

<asp:Button id="DropDownButton"
runat="server"
Text="Button"
ONCLICK="DropDownButton_Click"
CAUSESVALIDATION="False"></asp:Button>
</td></tr>

</table>

<br><br>

<asp:label ID="Message"
RUNAT="Server"/>

</form>

</body>
</html>

----------------------CODE BEHIND------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub SubmitButton_Click(ByVal sender As Object, ByVal e As
EventArgs)

' Determine which button was clicked.
Select Case (CType(sender, Button)).ID

Case "CityQueryButton"

' Validate only the controls used for the city query.
CityReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If CityReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following city: " & _
CityTextBox.Text

End If

Case "StateQueryButton"

' Validate only the controls used for the state query.
StateReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If StateReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following state: " & _
StateTextBox.Text

End If

Case "DropDownButton"
CustomValidator1.Validate()

' Take the appropriate action if the controls pass
validation.
If CustomValidator1.IsValid Then

Message.Text = "IsValid"
Else
Message.Text = "InValid"
End If

Case Else

' If the button clicked isn't recognized, erase the
message on the page.
Message.Text = ""

End Select

End Sub

Nov 19 '05 #2

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

Similar topics

2
by: Brennon Arnold | last post by:
I have a problem that I figured would be relatively common, but have been unable to find any information on it as of yet. I have a page that contains two DropDownList controls, with the second...
2
by: Wes Weems | last post by:
I'd like to have a 3 values in a drop down, and have a validator not allow a form submit unless its 2 of the 3 values (eg, one of the values says "select one") I used custom validator and did a...
4
by: Keith | last post by:
I have this simple scenario. -------- Choose an item ->dropdownlist-> or <a>Create a new item</a> -------- When someone decides to click on the create a new item link, it takes them to a...
5
by: Bryan Ax | last post by:
I have a page that has two copies of the same control in it. The control is: BorrowerControl the two references to it in the page are bControl1 cobControl1
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
3
by: Olivier Verdin | last post by:
Hi, I have a page with several Textboxes and several DropDownList. When I click on a 'save' button, it creates a record in a database. The page works fine under Internet Explorer. It does...
6
by: Oscar | last post by:
I want to add items to a dropdownlist control within a Javascript eventhandler. This is what I code : var dd = document.getElementById("DropDownList1"); dd.Items.Add("1990");...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
0
by: RosH | last post by:
Hi Everyone, I have a CreateUserWizard my public sign up page of my website. I have edited the wizard to include collection of other personal information. I have a DropDownList for the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.