Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从FormView中的文本框获取日期时间值_C#_Asp.net_Datetime_Formview_Findcontrol - Fatal编程技术网

C# 从FormView中的文本框获取日期时间值

C# 从FormView中的文本框获取日期时间值,c#,asp.net,datetime,formview,findcontrol,C#,Asp.net,Datetime,Formview,Findcontrol,我需要在文本框中找到一个值,该文本框包含在保存短日期的FormView中 DateTime LastPayDate = (DateTime)FormView1.FindControl("user_last_payment_date"); 我得到一个错误: CS0030: Cannot convert type 'System.Web.UI.Control' to 'System.DateTime' 而且,我不知道如何把一个值放回相同的格式。我希望能得到一些帮助,把我的头发拔出来,他们的头发也

我需要在文本框中找到一个值,该文本框包含在保存短日期的FormView中

DateTime LastPayDate = (DateTime)FormView1.FindControl("user_last_payment_date");
我得到一个错误:

CS0030: Cannot convert type 'System.Web.UI.Control' to 'System.DateTime'
而且,我不知道如何把一个值放回相同的格式。我希望能得到一些帮助,把我的头发拔出来,他们的头发也不多了


谢谢

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

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

您的代码中有错误,因为您试图在日期时间中直接转换控件,所以要解决您的错误,您需要在textbox控件中转换控件,然后在datetime中转换文本,如下所示

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

FindControl
将返回一个
控件,而不是该控件的内容

TextBox textBox = (TextBox)FormView1.FindControl("user_last_payment_date");
DateTime LastPayDate = DateTime.Parse(textBox.Text);
    //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
    }