2

我需要在一个 TextBox 中找到一个值,该值包含在一个包含较短日期的 FormView 中。

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

我得到错误:

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

而且,我不知道如何以相同的格式放回一个值。希望得到一些帮助,把我的头发拉出来,剩下的不多了。

谢谢

4

4 回答 4

2

您的代码中有错误,因为您尝试直接在日期时间中转换控件,因此要解决您的错误,您需要在文本框控件中转换控件,而不是在日期时间中转换文本,如下所示

 DateTime LastPayDate = Convert.ToDateTime( 
                      ((System.Web.UI.WebControls.TextBox)  
                       FormView1.FindControl("user_last_payment_date")).Text);
于 2010-07-22T09:57:29.680 回答
2

FindControl将返回 a Control,而不是控件的内容。

TextBox textBox = (TextBox)FormView1.FindControl("user_last_payment_date");
DateTime LastPayDate = DateTime.Parse(textBox.Text);
于 2010-07-22T09:58:07.883 回答
2
    //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
    }
于 2010-07-22T09:58:26.353 回答
1

我不确定这会编译,但会给你线索

DateTime LastPayDate = DateTime.Parse( (TextBox)FormView1.FindControl("user_last_payment_date")).Text );
于 2010-07-22T09:56:55.730 回答