嘿,我想弄清楚我在這裏做錯了什麼。 FYI我是新來的ASP.net和方式:O):ASP.net在代碼中設置javascript
<asp:ListBox ID="df_dd_ccyear" name="df_dd_ccyear" style="z-index: 1000;" runat="server" ClientIDMode="Static">
<script type="text/javascript">
var d = new Date();
var curr_year = d.getFullYear();
var i = 1;
while (i < 20) {
if (i == 1) {
document.write('<asp:ListItem Text=\"' + (curr_year + i) + '\" value=\"' + (curr_year + i) + '\" Selected=\"True\"></asp:ListItem>');
} else {
document.write('<asp:ListItem Text=\"' + (curr_year + i) + '\" value=\"' + (curr_year + i) + '\"></asp:ListItem>');
}
i++;
}
</script>
</asp:ListBox>
運行頁面。當我收到此錯誤:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: System.Web.UI.WebControls.ListItemCollection must have items of type 'System.Web.UI.WebControls.ListItem'. 'script' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.
Source Error:
Line 200: <div id="df-ccyear" style="z-index: 1000;">
Line 201: <asp:ListBox ID="df_dd_ccyear" name="df_dd_ccyear" style="z-index: 1000;" runat="server" ClientIDMode="Static">
**Line 202: <script type="text/javascript">**
Line 203: var d = new Date();
Line 204: var curr_year = d.getFullYear();
Source File: /chattclub/default.aspx Line: 202
上面的代碼只能產生2031個值。 – StealthRT
再試一次。我已經改變並測試了它。錯誤是在'For'語句之前聲明瞭ListItem變量。 – adripanico
這很棒!謝謝你的幫助,titopo。 – StealthRT