In the following code I have a dropdownlist and text box, what I'm trying to achieve is when an item from the dropdownlist is selected it needs to make sure the textbox is not null before page IsValid, if not Page is not valid, what I'm getting is Page is valid is textbox is null and not valid is textbox if populated!
Help, I'm new to this and cannot figure out why this is happening
Help, I'm new to this and cannot figure out why this is happening
Code:
<%@ Page Language="C#" %>
<html>
<head>
</head>
<body>
<form id="Form1" runat="server">
<p>
Outcome:
<asp:DropDownList ID="editStatusTextBox" runat="server"
CausesValidation="True" >
<asp:ListItem Selected="true" Value="Call Attempt 1" Text="Call Attempt 1"/>
<asp:ListItem Value="Call Attempt 2" Text="Call Attempt 2"/>
<asp:ListItem Value="Contacted - Interested" Text="Contacted - Interested"/>
<asp:ListItem Value="Contacted - Not Interested" Text="Contacted - Not interested"/>
<asp:ListItem Value="Callback Required" Text="Callback Required"/>
<asp:ListItem Value="Not in Business" Text="Not in Business"/>
<asp:ListItem Value="Number not in use" Text="Number not in use"/>
<asp:ListItem Value="Using alternative product" Text="Using alternative product"/>
<asp:ListItem Value="Nurture" Text="Nurture"/>
</asp:DropDownList>
Date:
<asp:TextBox id="editCallBack"
runat="server"></asp:TextBox>
<script runat="server">
void Button1_Click(Object sender, EventArgs e)
{
if (Page.IsValid)
{
Label1.Text = "Page is Valid";
}
else
{
Label1.Text = "Page Not Valid";
}
}
void ValidateNumber(object source, ServerValidateEventArgs args)
{
try
{
if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 1")
{
args.IsValid = false;
}
else if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 2")
{
args.IsValid = false;
}
}
catch (Exception ex)
{
args.IsValid = false;
}
}
</script>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="editCallBack" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:CustomValidator id="CustomValidator1"
runat="server" ControlToValidate="editCallBack"
ErrorMessage="You must enter a Date"
OnServerValidate="ValidateNumber"></asp:CustomValidator>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click"
runat="server" Text="Button"></asp:Button>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Comment