开发者

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("开发者_StackOverflow中文版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


    //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
    }


FindControl will return a Control, not the contents of the control.

TextBox textBox = (TextBox)FormView1.FindControl("user_last_payment_date");
DateTime LastPayDate = DateTime.Parse(textBox.Text);


There is error in you code because you are trying to convert control directly in date time, so to resolve your error you need to convert control in textbox control and than convert text in datetime as given below

 DateTime LastPayDate = Convert.ToDateTime( 
                      ((System.Web.UI.WebControls.TextBox)  
                       FormView1.FindControl("user_last_payment_date")).Text);


I'm not sure that this will compiled, but will give you the clue

DateTime LastPayDate = DateTime.Parse( (TextBox)FormView1.FindControl("user_last_payment_date")).Text );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜