2012-10-08 68 views
0

嘿,我想弄清楚我在這裏做錯了什麼。 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 

回答

2

我建議使用代碼背後總是你可以(在這種情況下) 。這樣,您就可以做同樣的是這樣的:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim currentYear As Integer = Date.Now.Year 
    For i As Integer = 0 To 19 
     Dim newItem As New ListItem 
     newItem.Text = currentYear + i 
     newItem.Value = currentYear + i 
     newItem.Selected = False 
     If i = 0 Then 
      newItem.Selected = True 
     End If 
     df_dd_ccyear.Items.Add(newItem) 
    Next 
End Sub 

在標記文件(.aspx文件),你只需要申報ListBox這樣的:

<asp:ListBox ID="df_dd_ccyear" style="z-index: 1000;" runat="server" /> 
+0

上面的代碼只能產生2031個值。 – StealthRT

+0

再試一次。我已經改變並測試了它。錯誤是在'For'語句之前聲明瞭ListItem變量。 – adripanico

+0

這很棒!謝謝你的幫助,titopo。 – StealthRT

0

你不能做到這一點。 ASP標籤在服務器上運行。 JavaScript在客戶端運行。你必須在其他地方動態地添加/刪除javascript項目。

通過查看代碼,它可能是最有意義的寫在你的代碼隱藏在C#/ VB中。

2

你不能以這種方式混合JavaScript和服務器端代碼。 Javascript運行在客戶端上,而ASP.NET運行在服務器上。

因此,第一個可能性是建立在服務器上:

<asp:ListBox 
    ID="df_dd_ccyear" 
    name="df_dd_ccyear" 
    style="z-index: 1000;" 
    runat="server" 
    ClientIDMode="Static" 
/> 

,並在後面的代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    var currentYear = DateTime.Now.Year; 
    var years = Enumerable.Range(currentYear, 20); 
    df_dd_ccyear.DataSource = years; 
    df_dd_ccyear.SelectedIndex = 0; 
    df_dd_ccyear.DataBind(); 
} 

,或者如果你想要一個純JavaScript的解決方案將項目添加到列表框中:

<%@ Page 
    Language="C#" 
    AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" 
%> 

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:ListBox 
      ID="df_dd_ccyear" 
      name="df_dd_ccyear" 
      style="z-index: 1000;" 
      runat="server" 
      ClientIDMode="Static" 
     /> 

     <script type="text/javascript"> 
      var years = document.getElementById('df_dd_ccyear'); 
      var d = new Date(); 
      var curr_year = d.getFullYear(); 

      for(var i = 0; i < 20; i++) { 
       if (i == 0) { 
        years.innerHTML += '<option value="' + curr_year + '" selected="selected">' + curr_year + '</option>'; 
       } else { 
        years.innerHTML += '<option value="' + (curr_year + i) + '">' + (curr_year + i) + '</option>'; 
       } 
      } 
     </script> 
    </form> 
</body> 
</html> 
+0

感謝另一種方式做所有這,Darin! – StealthRT

2

你不能在html中寫列表項,它們需要由asp.ne渲染t作爲選擇選項轉換爲html。

在你的代碼應該做的線沿線的東西:

For i = 0 to 20 
     df_dd_ccyear.items.add(currYear); 
    End For 

更多細節上加入listItems中可用:http://forums.asp.net/t/1142484.aspx/1

+0

謝謝你的建議,瑞恩! – StealthRT

相關問題