views:

95

answers:

5

I recently started development in ASP.NET, and coming from a PHP background I sprinkle code into the aspx pages using <% %> to control logic.

I'm not debating the best practices of only using code behind, but one strong point in that directions favor is a problem I am experiencing.

Whenever I go into Design view of a page that has code in the aspx file like I mentioned, and make modifications, randomly all the <% %> blocks -shift- in my aspx to what seems like random spots.

This obviously royally messes up my control logic, and after redoing all the internal code 10+ times (yes yes, I'm too lazy to just bite the bullet and move it to code behind), I'm really curious how Visual Studio could make such a mistake?

It even goes so far as to remove some of the <% %> blocks completely (causing missing close braces in my code).

If anyone can help me solve this issue, or knows it can't be resolved, I would greatly appreciate knowing.

+5  A: 

Best-practice for ASP.NET is to avoid <% %> blocks as much as you can. They're legacy from classic ASP.

If you need to put values into the content create controls on the page such as <asp:TextBox> and then set the values accordingly in the code-behind.

The design/source view should only be for markup and not logic (if you can help it).

Sonny Boy
+1 nice answer. :)
Saar
I would edit this to say that these blocks are to be avoided when using ASP.NET WebForms. ASP.NET MVC makes liberal use of them, and to great effect IMO.
Dave Markle
+1 but with one caveat. You do not add content to the page by the use of <asp:TextBox> but rather through the use of label controls. Simple <%#VarName%> elements are fine as well but text boxes should only be used for user input - not output.
Mark Brittingham
@Mark Good point!
Sonny Boy
A: 

Are you trying to force the code into an arrangement that Visual Studio wants to fix? Because if you're having these kinds of problems with VS your best bet is to let VS rearrange the code the way it wants, and fix the errors that occur. Do this until the code stabilizes, and VS no longer wants to rearrange it.

That said, VS should never restructure your code in such a way that it changes the logic.

Robert Harvey
I just decided to stop using Designer half way through the project to avoid the issue entirely. Project built fine, site displayed fine, only issues was the code rearranging when I modified something in Design view.It completely threw me off and I was at a total loss as to how it happened as well.
Aequitarum Custos
A: 

The best answer I can give you is simply don't use the design view. You can drop controls from the toolbox directly into the markup if you need to, and I prefer to see how an actual browser will render the page anyway.

Joel Coehoorn
+1  A: 

I believe I found out the issues.

It appears invalid HTML (unclosed tags, or extra close tags) is causing the issues.

Layout designer did by hand without a syntax checker.

Aequitarum Custos
While you accepted your own answer, I would heed Sonny Boy's advice. There is absolutly no question in my mind that you will sooner rather than later come to regret a decision to treat ASP.NET like PHP. There is simply no reason to place logic on the ASPX page - it is bad design and fails to heed the lessons gained by those who have gone before you.
Mark Brittingham
Yeah, I fully agree. Lesson has been learned from this project!
Aequitarum Custos
A: 

I have this problem as well. Letting VS format the code does not work well. The formatting it does is haphazard. I have seen it remove tags from time to time as well causing even more difficultly. The bug fix helped some, but the issue is still there. I am basically using design mode only on those pages that have this issue (ones that have AJAX controls). This is not a great solution. Microsoft needs to fix this!

Bradley