14

When I am working with ASP.NET, I find that there are always unexpected things I run into that take forever to debug. I figure that having a consolidated list of these would be great for those "weird error" circumstances, plus to expand our knowledge of oddness in the platform.

So: answer with one of your "Gotcha"s!

I'll start: Under ASP.NET (VB), performing a Response.Redirect inside a try/catch block does not stop execution of the current Response, which can lead to two concurrent Responses executing against the same Session.

3
  • 4
    You should move your example to an answer Sep 15, 2008 at 19:58
  • I think this should be a community wiki
    – rlb.usa
    Feb 24, 2011 at 20:32
  • Dangit! Wish I had seen this a few weeks ago! I was caught by that very thing.
    – elbillaf
    Jul 7, 2011 at 19:20

21 Answers 21

21

Don't dynamically add controls after the page init event as it will screw up the viewstate tree.

0
8

Viewstate ... if you are using it ... can get out of control if you are not paying attention to it.

2
  • Viewstate is fine for changes made to controls by the user. It's when changes made by your code start making their way in there that you have a problem. Sep 16, 2008 at 21:15
  • I agree ... it's just something you want to keep an eye on ... as it is prone to sloppiness.
    – mattruma
    Oct 7, 2008 at 16:29
7

The whole life-cycle thing in general.

Not that I see anything wrong with it, it's just that you'd be amazed at the number of people who start working on large ASP.Net projects before understanding it, rather than vice versa. Hence, it becomes a gotcha.

Note that I said large projects: I think the best way to come to terms with the life cycle is to work on a few smaller projects yourself first, where it doesn't matter so much if you screw them up.

2
  • +1 This is true in general. I go a step farther and recommend they stick around to feel the pain of their coding/design decisions. Nov 30, 2009 at 16:56
  • Going to go read up on this right now.
    – elbillaf
    Jul 7, 2011 at 19:26
6

Life cycle of custom controls does not match up perfectly with page life cycle events of same name.

5

Page_Load is run before control handlers. So you can't make changes in an event handler and then use those changes in the page load. This becomes an issue when you have controls in a master page (such as a login control). You can get around the issue by redirecting, but it's definitely a gotcha.

4

Having to jump through hoops to get the .ClientID property into javascript.

It'd be nice if the render phase of the lifecycle created a script that set up a var for each server control with the same name as the control that was automatically initialized to the clientID value. Or maybe have some way to easily trigger this action.

Hmm... I bet I could set up a method for this on my own via reflection.

6
  • 1
    I HATE this. Thank god the MVC framework makes this unnecessary. Oct 7, 2008 at 16:22
  • At least in VB.NET, you have the option to use classic ASP delimiters to get the job done. It's not pretty, but it works: function jsFunction() { var someElement = document.getElementById(<%= SomeControl.ClientId %>); ... } Oct 7, 2008 at 16:45
  • 1
    I wouldn't put it straight into a .getElementById() call. I prefer to have a global script near the top where each control I use get's it's own var, defined once, with the clientID set as the value. They'd save a lot of hassle if they just made that standard. Oct 8, 2008 at 2:04
  • 1
    this has been fixed in ASP.Net 4.0 Sep 10, 2009 at 12:49
  • It was addressed. I'd say it's still far from "fixed". Sep 10, 2009 at 13:29
3

Don't edit your web.config with notepad if you have accented characters, it will replace it with one with the wrong encoding. It will look the same though. Just your application will not run.

1
  • IME the problem is that it inserts a BOM at the start of the file. Correct for .aspx files, but wrong for web.config (XML documents must not start with a BOM.)
    – finnw
    Jul 15, 2010 at 10:16
3

I just learned this today: the Bind() method, as used with GridViews and ListViews, doesn't exist. It's actually hiding some Reflector magic that turns it into an Eval() and some kind of variable assignment.

The upshot of this is that calls like:

<%# FormatNameHelper(Bind("Name")) %>

that look perfectly valid will fail. See this blog post for more details.

3

Debugging is a very cool feature of ASP.Net, but as soon as you change some code in the app_code folder, you trigger a re-build of the application, leading to all sessions being lost.

This can get very annoying while debugging a website, but you can easily prevent this using the "StateServer mode" : it's just a service to start and a line to change in the web.config : refer to msdn : http://msdn.microsoft.com/en-us/library/ms178586.aspx

  1. InProc mode, which stores session state in memory on the Web server. This is the default.
  2. StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  3. SQL Server ...
  4. Custom ...
  5. Off!
1
  • You could just create a web application instead of a website, which aleviates a lot of these problems. Jul 28, 2010 at 11:35
2

If you are running Classic ASP applications in the same Virtual Directory as you ASP.Net application, the fist hit on the application must be on an ASP.Net page. This will ensure that the AppPool be built with the right context configurations. If the first page to be hit is a Classic ASP page, the results may vary from application to application. In general the AppPool is configured to use the latest framework.

2

Making a repeater-like control, and not knowing about INamingContainer.

2
  • You have to worry about session timeouts for applications where the user might take a long time.

  • You also have to worry about uploading timeouts for large applications, too

  • Validatiors may not always scroll your page to the scene of the data entry error (so the user may not ever see it and will only wonder why the submit button won't work )

  • If the user enters HTML symbols such as <, > (for example, P > 3.14 ), or an inadvertant <br> from copy-pasting on another page, ASP.NET will reject the page and display a error.

  • null.ToString() produces a big fat error. Check carefully.

  • Session pool sharing across multiple applications is a disaster silently waiting to happen

  • Moving applications around on machines with different environments is a migraine that involves web.config and many potential hours of google

  • ASP.NET and MySQL are prone to caching problems if you use stored procedures

  • AJAX can make a mess, too:

    • There are situations where the client can bypass page validation (especially by pressing ENTER instead of pressing the submit button). You can fix it by calling if(! Page.IsValid) { return ; }
    • ASP buttons usually don't work correctly inside of UpdatePanels
    • The more content in your UpdatePanel, the more data is asynchronously transmitted, so the longer it takes to load
    • If your AJAX panel has a problem or error of some kind, it "locks up" and doesn't respond to events inside it anymore
1

Custom controls are only supported by the designer when building the control or when building the page that uses the control, but not both.

1

When using a gridview without a datasource control (i.e. binding a dataset straight to the control) you need to manually implement sorting and paging events as shown here:

http://ryanolshan.com/technology/gridview-without-datasourcecontrol-datasource/

1

Linq: If you are using Linq-To-SQL, you call SubmitChanges() on the data context and it throws an exception (e.g. duplicate key or other constraint violation), the offending object values remain in your memory while you are debugging, and will be resubmitted every time you subsequently call SubmitChanges().

Now here's the real kicker: the bad values will remain in memory even if you push the "stop" button in your IDE and restart! I don't understand why anyone thought this was a good idea - but that little ASP.NET icon that pops up in your system tray stays running, and it appears to save your object cache. If you want to flush your memory space, you have to right-click that icon and forcibly shut it down! GOTCHA!

1
  • This smells like abusing your DataContext (extending its lifetime beyond what it was designed for). Also, try enabling Edit-and-Continue. Jul 28, 2010 at 11:37
0

You can't reference anything at all above the application's root folder.

1
  • That's a security feature. Otherwise malicious code, say on a shared server, could browse the entire drive. Nov 30, 2009 at 16:59
0

All the code I have to maintain that still looks like it was written in vb6, showing complete ignorance of the newer styles.

I'm talking things like CreateObject(), excessive <% %> blocks, And/Or instead of AndAlso/OrElse, Len() instead of .Length(), s/o Hungarian prefix warts, Dim MyVariable with no type, functions with no return type... I could go on.

0

Being unaware of heaps of existing and extensible functionality in the framework. Things often redone are membership, roles, authorization, site maps. Then there are the controls and the associated tags which can be customized to alleviate issues with the client IDs among others. Also simple things like not knowing to properly use the .config file to auto import namespaces into templates, and being able to do that on a directory basis. Less known things like tag expressions can be valuable at times as well. Surely, as with all frameworks, there is a learning curve and always something left to be desired, however more often than not it is better to customize and extend an existing framework instead of rolling your own.

0

Not a pure ASP.NET thing, but ...

I was trying to use either a) nested SELECT or b) WITH clause and just could not get it to work, but people who were obviously more knowledgeable (including someone I work with) told me the syntax was fine. TURNS OUT ...

Was not able to use either of those with OLEDB.

OLEDB query to SQL Server fails

(Also, I was bit by the response.redirect() in the try ... catch 'feature' mentioned in the OP! Great thread!)

0

Databound controls inside an INamingContainer control must not be placed inside templated controls such as FormView. See this bug report for an example. Since INamingContainer controls creates their own namespace for their contained controls, two-way databinding using Bind() will not work properly. But when loading the values everything will look fine (because it is done with Eval()) it is not before you try to post back the values that they will mysteriously seem to not land in the database.

This so question demonstrates the issue well: AJAX Tabcontainer inside formview not inserting values

-5

(VB.NET) If you send an Object via a Property's Get accessor into a function with a ByRef keyword, it will actually attempt to update the object using the Set accessor for the Property.

Ex:

UpdateName(ByRef aName as String)

UpdateName(Employee.Name) will attempt to update the name by using the Set on the Name property of Employee.

1
  • 3
    That's a good thing- the way it should be. Sep 15, 2008 at 20:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.