Results 1 to 7 of 7

Thread: Adding script to page clears design-time view

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Adding script to page clears design-time view

    Hi,

    I have a very strange problem with my website... I created a rather large page, consisting of a few tables with some labels and textboxes where the user can enter a mechanical car setup (it's for a racing game).

    I was working with simple textboxes previously, but I recently switched over to a NumericBox control which should allow only numeric input using some client-side javascript. The NumericBox control is simply a UserControl with a single TextBox on it, and some client side events are added to this textbox which call methods from a single javascript file ("Scripts/numericValidator.js")

    When I put a couple of these boxes on a page it works just fine, I can only enter numbers. For it to work though I need to add the script to the page and I do that using this line, either in the head content or in the body content controls:
    asp Code:
    1. <script src="../../Scripts/numericValidator.js" type="text/javascript" />
    (the path is chosen with the 'Pick URL' dialog so it should be alright, and it does indeed need to go up two directories first as the page is in the '~/Pages/RequireLogon" directory)

    However, after replacing most of the textboxes on my large page with NumericBoxes, and adding this script line to my aspx code, the design view is 'broken'. I cannot see any of my controls anymore, and I cannot even put any new controls on there. It seems like the Content control is missing, even though it's still there just fine without errors in the aspx code.

    When I remove the script line, the problem is fixed again, the controls re-appear. So it's definitely the script line causing the problem, but I cannot see how it could...


    I tried it with a new blank page and the problem appears there too. If I use this simple page with just a button:
    asp Code:
    1. <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Test3.aspx.vb" Inherits="F1TimeTrials.Test3" %>
    2.  
    3. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    4.  
    5.     <!-- Script causes the designer to break... -->
    6.     <script src="../Scripts/numericValidator.js" type="text/javascript" />
    7.  
    8. </asp:Content>
    9. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    10.  
    11.     <asp:Button runat="server" Text="Button" />
    12.  
    13. </asp:Content>
    then the designer is broken. I cannot see the button and I cannot add any controls either. Remove the 'script' line from the head content and it works again.

    Strange...? Or am I doing something completely wrong :/



    I tried restarting visual studio, deleting bin/obj folders and restarting, restarting PC, nothing helps.

    Just in case it's the javascript itself causing problems I attached it so you can see it (it's not mine I found it somewhere online), but I don't think the designer will execute javascript code?!
    javascript Code:
    1. // version: beta
    2. // created: 2005-08-30
    3. // updated: 2005-08-31
    4. // mredkj.com
    5. function extractNumber(obj, decimalPlaces, allowNegative)
    6. {
    7.     var temp = obj.value;
    8.    
    9.     // avoid changing things if already formatted correctly
    10.     var reg0Str = '[0-9]*';
    11.     if (decimalPlaces > 0) {
    12.         reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
    13.     } else if (decimalPlaces < 0) {
    14.         reg0Str += '\\.?[0-9]*';
    15.     }
    16.     reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
    17.     reg0Str = reg0Str + '$';
    18.     var reg0 = new RegExp(reg0Str);
    19.     if (reg0.test(temp)) return true;
    20.  
    21.     // first replace all non numbers
    22.     var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
    23.     var reg1 = new RegExp(reg1Str, 'g');
    24.     temp = temp.replace(reg1, '');
    25.  
    26.     if (allowNegative) {
    27.         // replace extra negative
    28.         var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
    29.         var reg2 = /-/g;
    30.         temp = temp.replace(reg2, '');
    31.         if (hasNegative) temp = '-' + temp;
    32.     }
    33.    
    34.     if (decimalPlaces != 0) {
    35.         var reg3 = /\./g;
    36.         var reg3Array = reg3.exec(temp);
    37.         if (reg3Array != null) {
    38.             // keep only first occurrence of .
    39.             //  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
    40.             var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
    41.             reg3Right = reg3Right.replace(reg3, '');
    42.             reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
    43.             temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
    44.         }
    45.     }
    46.    
    47.     obj.value = temp;
    48. }
    49. function blockNonNumbers(obj, e, allowDecimal, allowNegative)
    50. {
    51.     var key;
    52.     var isCtrl = false;
    53.     var keychar;
    54.     var reg;
    55.        
    56.     if(window.event) {
    57.         key = e.keyCode;
    58.         isCtrl = window.event.ctrlKey
    59.     }
    60.     else if(e.which) {
    61.         key = e.which;
    62.         isCtrl = e.ctrlKey;
    63.     }
    64.    
    65.     if (isNaN(key)) return true;
    66.    
    67.     keychar = String.fromCharCode(key);
    68.    
    69.     // check for backspace or delete, or if Ctrl was pressed
    70.     if (key == 8 || isCtrl)
    71.     {
    72.         return true;
    73.     }
    74.  
    75.     reg = /\d/;
    76.     var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    77.     var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
    78.    
    79.     return isFirstN || isFirstD || reg.test(keychar);
    80. }


    Help? Thanks!

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Adding script to page clears design-time view

    Hmm.
    Inexperienced with asp and master pages but 2 quick observations.
    Can the script declaration be made outside the contentplaceholder?
    Rather stupid question but, this is not your actually full directory? "../Scripts/numericValidator.js" (with the ".." )
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Adding script to page clears design-time view

    Hey Nick,

    That is indeed a strange one, you really aren't having much luck lately are you?

    I have never seen a situation where including a script block has caused problems with the designer. It also seems like you have tried all the obvious suggestions. The one thing that I would be interested to see is the definition of the Master Page, i.e. where does that Content block go on the page. If you show the Master page definition, I will see if I can replicate the problem at this end, and we can work from there.

    What Visual Studio are you using?

    Gary

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Adding script to page clears design-time view

    Even better, I created a completely brand new Web Application project, left everything default (I get a default master page, a Default.aspx page and some login stuff which I didn't use). I add the script to the Scripts directory. I reference it from the Default page, and boom, there it goes.

    I attached the project files. I commented the offending script line (in Default.aspx), and in this way it works just fine, but as soon as I uncomment it and refresh the designer it breaks...

    Let's see if it's just me.

    Oh I'm using VS2010 by the way (and I suppose you'll need VS2010 to open this project).


    Edit
    Besides the problem in the designer it seems it is also broken during run-time. While the controls do show during run-time, it seems that they don't work at all. I can click buttons but there's not even a postback, just nothing happens.
    Attached Files Attached Files

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Adding script to page clears design-time view

    Okay, I am at a loss as to why this is, but, if you change this:

    Code:
    <script src="Scripts/numericValidator.js" type="text/javascript" />
    to this:

    Code:
    <script src="Scripts/numericValidator.js" type="text/javascript"></script>
    It will work.

    I will do some digging to see if I can come up with a reason.

    Gary

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Adding script to page clears design-time view

    Thanks, that is really strange but it does work. I thought <... /> was just shorthand notation for <..></..> and that it would always work but apparently there's something wrong here... A bug perhaps? Anyway, it works again, thanks for your help That's like the third time you've helped me out in a few weeks yet I still cannot rate you

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Adding script to page clears design-time view

    Hey,

    You are correct, < /> should be the same as <></> but for some reason (which I still can't figure out) in this situation, it is not being respected. I tried looking for a stray opening or closing set of brackets that could be causing the problem, but I couldn't find anything. It could well be a bug, might be worth submitting to MS, as it is definitely a repeatable problem.

    Not a problem about the rep, thanks for trying. You need to rep at least 10 other people I think before you can give rep to the same person again.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width