Link to home
Start Free TrialLog in
Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

check for valid date when textbox is visible

I have asp.net page with checkbox and textbox, if checkbox is checked then textbox is made visible and I then need to check for a valid date.  However if checkbox is not checked then I hide textbox and do not need to check for a valid date.  How can I do this?  This is my code.......

if (checkbox.Checked)
            {
                customer.DateRaised = DateTime.Parse(txtDate.Text);  //check for valid date here
            }
            else
            {
                customer.DateRaised = DateTime.Now;
            }
Avatar of Pratima
Pratima
Flag of India image

best way you can use any date control along with textbox
Avatar of mugsey

ASKER

Thanks but I need to use microsoft controls within visual studio
ASKER CERTIFIED SOLUTION
Avatar of karstieman
karstieman
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hey,
checkbox should be enabled with Autopostback property,,,see below..for the definition

<asp:CheckBox ID="CheckBox1" runat="server"
            oncheckedchanged="CheckBox1_CheckedChanged" Checked="false" AutoPostBack="true" />

In the Page load event
PageLoad()
{
  txtDate.Visible =false; //make your textbox false
}

In the CheckBox1_checkedChanged()
{
 if (CheckBox1.Checked)
            TextBox1.Visible = true;          
    else
            TextBox1.Visible= false;
}


In the TextBox1_TextChanged event()
{
     customer.DateRaised = DateTime.Parse(TextBox1.Text);  
}
You can try like this

  If CheckBox1.Checked = True Then
            TextBox1.Visible = True
        Else
            TextBox1.Visible = False
            TextBox1.Text = DateTime.Now
        End If

Add regular expression validator with date validation regular expression
when checkbox not selected just set valid date as now