ASP.net在代码中设置javascript

时间:2012-10-08 13:18:08

标签: javascript asp.net

嘿所有我想弄清楚我在这里做错了什么。仅供参考我是ASP.net及其新手:o):

<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 

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

你不能用html编写列表项,它们需要由asp.net渲染成html作为选择选项。

在您的代码中,您应该按照以下方式执行以下操作:

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

有关添加列表项的更多详细信息,请访问:http://forums.asp.net/t/1142484.aspx/1

答案 2 :(得分:2)

我建议您始终使用Code(如本例所示)。这样你就可以这样做:

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" />

答案 3 :(得分:0)

你不能这样做。 ASP标记在服务器上运行。 JavaScript在客户端运行。您必须在其他地方动态添加/删除javascript中的项目。

通过查看代码,在代码隐藏中用C#/ VB编写它可能是最有意义的。