在javascript函数中添加代码块

时间:2015-07-08 07:47:28

标签: javascript asp.net

我正在尝试通过javascript设置ASP TextBox的值,当我单击按钮但我无法使用代码块获取TextBox控件的ClientID。

我收到此错误:

  

由于控件无法修改Controls集合   包含代码块(即<%...%>)

这是我的.aspx:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/Site.css" rel="Stylesheet" type="text/css" />
    <script>
        function testf() {
            alert("here");
            document.getElementById('<%= TextBox1.ClientID %>').value = "asd";
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSource1" 
            CssClass="Menu" StaticDisplayLevels="2" MaximumDynamicDisplayLevels="5" >
            <DataBindings>
                <asp:MenuItemBinding DataMember="rule" TextField="name" ValueField="value"/>
                <asp:MenuItemBinding DataMember="splitByField" TextField="name" ValueField="value" NavigateUrl="javascript:testf()"/>
                <asp:MenuItemBinding DataMember="splitByObject" TextField="name" ValueField="value"/>
                <asp:MenuItemBinding DataMember="statDefinition" TextField="name" ValueField="value"/>
            </DataBindings>
        </asp:Menu>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
            DataFile="~/App_Data/stats.xml"></asp:XmlDataSource>
        <asp:Literal ID="Message" runat="server" Text="Status:"></asp:Literal>
        <asp:TextBox ID="TextBox1" runat="server" EnableTheming="True" Height="137px" 
            Rows="6" TextMode="MultiLine" Width="842px"></asp:TextBox>

    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

替换

<asp:MenuItemBinding DataMember="splitByField" TextField="name" ValueField="value" NavigateUrl="javascript:testf()"/>

<asp:MenuItemBinding DataMember="splitByField" TextField="name" ValueField="value" OnClientClick="return testf()"/>