Results 1 to 22 of 22

Thread: ASPX code being auto-formatted the wrong way

  1. #1

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

    ASPX code being auto-formatted the wrong way

    Hi,

    I've had this problem for a while now, it seems to come and go (sometimes it happens, sometimes it doesn't) and I can't really figure out what's wrong.

    Basically my aspx code will sometimes format automatically, except not in the way I want it to. I always structure html and xml-like code as such:
    Code:
    <parent>
        <child />
        <child>
            <subchild />
            <subchild />
        </child>
    </parent>
    This way I can quickly see which tags belong to which, and I think this is the usual way html is formatted, right?

    Anyway, suppose that I have this code in my aspx code window (don't mind that it's not aspx code, that's not the point), then sometimes VS will automatically format it to this:
    Code:
    <parent>
        <child />
        <child>
            <subchild />
            <subchild /></child></parent>
    Basically it seems to put all closing tags on the last line, leaving me sometimes (with many nested tags) with an extremely long line at the end, making it completely impossible to see the structure of my code.



    Here is an actual example. This is how I wrote my code:
    xml Code:
    1. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    2.    
    3.     <h2>Login</h2>
    4.  
    5.     <p>
    6.         <asp:Label runat="server" ID="lblMessage" Font-Bold="true" ForeColor="Red" />
    7.     </p>
    8.  
    9.     <p>
    10.         <asp:Table runat="server">
    11.             <asp:TableRow>
    12.                 <asp:TableCell>Gebruikersnaam:</asp:TableCell>
    13.                 <asp:TableCell Width="35px" />
    14.                 <asp:TableCell>
    15.                     <asp:TextBox runat="server" ID="txtUsername" />
    16.                 </asp:TableCell>
    17.             </asp:TableRow>
    18.  
    19.             <asp:TableRow>
    20.                 <asp:TableCell>Wachtwoord:</asp:TableCell>
    21.                 <asp:TableCell Width="35px" />
    22.                 <asp:TableCell>
    23.                     <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" />
    24.                 </asp:TableCell>
    25.             </asp:TableRow>
    26.         </asp:Table>
    27.     </p>
    28.  
    29.     <p>
    30.         <asp:Button runat="server" ID="btnOK" Text="Login" OnClick="btnOK_Click" />
    31.         <asp:Button runat="server" ID="btnCancel" Text="Annuleren" OnClick="btnCancel_Click" />
    32.     </p>
    33.  
    34. </asp:Content>

    Now when I change my view to 'Split view' (to see both code and design in one window) and start clicking in the design view, sometimes (not always for some reason), VS seems to think I want to format my code, and it changes it into this mess:
    xml Code:
    1. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    2.    
    3.     <h2>Login</h2>
    4.  
    5.     <p>
    6.         <asp:Label runat="server" ID="lblMessage" Font-Bold="true" ForeColor="Red" />
    7.     </p>
    8.  
    9.     <p>
    10.         <asp:Table runat="server">
    11.             <asp:TableRow>
    12.                 <asp:TableCell>Gebruikersnaam:</asp:TableCell>
    13.                 <asp:TableCell Width="35px" />
    14.                 <asp:TableCell>
    15.                     <asp:TextBox runat="server" ID="txtUsername" />
    16.                 </asp:TableCell></asp:TableRow><asp:TableRow>
    17.                 <asp:TableCell>Wachtwoord:</asp:TableCell><asp:TableCell Width="35px" />
    18.                 <asp:TableCell>
    19.                     <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" />
    20.                 </asp:TableCell></asp:TableRow></asp:Table></p><p>
    21.         <asp:Button runat="server" ID="btnOK" Text="Login" OnClick="btnOK_Click" />
    22.         <asp:Button runat="server" ID="btnCancel" Text="Annuleren" OnClick="btnCancel_Click" />
    23.  
    24.     </p>
    25.  
    26. </asp:Content>
    This is actually not as bad as it gets, I've seen it way worse, but still I cannot read this mess anymore and I have to go back and manually format the code again until it just happens again...


    Using Format Document helps a little bit but it still isn't the way I want it... After Format Document, the Table for example is condensed into this:
    xml Code:
    1. <asp:Table runat="server">
    2.             <asp:TableRow>
    3.                 <asp:TableCell>Gebruikersnaam:</asp:TableCell>
    4.                 <asp:TableCell Width="35px" />
    5.                 <asp:TableCell>
    6.                     <asp:TextBox runat="server" ID="txtUsername" />
    7.                 </asp:TableCell></asp:TableRow>
    8.             <asp:TableRow>
    9.                 <asp:TableCell>Wachtwoord:</asp:TableCell><asp:TableCell Width="35px" />
    10.                 <asp:TableCell>
    11.                     <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" />
    12.                 </asp:TableCell></asp:TableRow>
    13.         </asp:Table>
    It still places many closing tags on a single line for some reason.



    I have made several journeys through the many Text Editor options, but I have not yet been able to fix this issue... I've looked at the HTML options, XML options, whatever seems likely, but I cannot figure out the correct combination of options that will make VS auto-format my code in the usual way, instead of just dropping multiple closing tags on one line...


    Any help?

  2. #2
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: ASPX code being auto-formatted the wrong way

    Hi,

    I have the same problem in VS 2008. VS always adds 100's of lines of spaces inbetween my markup and it really annoys me. I fixed it once but the next time I just left it the way it is

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

    Re: ASPX code being auto-formatted the wrong way

    Hello,

    If you don't "like" the default formatting options of the "Format Document" (CTRL + K, D) command, then you can tell Visual Studio what you preferences are.

    Take a look at this short video:

    http://scottcate.com/Tricks/018/

    Hope that helps!

    Gary

  4. #4

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

    Re: ASPX code being auto-formatted the wrong way

    Yeah I've played around with those options, but they don't seem to work. For example, in the 'tag specific options' I added the <asp:Table> tag for controls and told VS to insert a newline before, in between, and after, (same for <asp:TableRow> and <asp:TableCell>) but after auto-formatting it still puts the closing tags on one line... </asp:TableCell></asp:TableRow></asp:Table>. It simply doesn't want to listen... lol.

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

    Re: ASPX code being auto-formatted the wrong way

    Which option were you playing with specifically? Text Editor | HTML? Or something else?

    Gary

  6. #6

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

    Re: ASPX code being auto-formatted the wrong way

    Text Editor - HTML / XML / XAML and probably a few others. In the HTML options I also used the tag specific options button but still no luck. If I have some time later today I'll show you the exact settings and how the autoformat kicks into effect.

  7. #7

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

    Re: ASPX code being auto-formatted the wrong way

    Ok, I got a bit further. The tag specific options I have now are this:
    Code:
    Default Settings
    	Client tag supports contents
    		Line breaks = Before opening, within, and after closing
    		Indent contents
    
    	Server tag suppors contents
    		Line breaks = Before opening, within, and after closing
    		Indent contents
    
    ASP.NET Controls
    	asp:Table / asp:TableCell / asp:TableRow
    		Line breaks = Before opening, within, and after closing
    		Indent contents
    (I am trying to format the Table correctly at the moment, hence those options).
    Judging from the example this should give me what I want.

    However, for some reason if my code is formatted as such:
    Code:
    <asp:Table runat="server">
        <asp:TableRow>
            <asp:TableCell>  ...  </asp:TableCell></asp:TableRow></asp:Table>
    (This happens automatically for some reason, haven't figured out yet why)

    Now if I do Edit - Advanced - Format Document, it does not work. It does not change anything. However, if I put the closing tags on separate lines manually, but still not indented properly, like this
    Code:
    <asp:Table runat="server">
        <asp:TableRow>
            <asp:TableCell>  ...  
            </asp:TableCell>
            </asp:TableRow>
            </asp:Table>
    Now after Format Document it DOES format it correctly:
    Code:
    <asp:Table runat="server">
        <asp:TableRow>
            <asp:TableCell>  
                ...  
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    So it seems to be quite buggy to me, it simply ignores closing tags that are on the same line when it should actually put them on a new line as per the options I chose...

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

    Re: ASPX code being auto-formatted the wrong way

    Hello Nick,

    Did you ever do any more investigation into this? I wasn't able to get it to do what you wanted

    Gary

  9. #9

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

    Re: ASPX code being auto-formatted the wrong way

    Hmmm, I still think that there MUST be a way to do this, and I would be VERY surprised if there isn't.

    If I get a chance, I will do some more digging on this.

    Gary

  11. #11
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Re: ASPX code being auto-formatted the wrong way

    I am experiencing the same problem. I want my Heading1 elements to be on one line. I have set the proper "Tag Specific Options" after creating an h1 "Client HTML Tag", but it never puts my code on one line. It gets formatted differently depending on white space use before issuing the format command, but never like I want it. VS 2008 was the same way. This is a real pain.

    , Lee

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

    Re: ASPX code being auto-formatted the wrong way

    Hello Lee,

    This is starting to sound more and more like a bug within Visual Studio itself. You might want to think about submitting a but over at:

    http://connect.microsoft.com/

    With steps to reproduce, and see what they say.

    Gary

  13. #13
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Re: ASPX code being auto-formatted the wrong way

    The last several times I tried, Visual Studio was not one of the "products accepting bugs or suggestions". That's a great way to stifle feedback, Microsoft. I'll set a reminder in Outlook to keep trying tell you about your bugs, hoping that one day you're willing to listen. Sheesh.

  14. #14
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Re: ASPX code being auto-formatted the wrong way


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

    Re: ASPX code being auto-formatted the wrong way

    Quote Originally Posted by Lee C. View Post
    The last several times I tried, Visual Studio was not one of the "products accepting bugs or suggestions". That's a great way to stifle feedback, Microsoft. I'll set a reminder in Outlook to keep trying tell you about your bugs, hoping that one day you're willing to listen. Sheesh.
    Oh really! I was not aware of that!

    Looks like there have been a number of reports about this functionality though

  16. #16

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

    Re: ASPX code being auto-formatted the wrong way

    Judging from that page, it seems like it is a bug after all and they won't be fixing it until the next release... But then I don't understand why not everyone can reproduce it? I doubt it's a matter of 'not noticing' it because it is VERY noticeable if 8 closing tags get placed on one line....

  17. #17
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Re: ASPX code being auto-formatted the wrong way

    This bug has existed for a long time. I'm nearly certain that I reported it in the 2008 release.

    Here's a report from 1/16/2006!

    http://connect.microsoft.com/VisualS...is-not-applied

    Don't get your hopes up for a fix. I'm surprised Microsoft didn't do the old remove-the-feature-instead-of-fixing-it fix!

    How many VS realeases (including SPs) have there been since 1/16/2006? Wow.

  18. #18
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Re: ASPX code being auto-formatted the wrong way

    Quote Originally Posted by NickThissen View Post
    ...it seems like it is a bug after all and they won't be fixing it until the next release...
    I wish I could believe that. Connect is a place to gather votes. If the bug fails to get lots of votes, it gets no love from Microsoft. Pure democracy is bad. The Founding Fathers knew it; I wish Microsoft did.

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

    Re: ASPX code being auto-formatted the wrong way

    It is difficult or them I guess. With such a huge code base, they have to pick carefully about what getting included.

    Gary

  20. #20

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

    Re: ASPX code being auto-formatted the wrong way

    I can believe it's difficult, but it's also a huge pain! It makes maintaining aspx code simply impossible. I already find it difficult to read aspx/html code (compared to other languages) for some reason, even if properly formatted, but if the formatting goes haywire like this I can't make sense of it anymore...
    Imagine writing C# code, and each time you switch to the designer the code editor screws up and creates something like this mess
    Code:
    public 
                                                   class Someclass : Entity<TValue
    ,
           TKey>
    { public SomeClass(int i) { this.Value = i;
    }}
    That'd be pretty horrible, right..? That's what it's like for me. I have to go back and re-format the aspx code every time I want to properly read it, and then as soon as I switch to design view it's messed up again...

    I was actually looking at creating code editor add-ons so I could write my own formatter but at the moment that's not worth the trouble... I thought it would be easy in VS2010 but it's still pretty complicated.

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

    Re: ASPX code being auto-formatted the wrong way

    Hey,

    I honestly can't say that I have been plagued by this issue though. The example that you gave dealt specifically with an asp:Table. Do you have other examples where this happens, or is it only with this particular tag? The reason that I ask is that I typically don't use Tables (either HTML or ASP.Net) in my ASPX markup.

    Gary

  22. #22
    New Member
    Join Date
    Apr 2011
    Posts
    6

    Re: ASPX code being auto-formatted the wrong way

    Quote Originally Posted by gep13 View Post
    The example that you gave dealt specifically with an asp:Table. Do you have other examples where this happens, or is it only with this particular tag?Gary
    As mentioned, for me it was for <h1>, as well as h2, h3, etc. I think it's an issue for any tag that I have to add to the list (versus the built-in list).

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