简体   繁体   中英

Adding code blocks in javascript function

I'm trying to set the value of an ASP TextBox via javascript when I click a button but I can't get the ClientID of my TextBox control using code blocks.

I get this error :

The Controls collection cannot be modified because the control contains code blocks (ie <% ... %>)

Here's my .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>

Replace

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM