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
Related
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
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
I have two drop down list and a calendar control in my webform. When I click on my calendar values. It should select the right values and place the values in my dropdown list, but nothing is showing in my drop down lists after i selected them. The ID values are correctly named. The even handler is wired. I'm confused what's going on. Here is the code I'm using to link them together.
protected void clnArrival_SelectionChanged(object sender, EventArgs e)
{
ddlMonth.SelectedValue = clnArrival.SelectedDate.Month.ToString();
ddlDay.SelectedValue = clnArrival.SelectedDate.Day.ToString();
}
here my aspx code
<asp:Calendar ID="clnArrival" runat="server" Visible="true"
OnSelectionChanged="clnArrival_SelectionChanged">
<TodayDayStyle BackColor="Blue" Font-Bold="True" ForeColor="White"
/>
<TitleStyle BackColor="Blue" Font-Bold="True" ForeColor="White" />
<NextPrevStyle ForeColor="White" />
</asp:Calendar>
Edit
This information was taking from an answer that should have been an edit
<asp:DropDownList ID="ddlMonth" runat="server" Height="18px" Width="150px">
</asp:DropDownList>
<asp:DropDownList ID="ddlDay" runat="server" Width="173px">
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth" runat="server" Height="18px"
Width="150px">
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="3">March</asp:ListItem>
<asp:ListItem Value="4">April</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">June</asp:ListItem>
<asp:ListItem Value="7">July</asp:ListItem>
<asp:ListItem Value="8">August</asp:ListItem>
<asp:ListItem Value="9">September</asp:ListItem>
<asp:ListItem Value="10">October</asp:ListItem>
<asp:ListItem Value="11">November</asp:ListItem>
<asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlDay" runat="server" Width="173px">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>13</asp:ListItem>
<asp:ListItem>14</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>17</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>19</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>21</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
<asp:ListItem>24</asp:ListItem>
<asp:ListItem>25</asp:ListItem>
<asp:ListItem>26</asp:ListItem>
<asp:ListItem>27</asp:ListItem>
<asp:ListItem>28</asp:ListItem>
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
</asp:DropDownList>
protected void clnArrival_SelectionChanged(object sender, EventArgs e)
{
ddlMonth.SelectedValue = clnArrival.SelectedDate.Month.ToString();
ddlDay.SelectedValue = clnArrival.SelectedDate.Day.ToString();
}
This code should work, now the two droplists will display the proper month and date, just make sure your aspx value property for the droplists are correct.
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
}
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>