Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: ASP.NET FAQ


Tidbits by link9
Posted: 3 Mar 03

Hello all.  In this forum's beginning, we started a thread to just house helpful bits of information that any member found along their asp.net travels.

12 months later, that thread is beginning to get a little long and unwieldly, and so at the request of a few people, I've decided to put the contents of that thread into this FAQ.

If you, as a reader of this FAQ, have any input that you'd like to see here, please don't hesitate to send it.  You can either (a) send it directly to me through the link in this FAQ, or (b) post something in the main forum with a title with the word "TidBit" in it.

I'll pick it up, post it here, give you credit, and thank you profusely for your input.

So sincere thanks to all members who have and will contribute to this hodge-podge, and without further ado:

Accessing request objects in a class file

To access, say, request.form in a class file (perhaps to use a single assembly to process all requests on a site), you have to qualify it with 'Current', only after using or importing the System.Web.HttpContext namespace --

So that in an aspx file,

And in a .vb (or .cs) file,

Are synonymous.

Other intrinsic web objects would be the same (assuming you have added references to the proper assemblies)


(from member, Crystalized)
When dealing with events that are not firing consider the following.

If you have been using design view in Visual Studio to add and remove web controls, the tool may have erased some of the autogenerated code you require.

If you have an event you created the outline for by double clicking the control in design view, you will find an entry in the auto generated section of the code behind (in the InitializeComponent function).  The line will look something like the following which is a calendar control selection changed event:

this.clnDate.SelectionChanged += new System.EventHandler(this.clnDate_SelectionChanged);

In fact when I went to the page I grabbed the sample from I found - guess what - the code was missing, and I needed to copy it from elsewhere

Without this line, or some other piece of code to wire up the event the event code will not fire.

This is ALWAYS the first thing I check when things that worked suddenly stop firing.  If I have a page I am still going to be adding or removing controls from, I take the code out of the initializeComponent function and put it into the page load event until I am done with the control changes.

I hope this helps someone.


A contribution from Alcar about using #Region to organize your code



(from member, wsellars)

In-depth: Search Pages using Data-bound Controls, Index Server, & ASP.NET

Wayne Sellars


(from member, wsellars)
Selecting an Item in a DropDownList by Value

Many people have asked how to go about selecting an item in a DropDownList by value. This functionality is not built into the DropDownList, so you must loop through the DropDownList manually. This example shows how to do this.


Wayne Sellars


Autoreversing Datagrid Custom Control


for your consumption and enjoyment.  Now if only someone would write an extension to this that would repeat a header in the grid, I'd really be impressed.   

(this extensability invitation/challenge is still open as of today, btw)  


(from member, Zarcom)

Bit of an update on the dropdownlist.  You don't actually have to loop through the list to find your item.  I found that this works very well.
[color = red]
dropdownlist.Items.FindByValue("stuff").Selected = True



Ever wish you could use a for-each loop on your own custom data type?  


This is a great article on how to implement the iEnumerable interface on your own class to let you do just that.  You get an added bonus, too -- an instance of the resulting class you write is now able to be bound to such things as a datagrid as its datasource.  Very cool stuff.


(from member, jFrost10)

Re: Treeview Control

I won't bore you with the long story, but lets just say that if you want to get the most of your treeview, make sure you set its autopostback to = TRUE. Once you do that, alot of the functionality (i.e. selected node clicked event, etc.) that one might *ahem* have had to code manually will become available.

Just something to watch out for, as the default is False.



(from member, Zarcom)

There is a rather ambiguous little problem with deploying a web solution.  Often it is desirable to have the .Net Framework installed if it is not already so.  Many people may be tempted to just include the dotnetfxredist_x86_enum.msm file.  However, this will give you an error saying that the file cannot be used for redistribution and must be excluded. I found a fix for this on the microsoft site.  The file giving the error is for looks only basically, so it hasbe exluded.  The microsoft site had a premade setup.exe that can install the framework first if it doesn't exist.  You merely need to change the setup.ini file to point to your msi installer and to the dotnetfx.exe file.  

For anyone who is interested, here is the link.



Returning interfaces instead of objects

One of the design objectives of any application/system is to make it very maintainable.  A great way to move toward this goal is to specify the return value of a function as a particular interface type, rather than an object type.

private sub bindList()
  myDataList.DataSource = getDataSource()
end sub

private function getDataSource() as IEnumerable
  dim output as new arraylist()
  return output
end function

Now, the beauty of this setup is that you can change the implementation of the getDataSource() function to actually return, say... a hashtable, or even a dataview if you like.

Why?  Because all these objects implement the IEnumerable interface.  That's what allows objects to be bound to databound controls, and that's because if such an object does implement that interface, it supports a for-each iteration of its members -- a collection (see above post for how to make your own enumerable class).

Therefore, any object that implements that interface can be returned to the calling procedure, and life is grand.

Although my example is ultra-simplistic, this type of implementation could be put into play in a multi-tiered application, where the getDataSource() function is maintained by one developer in some satellite assembly, and could be changed by that developer at any time, as long as they stick by the rules (what's known as the contract in OOSD)... w/o ever bothering the other developer who just wants his/her datalist not to break.  This is a great way to loosen up that contract and still keep everyone very very happy.


(from member, jFrost10)

Speed Up Your Code
(From Speed Up Your VB.NET Code article, Visual Studio Magazine, August 2002)
Hey guys, I wanted to pass these tips on to you. If you want the greater-detail explanations, check out the mag. Here's a summary though:

Concatenate Strings With Stringbuilder
.NET strings are immutable. That means you can't change them once they're created. So in teh following code:

Dim strValue as string
strValue = "Hello There"
strValue = strValue & "See you later"

.NEt actually stores strValue in TWO different memory slots, even though it appears that you're just appending to the string. The way around this is to use the StringBuilder class and utilize its Append and Join functions.

Don't Throw Unnecessary Exceptions
Other than for critical errors, exceptions should be thrown sparingly. If you have a routine that loops through, and throws an exception once every hundred iterations, the routine will run 4 times slower. Not that I've personnaly seen or used exceptions in this way, but still a good rule of thumb not to use them like they're going out of style.

Seal Methods
If you're going to have overridable methods, the author suggests to NOT store actual code within them. Instead, point to another sub/function where the code actually resides. This is because calls to overridable methods can run 35% slower than a sealed one, and the compiler can often inline short nonoverridable methods, making your code faster. So instead of
Overridable Sub MyProc()
     Dim i as integer
     i = 10
   End Sub

the author suggests this:
Overridable Sub MyProc()
      Call InternalProc
   End Sub

   Private Sub InternalProc()
     Dim i as integer
     i = 10
   End Sub

(Paul, don't hate me, but...)
Avoid Calls To Interface Methods
Calling a method through a second interface is slower than calling the same method in the main class interface itself, according to the author. In face, it can be 4 or 5 times faster. However, he pointed out that for most people, the difference is negligable. However, if you're performing "millions of method invocations in a tight loop", its a good idea to avoid it.

There's the first 4. I should probably take a break from my novel and get some work done.

I'll post the otehr 4 later on this afternoon.




Sometimes, you don't want a user pressing the "Back" button in your application...  especially if there is processing that has to occur on every load of the page, whereas the values of your local variables can get out of sync if the user does, since the server side processing does not occur when they do press "Back".

This can produce unexpected results for your users...  not error messages, just wierdness.

At any rate, you can put this piece of script on your page to effectively disable the back button on browsers:


A simple yet efficient hack.  


"Fixing" your project to hit a breakpoint

Ever had a little cussing session over your project not hitting a breakpoint when debugging?  Breakpoint still have that damned question mark on it, saying that symbols aren't loaded, when you know debugging is enabled?

Just delete the .pdb file (in your /bin) folder, and run it again.  Viola.

I guess those things get corrupted sometimes, so just make the system rebuild it from scratch.


ps.  the same technique will work on most operating system files, too.  I stress MOST (don't go deleting your autoexec.bat)!


(from member vganeshbabu)

Giving an Alert to the Client if the Server Operation is Successful or Failure. Suppose I have a <asp:Button> to add a Record. After Adding the Result of the process of adding the record should be shown to the user as an Alert message... this Can be Achieved by the following

Protected WithEvents DivScript as System.Web.UI.HtmlControls.HtmlGenericControl

Private Sub AddRecord()
  'Write Code to Add to the Database
  DivScript.InnerHTML = "<Script language='javascript'> alert('record was successfully entered into an SQL database') </Script> "

End Sub

Once the function is executed. The HTML goes to the client and The Script in the Div Gets Executed in the Client Side and so the Alert is shown to the User. The Same way we can show the Error Message Also....


Thanks to Custom24 & Camel for this tidbit on regular expressions in the vb.net forum:



(from member jFrost10)


Visual Studio.NET makes it really easy to see all the possible exceptions that can be caught from .NET framework classes.

Just select the Debug menu, then Exceptions. A dialog will appear, and you can expand the tree and see all the various exceptions.

This can be helpful if you know of a handful of possible exceptions that could happen in your code (i.e. you try to write a file to a server, but theres no space), so you can deal with it specifically instead of just a general error handling solution.



(from member, Deadline)

Cool utility or so I was told.

Believe it or not, from Micro$oft, its a FREE product for developing ASP.Net pages.

Check this out.


Thank you,


(from member, Custom24)

Don't use straight SQL when it incorporates anything the user types or from an external source (eg the querystring)- there are security implications.  Either checj your SQL string or use parameters

Use of the querystring, plus how to allow for special characters


Ever wonder how many of those files you actually need to make a project run?  Don't want to FTP more than you have to to the production server?  Don't want to FTP your actual source code to the production server?  

Well, you don't have to.  In ASP.NET, you only need the compiled assemblies (.dll files), your .aspx UI pages, and a few other select files to make an app run.

No .vb or .cs files needed at all.

Select Project --> Copy from the VS menu and select "Only the files needed to make the application run" from the options on the resulting window, and then select your target directory.

You might be surprised at how little is actually needed to make it run.


Default Buttons for WebForms

Mmmmmm...  Default buttons for webforms.



ps.  Check out their other great (free!) webcontrols while you're there.  They have alot of very useful widgets.

Cookie Handling in ASP.NET

Great article on the care and handling of cookies in ASP.NET from Paul Riley and http://theCodeProject.com (which is a great site if you haven't found it yet).



Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close