Search code examples
c#asp.netdatetimeformviewfindcontrol

Get DateTime Value from TextBox in FormView


I need to find a value in a TextBox, contained within a FormView which holds a short date.

DateTime LastPayDate = (DateTime)FormView1.FindControl("user_last_payment_date");

I get the error:

CS0030: Cannot convert type 'System.Web.UI.Control' to 'System.DateTime'

And, I have no idea how to put a value back in the same format. Would love some help, pulling my hair out and their isn't much left.

Thanks


Solution

  •     //If you really need to find the textbox
        TextBox dateTextBox = 
                FormView1.FindControl("user_last_payment_date") as TextBox;
    
        if(dateTextBox == null)
        {
            //could not locate text box
            //throw exception?
        }
    
        DateTime date = DateTime.MinValue;
    
        bool parseResult = DateTime.TryParse(dateTextBox.Text, out date);
    
        if(parseResult)
        {
            //parse was successful, continue
        }