June 17, 2014

Session management in ASP.Net MVC and C#

In ASP.NET MVC, we can manage session by four ways: Default, Disabled, ReadOnly and Required. By default, or if we do not provide a session mode, it is set to its Default mode. Session mode is defined in “SessionStateBehavior” class.
To set session mode of a controller, we can use “SessionState” attribute at the beginning of defined controller class. For example:

public class HomeController : Controller


As we are defining the session mode of a particular controller, so its scope is limited to the respective controller only. Off course, for different controller we can set its different session mode.

Default mode has with its default and limited functionalities. Disabled mode restricts to store value in session. ReadOnly mode allows to read the session values but does not allow to write a value into session. Required mode allows to both read and write.

(This tutorial written under VS 2010, ASP.NET MVC 4)

June 11, 2014

Creating DOM in jQuery

Document Object Model in HTML or XML is a well-formed document with HTML tags or XML elements.
With jQuery, we can create DOM using ‘createElement()’ method. Many times, while programming, there may situations arise when you have part of HTML in string format and need to query it to find a particular value of tag, defined in it. In such cases DOM plays a vital role to achieve it.
Mechanism is simple, first create a DOM object with HTML string and then find for the element in the DOM object. In jQuery, you can create DOM object as:

var dom = document.createElement('div');

While programming, I got trapped in a situation where I need to find ‘span’ tag value on click event of a ‘div’ element. The scenario was, it was bound with a nested ‘div’ and was returning complete element having multiple HTML tags and I had to find single value – Transaction Number, to display the corresponding detailed information. The same I am sharing here with you.

Now, you can easily find element from the above, using simple jQuery syntax.

var val = $(dom).find("span").text();

And the above code will return: ‘TRANS00007’ without worrying about to manipulate long string.

Hope, it will be useful for you somewhere.