2010-05-30 42 views
1
<asp:TextBox ID="txtDate" runat="server" Value="<%= DateTime.Today.ToShortDateString() %>" /> 

Value="<%= DateTime.Today.ToShortDateString() %>"不會在txt字段中寫入日期,而是整個字符串。我做錯了什麼?在asp中寫入日期:TextBox

回答

1

使用JavaScript和jQuery:

var now = new Date(); 
$('#txtDate').text(now.getDate() + '/' + now.getMonth()+ '/' + now.getYear()); 

或普通的JavaScript:

var now = new Date(); 
document.getElementById('txtDate').value = now.getDate() + '/' + now.getMonth()+ '/' + now.getYear(); 

或標記(使用System.Web.UI.WebControls.TextBox.Text屬性,它沒有Value屬性):

<asp:TextBox ID="txtDate" runat="server" Text="<%# DateTime.Today.ToShortDateString() %>" /> 

和在那之後撥打this.DataBind();或者不是頁面,b ut TextBox的父控件。

+0

你的最後一個方法將實際呈現的''<% ... %>文本框之內。要使用spaghetti方法進行設置,您只能使用標準的非服務器輸入控件。 – 2010-05-30 08:44:16

+0

@Anthony:是的,你說得對。我弄錯了:需要使用'<%#'而不是'<%=' – abatishchev 2010-05-30 08:55:17

1

請參閱this similar question

正如您所見,您不能使用<%= %>構造來設置服務器控件的屬性。

的常用方法來設置標記的屬性是使用<%# data-binding expression %>