ASP.Net JQuery customvalidator on multiple fields - c#

I am trying to do validation on multiple fields with the custom validator in ASP.net using JQuery but I can't get it to work.
There is a drop down box which contains two values, based on these values other items appear.
So if the user selects option 1, then a text box appears, if they select option 2 the text box disappears and two drop down boxes appear.
Depending on what option they choose I want to validate their input.
What I have so far that is not working is
.ASPX
<asp:DropDownList ID="drpHeight" runat="server">
<asp:ListItem Value="CMs">CMs</asp:ListItem>
<asp:ListItem Value="Feet">Feet</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtCm" runat="server" Width="100px"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height"
ClientValidationFunction = "ValidateHeight" Display="Dynamic" ControlToValidate="txtCm" ValidateEmptyText="true"></asp:CustomValidator>
<asp:RegularExpressionValidator ID="revCm" runat="server"
ControlToValidate="txtCm" Display="Dynamic"
ErrorMessage="Please enter a valid number"
ValidationExpression="^([0-9]*|\d*\.\d{1}?\d*)$" SetFocusOnError="True"></asp:RegularExpressionValidator>
<br />
<br />
<asp:DropDownList ID="drpFeet" runat="server">
<asp:ListItem Value="-1">Select...</asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
<asp:ListItem Value="5"></asp:ListItem>
<asp:ListItem Value="6"></asp:ListItem>
<asp:ListItem Value="7"></asp:ListItem>
<asp:ListItem Value="8"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblFeet" runat="server" Text="Ft"></asp:Label>
<asp:DropDownList ID="drpInches" runat="server">
<asp:ListItem Value="-1">Select...</asp:ListItem>
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
<asp:ListItem Value="5"></asp:ListItem>
<asp:ListItem Value="6"></asp:ListItem>
<asp:ListItem Value="7"></asp:ListItem>
<asp:ListItem Value="8"></asp:ListItem>
<asp:ListItem Value="9"></asp:ListItem>
<asp:ListItem Value="10"></asp:ListItem>
<asp:ListItem Value="11"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblInches" runat="server" Text="Inches"></asp:Label>
<br />
<br />
JQuery
function ValidateHeight(sender, args) {
if ($('#<%=drpHeight.ClientID%>').val = "CMs") {
if ($('#<%=txtCm.ClientID%>').val().length > 0) {
args.IsValid = true;
}
else {
args.IsValid = false;
return;
}
}
else if ($('#<%=drpHeight.ClientID%>').val = "Feet") {
args.IsValid = true;
}
}
</script>
At the moment it is not working, I did have it validating the CM b but then it stopped working and now I can't figure out why.
What is the best way to approach this? I've got another one for Weight to do as well, that one has 3 input possibilities.

I came across the answer finally, I will provide the answer for anyone else who wants to know how to do this.
Changed my customer validator to:
<asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height"
ClientValidationFunction = "ValidateHeight" Display="Dynamic" ValidateEmptyText="true"></asp:CustomValidator>
Then my JQuery became
<script type="text/javascript">
function ValidateHeight(sender, args) {
var drpHeight = $('#<%=drpHeight.ClientID%>').val();
if (drpHeight == "CMs") {
if ($('#<%=txtCm.ClientID%>').val().length > 0) {
args.IsValid = true;
return;
}
else {
args.IsValid = false;
return;
}
}
else if (drpHeight == "Feet") {
var drpFeet = $('#<%=drpFeet.ClientID%>').val();
var drpInches = $('#<%=drpInches.ClientID%>').val();
if (drpFeet == -1 || drpInches == -1) {
args.IsValid = false;
return;
}
else {
args.IsValid = true;
return;
}
}
}
</script>

Related

Select multiple value in ListBox using ASP.NET and C#

Why "SelectedValue" selects only the first value in my multiple selection mode ?
<dt>Tags:</dt><dd>
<asp:ListBox ID="ListTag" runat="server" SelectionMode="Multiple"
DataSourceID="SqlDataSourceTag" DataTextField="tag_name"
DataValueField="tag_id">
</asp:ListBox>
<asp:SqlDataSource ID="SqlDataSourceTag" runat="server"
ConnectionString="<%$ ConnectionStrings:db_cc %>">
</asp:SqlDataSource>
</dd>
ListTag.SelectedValue = "Tag1"; <-- only this is selects
ListTag.SelectedValue = "Tag2";
in your asp page you have this.
<asp:ListBox ID="ListTag" runat="server" AutoPostBack="True" SelectionMode="Multiple" Width="250px">
<asp:ListItem Selected="False" Text="Tag-1" Value="1">Tag - 1</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-2" Value="2">Tag - 2</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-3" Value="3">Tag - 3</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-4" Value="4">Tag - 4</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-5" Value="5">Tag - 5</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-6" Value="6">Tag - 6</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-7" Value="7">Tag - 7</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-8" Value="8">Tag - 8</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-9" Value="9">Tag - 9</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-10" Value="10">Tag - 10</asp:ListItem>
</asp:ListBox>
then in your cs(Code Behind) in the event that you want you have this.
for (int i = 0; i < lsBox.Items.Count; i++)
{
var item = lsBox.Items[i];
string[] selectecvalues = new string[] { "1", "3", "6", "9" };
if (selectecvalues.Contains(item.Value)) // or item.Text if you like
{
lsBox.Items[i].Selected = true;
}
}
that will result in Tags 1,3,6,9 selectd

Error in selection change of item in DropdownList

In ASP.NET project I have to load the content( TextBlock,Buttons etc) based on item selction
<asp:DropDownList ID="ddl_FieldType" CssClass="form-control" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_FieldType_SelectedIndexChanged">
<asp:ListItem Text="Single Line Text" Value="0"></asp:ListItem>
<asp:ListItem Text="Multiple Line Text" Value="5"></asp:ListItem>
<asp:ListItem Text="Date Time" Value="2"></asp:ListItem>
<asp:ListItem Text="Date Only" Value="4"></asp:ListItem>
<asp:ListItem Text="Single Select" Value="1"></asp:ListItem>
<asp:ListItem Text="Multiple Select" Value="6"></asp:ListItem>
<asp:ListItem Text="Yes / No" Value="3"></asp:ListItem>
</asp:DropDownList>
I faced error while changing item selection related to postback or callback:
I would probably try to achieve this with javascript.
Something like this should work:
<asp:DropDownList ID="ddl_FieldType" CssClass="form-control" runat="server" Onchange=findselected()>
<asp:ListItem Text="Single Line Text" Value="0"></asp:ListItem>
<asp:ListItem Text="Multiple Line Text" Value="5"></asp:ListItem>
<asp:ListItem Text="Date Time" Value="2"></asp:ListItem>
<asp:ListItem Text="Date Only" Value="4"></asp:ListItem>
<asp:ListItem Text="Single Select" Value="1"></asp:ListItem>
<asp:ListItem Text="Multiple Select" Value="6"></asp:ListItem>
<asp:ListItem Text="Yes / No" Value="3"></asp:ListItem>
<script type="text/javascript">
function findselected() {
var selMenu = document.getElementById('<%=DropDownList1.ClientID%>');
var someField = document.getElementById('<%=TB1.ClientID%>');
var anotherField = document.getElementById('label1');
var btnField= document.getElementById('<%=ExportBtn.ClientID%>');
if (selMenu.value == '2' || selMenu.value =='3') {
someField.style.display = "none";
anotherField.style.display = "none";
btnField.style.display = "none";
}
else {
someField.style.display = "";
anotherField.style.display = "";
btnField.style.display = "";
}
}
</script>
Hope this helps,
Apex

C# asp.net widget with postback defaults to login even with web.config excluding it. Work arounds?

I have an input widget "subscribeContact.aspx" I have written in c#, asp.net.
It is excluded from authorization with an entry in "web.config". It defaults to displaying the login page. If the screen is refreshed (e.g. F5), the widget appears without the necessity of logging in.
The routine "WebinarDisplay.aspx" also includes a postback function, but does not display the same behavior.
web.config entry:
<location path="~/subscribeContact.aspx">
<system.web>
<authorization>
<allow users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="~/WebinarDisplay.aspx">
<system.web>
<authorization>
<allow users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
The source for the page is:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="subscribeContact.aspx.cs" Inherits="SchedulerServices.subscribeContact" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="inputDiv" runat="server">
<asp:Label ID="allFieldsRequired" runat="server" Text="All Fields are Required." Visible="true" Font-Size="Small"></asp:Label>
<asp:Label ID="selectedFieldsRequired" runat="server" Text="Fields Marked in red are Required." Visible="false" Font-Size="Small"></asp:Label>
<br />
<br />
<asp:Label ID="firstNameLabel" runat="server" Text="First Name" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="firstNameBox" runat="server" Font-Size="Small"></asp:TextBox>
<br />
<asp:Label ID="lastNameLabel" runat="server" Text="Last Name" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="lastNameBox" runat="server" Font-Size="Small"></asp:TextBox>
<br />
<asp:Label ID="emailLabel" runat="server" Text="Email Address" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="emailAddressBox" runat="server" Font-Size="Small" TextMode="Email"></asp:TextBox>
<br />
<asp:Label ID="phoneNumberLabel" runat="server" Text="Phone Number" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="phoneNumberBox" runat="server" Font-Size="Small" TextMode="Phone"></asp:TextBox>
<br />
<asp:Label ID="stateLabel" runat="server" Text="State" Font-Size="Small"></asp:Label>
<br />
<asp:DropDownList ID="stateDDL" runat="server" Font-Size="Small" >
<asp:ListItem Value="" Text="Select a state" Selected="True"></asp:ListItem>
<asp:ListItem Value="AL" Text="Alabama" ></asp:ListItem>
<asp:ListItem Value="AK" Text="Alaska" ></asp:ListItem>
<asp:ListItem Value="AZ" Text="Arizona" ></asp:ListItem>
<asp:ListItem Value="AR" Text="Arkansas" ></asp:ListItem>
<asp:ListItem Value="CA" Text="California" ></asp:ListItem>
<asp:ListItem Value="CO" Text="Colorado" ></asp:ListItem>
<asp:ListItem Value="CT" Text="Connecticut" ></asp:ListItem>
<asp:ListItem Value="DE" Text="Delaware" ></asp:ListItem>
<asp:ListItem Value="DC" Text="District of Columbia" ></asp:ListItem>
<asp:ListItem Value="FL" Text="Florida" ></asp:ListItem>
<asp:ListItem Value="GA" Text="Georgia" ></asp:ListItem>
<asp:ListItem Value="HI" Text="Hawaii" ></asp:ListItem>
<asp:ListItem Value="ID" Text="Idaho" ></asp:ListItem>
<asp:ListItem Value="IL" Text="Illinois" ></asp:ListItem>
<asp:ListItem Value="IN" Text="Indiana" ></asp:ListItem>
<asp:ListItem Value="IA" Text="Iowa" ></asp:ListItem>
<asp:ListItem Value="KS" Text="Kansas" ></asp:ListItem>
<asp:ListItem Value="KY" Text="Kentucky" ></asp:ListItem>
<asp:ListItem Value="LA" Text="Louisiana" ></asp:ListItem>
<asp:ListItem Value="ME" Text="Maine" ></asp:ListItem>
<asp:ListItem Value="MD" Text="Maryland" ></asp:ListItem>
<asp:ListItem Value="MA" Text="Massachusetts" ></asp:ListItem>
<asp:ListItem Value="MI" Text="Michigan" ></asp:ListItem>
<asp:ListItem Value="MN" Text="Minnesota" ></asp:ListItem>
<asp:ListItem Value="MS" Text="Mississippi" ></asp:ListItem>
<asp:ListItem Value="MO" Text="Missouri" ></asp:ListItem>
<asp:ListItem Value="MT" Text="Montana" ></asp:ListItem>
<asp:ListItem Value="NE" Text="Nebraska" ></asp:ListItem>
<asp:ListItem Value="NV" Text="Nevada" ></asp:ListItem>
<asp:ListItem Value="NH" Text="New Hampshire" ></asp:ListItem>
<asp:ListItem Value="NJ" Text="New Jersey" ></asp:ListItem>
<asp:ListItem Value="NM" Text="New Mexico" ></asp:ListItem>
<asp:ListItem Value="NY" Text="New York" ></asp:ListItem>
<asp:ListItem Value="NC" Text="North Carolina" ></asp:ListItem>
<asp:ListItem Value="ND" Text="North Dakota" ></asp:ListItem>
<asp:ListItem Value="OH" Text="Ohio" ></asp:ListItem>
<asp:ListItem Value="OK" Text="Oklahoma" ></asp:ListItem>
<asp:ListItem Value="OR" Text="Oregon" ></asp:ListItem>
<asp:ListItem Value="PA" Text="Pennsylvania" ></asp:ListItem>
<asp:ListItem Value="RI" Text="Rhode Island" ></asp:ListItem>
<asp:ListItem Value="SC" Text="South Carolina" ></asp:ListItem>
<asp:ListItem Value="SD" Text="South Dakota" ></asp:ListItem>
<asp:ListItem Value="TN" Text="Tennessee" ></asp:ListItem>
<asp:ListItem Value="TX" Text="Texas" ></asp:ListItem>
<asp:ListItem Value="UT" Text="Utah" ></asp:ListItem>
<asp:ListItem Value="VT" Text="Vermont" ></asp:ListItem>
<asp:ListItem Value="VA" Text="Virginia" ></asp:ListItem>
<asp:ListItem Value="WA" Text="Washington" ></asp:ListItem>
<asp:ListItem Value="WV" Text="West Virginia" ></asp:ListItem>
<asp:ListItem Value="WI" Text="Wisconsin" ></asp:ListItem>
<asp:ListItem Value="WY" Text="Wyoming" ></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="timeZoneLabel" runat="server" Text="Time Zone" Font-Size="Small"></asp:Label>
<br />
<asp:DropDownList ID="timezoneDDL" runat="server" Font-Size="Small">
<asp:ListItem Value="" Text="Select a state" Selected="True"></asp:ListItem>
<asp:ListItem Value="HI" Text="Hawaii" ></asp:ListItem>
<asp:ListItem Value="AK" Text="Alaska" ></asp:ListItem>
<asp:ListItem Value="PST" Text="Pacific" ></asp:ListItem>
<asp:ListItem Value="Arizona" Text="Arizona" ></asp:ListItem>
<asp:ListItem Value="MST" Text="Mountian" ></asp:ListItem>
<asp:ListItem Value="CST" Text="Central" ></asp:ListItem>
<asp:ListItem Value="EST" Text="Eastern" ></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="resumeLabel" runat="server" Text="Resume" Font-Size="Small"></asp:Label>
<asp:TextBox ID="resumeBox" runat="server" TextMode="MultiLine" Columns="50" Rows="15"></asp:TextBox>
<br />
<br />
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_onClick"> </asp:Button>
</asp:Panel>
<asp:Panel ID="thankYouDiv" runat="server">
<h1>Thank You</h1>
</asp:Panel>
</form>
</body>
</html>
And the code behind is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace SchedulerServices
{
public partial class subscribeContact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitButton_onClick(object sender, EventArgs e)
{
// test that fields are filled in
string xrefID = Request.QueryString["XR"];
bool sendBack = false;
firstNameLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(firstNameBox.Text.ToString()))
{
firstNameLabel.ForeColor = System.Drawing.Color.Red;
firstNameBox.Focus();
sendBack = true;
}
lastNameLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(lastNameBox.Text.ToString()))
{
lastNameLabel.ForeColor = System.Drawing.Color.Red;
lastNameBox.Focus();
sendBack = true;
}
emailLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(emailAddressBox.Text.ToString()))
{
emailLabel.ForeColor = System.Drawing.Color.Red;
emailAddressBox.Focus();
sendBack = true;
}
phoneNumberLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(phoneNumberBox.Text.ToString()))
{
phoneNumberLabel.ForeColor = System.Drawing.Color.Red;
phoneNumberBox.Focus();
sendBack = true;
}
stateLabel.ForeColor = System.Drawing.Color.Black;
if (stateDDL.SelectedValue == "")
{
stateLabel.ForeColor = System.Drawing.Color.Red;
stateDDL.Focus();
sendBack = true;
}
timeZoneLabel.ForeColor = System.Drawing.Color.Black;
if (timezoneDDL.SelectedValue == "")
{
timeZoneLabel.ForeColor = System.Drawing.Color.Red;
timezoneDDL.Focus();
sendBack = true;
}
resumeLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(resumeBox.Text.ToString()))
{
resumeLabel.ForeColor = System.Drawing.Color.Red;
resumeBox.Focus();
sendBack = true;
}
if (sendBack)
{
allFieldsRequired.Visible = false;
selectedFieldsRequired.Visible = true;
return;
}
// get the account ID and User ID
// put the information in a record layout
// write record
inputDiv.Visible = false;
thankYouDiv.Visible = true;
}
}
}
I am stumped on this one. The other stand alone pages work without a problem, including using code that exists in non-excluded areas. Once the page loads, then it operates as it should.
The issue is that postback will execute the login.aspx regardless of the web.config exclusion.
The answer lies in modifying the login.aspx.cs file to include the following code in the Page_Load section:
protected void Page_Load(object sender, EventArgs e)
{
string returnURL = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(returnURL))
{
// Test for the exclusion
if (returnURL.Length > 7)
{
if (returnURL.Substring(1, 7) == "Webinar") // or whatever you want to continue on to processing
{
Response.Redirect(returnURL);
}
if (returnURL.Substring(1, 9) == "subscribe") // or whatever you want to continue on to processing
{
Response.Redirect(returnURL);
}
}
if (System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.Redirect(returnURL); // this exists to allow users to re-log in if the session has not timed out
}
}
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); // use this if you want the person directed to a registration page
ResendEmailHyperLink.NavigateUrl = "Resend.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); // use this if you want the person to have the registration email resent to them
ResetHyperLink.NavigateUrl = "ResetPassword.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); // use this to allow the user to reset their password
}
This now works. So the work around is to code the Login.asp.cs (or whatever is your login page) to specifically redirect to the value in ReturnURL. I re-discovered my workaround when tracing the process and saw the work-around I created 4+ years ago.
I hope this help anyone else stuck with figuring out how to code a widget using straight C#, ASP.NET code without having to resort to more exotic work-arounds using JSON, JQUERY, or MVC

Step by Step dropdown selection

Please help me if you have an idea on autopostback in asp.net using C#
i have 4 dropdown lists in one page
if i selects 1st dropdownlist then it enables 2nd dropdownlist
and after selection an option in 2nd dropdownlist 3rd dropdownlist enables
and after selection an option in 3rd drop down list 4th dropdown list enables
please let me know to do this task...
please help me
Try this:-
ASPX
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
ASPX CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Enabled = true;
DropDownList2.Enabled = false;
DropDownList3.Enabled = false;
DropDownList4.Enabled = false;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Enabled = true;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Enabled = true;
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList4.Enabled = true;
}
you can use SelectedIndexChanged event of drop down list to do the server side functionality and set AutoPostBack = true so that on selection change your page will get re-loaded.
Check out this Fiddle .
Important part is to handle the change event of dropdown list and to show another dropdownlist.
$("#one").change(function()
{
$("#two").toggleClass("show",true);
});

Compare Validation for drop down list with time in AM/PM format

I have 4 drop downs: here is the drop down code.
<asp:DropDownList ID="DDL_TimeFromMon" runat="server">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
<asp:ListItem Text="9" Value="9"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="11" Value="11"></asp:ListItem>
<asp:ListItem Text="12" Value="12"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_MonFromAMPM" runat="server">
<asp:ListItem Text="AM" Value="AM"></asp:ListItem>
<asp:ListItem Text="PM" Value="PM"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" ForeColor="Gray" Text="to"></asp:Label>
<asp:DropDownList ID="DDL_TimeToMon" runat="server">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
<asp:ListItem Text="9" Value="9"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="11" Value="11"></asp:ListItem>
<asp:ListItem Text="12" Value="12"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_MonToAMPM" runat="server">
<asp:ListItem Text="AM" Value="AM"></asp:ListItem>
<asp:ListItem Text="PM" Value="PM"></asp:ListItem>
</asp:DropDownList>
one drop down is for from time in 12 hours the other is AM/PM. same repeats for to drop down. How can a compare validator work, so that we can verify that to time is greater then from time. AM/PM. I tried Compare validator on both, but it is giving error when i select drop with whatever values. please point me to right direction. If this is not possible with compare validator, then sample code for jquery or javascrpt for validation.
Thanks in advance
You could use a customvalidator like this:
Markup:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Select a valid time" OnServerValidate="TimeValidate">
</asp:CustomValidator>
CodeBehind:
protected void TimeValidate(object source, ServerValidateEventArgs args)
{
int fromVal = int.Parse(DDL_TimeFromMon.SelectedValue)
int toVal = int.Parse(DDL_TimeToMon.SelectedValue)
string fromAMPM = ddl_MonFromAMPM.SelectedValue
string toAMPM = ddl_MonToAMPM.SelectedValue
if(fromAMPM == "AM" && toAMPM == "PM")
args.IsValid = True
else if(toAMPM == "AM" && fromAMPM == "PM")
args.IsValid = False
else
args.IsValid = fromVal < toVal
}

Categories

Resources