26 Aralık 2009 Cumartesi

Accessing Session objects from library classes in ASP.NET

Problem: When you try to use Session objects in your class libraries you will get the error  "the name 'Session' does not exist in the class or namespace" . Because session is an http object and can be reached in the web context.

Solution: The following line will make make you access session objects if your class is in use as a result of an http request...

HttpContext.Current.Session("MyObject")


Yours sincerely.

Adding Alert Messages and JScripts to the .NET Web Applications

In your program logic sometimes you need to show alert messages in .NET Web Applications.. You can do this by adding java script attributes to the actions of your controls:

Simple Alert Message:
Button1.Attributes.Add("OnClick", "javascript:alert('Write your message here')");

Confirmation Alerts (Okey, Cancel):
Button1.Attributes.Add("OnClick", "javascript:if(!confirm('Are you sure about this opertaion?')) return false;");

Yours sincerely...

25 Aralık 2009 Cuma

Useful and Funny Visual Studio Shortcuts

* prop + tab + tab : implements properties automatically

  Ex: public int myProperty {get; set;}

* ctor + tab + tab : creates constructor of the class.

* Shift + Alt + F10 : Adds the namespace of an object into "using" part. Do it when the mouse is on the object.

* Select all the "using" part + right click ->  "Organise Using" -> "remove unused usings"  removes unused namespaces or you can sort the namespaces by "sort usings" option.

* Ctrl + k + c : comments the selected line
* Ctrl + k + u : uncommens the selected line

* Ctrl + shift + alt : opens the window of "add new item to the solution" Ex: add class, add form vs..

* Ctrl + tab : make you navigate in the open pages in solution
* right click on a page and select the option "close all but this" : closes all pages except the page you right clicked on.

* When you look at the properties of an object by adding a dot after the name of object, a combobox is opened and you can not see the code behind the combo..in that moment if you push the ctrl button long, the combo will be transparent and you can see behind of it...


"Cross-thread operation not valid" exception in VS 2008.

Exception: Cross-thread operation not valid. "Control 'textBox1' accessed from a thread other than the thread it was created on. "

When more than one thread try to access a control, we get this exception in multithread applications.

Solution: Paste the following line into your Page_Load method.So your program will not catch this kind of situations any more.
             Control.CheckForIllegalCrossThreadCalls = false;

Have a nice day!