![](/img/trans.png)
[英]Get value from DetailsView to FormView Textbox in insert mode
[英]Get DateTime Value from TextBox in FormView
我需要在一個包含簡短日期的FormView內的TextBox中找到一個值。
DateTime LastPayDate = (DateTime)FormView1.FindControl("user_last_payment_date");
我收到錯誤:
CS0030: Cannot convert type 'System.Web.UI.Control' to 'System.DateTime'
而且,我不知道如何以相同的格式重新設置值。 希望得到一些幫助,把我的頭發拉出來,他們的余地就不多了。
謝謝
代碼中有錯誤,因為您試圖直接在日期時間中轉換控件,因此要解決錯誤,您需要在文本框控件中轉換控件,而不是在datetime中轉換文本,如下所示
DateTime LastPayDate = Convert.ToDateTime(
((System.Web.UI.WebControls.TextBox)
FormView1.FindControl("user_last_payment_date")).Text);
FindControl
將返回一個Control
,而不是該Control
的內容。
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
}
我不確定會不會編譯,但是會給你提示
DateTime LastPayDate = DateTime.Parse( (TextBox)FormView1.FindControl("user_last_payment_date")).Text );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.