views:

477

answers:

5

Using ASP.NET 2.0, I have a web app where I am trying to use Javascript to make one tab in a tabcontainer the active tab.

The recommendations have been based on:

var mX=document.getElementById('<%= tc1.ClientID%>')

$find('<%= tc1.ClientID%>').set_activeTabIndex(1);

Which both produce the error:

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

I've tried moving the code out of the head tag and into the body tag; same error.

I've also tried the alternative <%# tc1.ClientID%>, as in:

var mX = document.getElementById('<%# tc1.ClientID %>')

mX.ActiveTabIndex="2";

Generates a null error - code above is rendered in the html as:

var mX = document.getElementById('')

mX.ActiveTabIndex="2";

Can anyone explain in plain(er) language what this means and what the solution is?

Many thanks Mike Thomas

A: 

It looks and sounds like the code snippets are not themselves offensive, but some other code that was modifying the controls collection is now upset about them. Can you tell where in your program the error is actually occuring?

By the way, the <%# %> is not appropriate here — it's only for data-bound controls.

harpo
A: 

You can't have code blocks mixed with controls, instead store the value in javascript and assign it that way... something like this:

<div runat="server" id="rawr">
 <span id="myspan">HI</span>
</div>
</form>

<script type="text/javascript">
 var obj = '<%= DateTime.Now.ToShortDateString() %>';
 var ele = document.getElementById("myspan");
 ele.innerHTML = obj;
</script>
sontek
+2  A: 

I've actually run into that before. Here's an explanation: http://west-wind.com/WebLog/posts/6148.aspx

For example, if your markup looks like:

<asp:Panel id="whatever" runat="server">
    <script type="text/javascript">
         var mX=document.getElementById('<%= tc1.ClientID%>');
         //and so on...
    </script>
</asp:Panel>

And if you try to programatically add a control to that Panel it'll fail with the error you're getting.

One solution is to put your Javascript somewhere else in the page. Another way (although a hack) is this:

<asp:Panel id="whatever" runat="server">
    <asp:PlaceHolder id="dontCare" runat="server">
        <script type="text/javascript">
             var mX=document.getElementById('<%= tc1.ClientID%>');
             //and so on...
        </script>
    </asp:PlaceHolder>
</asp:Panel>

Now the <%= ... %> part is inside the PlaceHolder, not directly inside the Panel. Adding controls in your C# or VB code to the Panel should now work (although adding controls to the PlaceHolder would fail.)

EDIT:
Yeah, I tried using <%# ... %> instead too, but that's only for inside a DataBound control. For example, that would work if it was in the middle of a DataGrid and I called it's DataBind() method this PostBack.

Matt Blaine
+2  A: 

Hello!!! This is a fix to this obscure error if you're just beginning to use ASP.NET MVC. I leave this note here in case another person just like me is having this problem and I really want to save that guy from wasting hours of his time due to this lame error:

CHANGE THE LOCATION OF THE <SCRIPT> tags!!!

Most likely you wanted to put some javascript on the Site.Master page but when you did it, you did it below the tags when you included jQuery. No sir! Please include the <script> tag with the javascript code out of the <head> and into the <body> . Good luck!

wallyqs