Labels

Thursday, December 27, 2007

Addition to the ViewState Myth

This is in addition to the post on Viewstate Myth in Apr 2007

In my last post, I have described the Viewstate Myth on ‘Change Of Values’ in controls.

Today I’m posting the conclusion of viewstate myth on the Visibility and Enabling Disabling of controls.

After making research on above two forms below is the conclusion:



Visibility –

· All controls are dependent on Viewstate.

· So if the Viewstate is disabled, the controls will become visible again on 'SimplePostBack' operation.

Enable/Disable-

· All controls are dependent on Viewstate.

· So if the Viewstate is disabled, the controls will become enable again on 'SimplePostBack' operation.

Recap:

Changing Value –

· Textbox, HiddenField, Dropdown does not depend on ViewState.

· Label requires view state.

Thanks & Regards,

Arun Manglick || Tech Lead

Monday, December 17, 2007

ASP.NET Dynamic Data Support - Scott G

ASP.NET Dynamic Data Support

http://weblogs.asp.net/scottgu/archive/2007/12/14/new-asp-net-dynamic-data-support.aspx

This is one of the cool new feature - "ASP.NET Dynamic Data Support". In a nutshell this enables us to really quickly build data driven web-sites that work against a LINQ to SQL (and in the future LINQ to Entities) object model - and optionally allows you to-do this Without Having To Build Any Pages Manually.

Below steps are required to work with this:

· Create a ‘Dynamic Data WebSite’ [A new template came in picture after you install ASP.NET 3.5 Extensions CTP release]


· Add a new LINQ to SQL data model to your project (right-click->add new item->LINQ to SQL Classes item).

Object Relational Mapper:

· LINQ to SQL is an O/RM (object relational mapper) that ships in .NET 3.5, and which allows you to model a relational database using .NET classes.

· You can then query the database using LINQ, as well as update/insert/delete data from it.

· LINQ to SQL fully supports transactions, views, and stored procedures.

· It also provides an easy way to integrate data validation and business logic rules into your data model.

· Scaffolding - Enable Dynamic Templates in Your Project:

· ASP.NET Dynamic Data projects provide built-in support for creating automatic "scaffolding" views of your LINQ to SQL and LINQ to Entities data models.

· Dynamic Data projects include template pages and user controls that can automatically create built-in Data Browsing/Editing/Selection/Deleting Ui That Is Dynamically Constructed At Runtime based on the LINQ to SQL or LINQ to Entities data model you've added to your project.

· To enable this auto-scaffolding support, open the web.config file of your project, search for the "dynamicData" section within it, and set the "enableTemplates" attribute to "true":

<system.web.extensions>

<dynamicData dataContextType=”” enableTemplate=”True”>

</system.web.extensions>

· Now the last step – Hit F5. You'll by default be taken to a default.aspx page that lists all of the table objects in your data model.

Thanks & Regards,

Arun Manglick || Tech Lead

ASP.NET 3.5 Extensions preview

ASP.NET 3.5 Extensions preview:

http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx

After the release of Orcas (VS 2008 and .NetFramework 3.5), there were few new features have been added into it. See my blog post.

Out of these new features, few have been for ASP.Net. These were called as ASP.Net 3.5 Extension.

This first ASP.NET 3.5 Extensions preview release (Dec 07) includes below:

  • ASP.NET AJAX Improvements: New ASP.NET AJAX features in the ASP.NET 3.5 Extensions release include better browser history support (back/forward button integration, and server-side history management support), improved AJAX content linking support with permalinks, and additional JavaScript library improvements.

  • ASP.NET MVC: This model view controller (MVC) framework for ASP.NET provides a structured model that enables a clear separation of concerns within web applications, and makes it easier to unit test your code and support a TDD workflow. It also helps provide more control over the URLs you publish in your applications, and more control over the HTML that is emitted from them.

http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx

  • ASP.NET Dynamic Data Support: The ASP.NET 3.5 Extensions release delivers new features that enable faster creation of data driven web sites. It provides a rich scaffolding framework, and will enable rapid data driven site development using both ASP.NET WebForms and ASP.NET MVC.

http://weblogs.asp.net/scottgu/archive/2007/12/14/new-asp-net-dynamic-data-support.aspx

  • ASP.NET Silverlight Support: With the ASP.NET 3.5 Extensions release we'll deliver support for easily integrating Silverlight within your ASP.NET applications. Included will be new controls that make it easy to integrate Silverlight video/media and interactive content within your sites.

  • ADO.NET Data Services: In parallel with the ASP.NET Extensions release we will also be releasing the ADO.NET Entity Framework. This provides a modeling framework that enables developers to define a conceptual model of a database schema that closely aligns to a real world view of the information. We will also be shipping a new set of data services (codename "Astoria") that make it easy to expose REST based API endpoints from within your ASP.NET applications.

Thanks & Regards,

Arun Manglick || Tech Lead

Friday, December 14, 2007

Display vs Visibility

Showing and hiding a dialog box

Showing or hiding an element can be done by

  • Setting its display property to none or
  • Setting its visibility to hidden.

Display: An element whose display is set to none does not consume space in the layout i.e. the layout of the rest of the element is done as if this element did not exist at all.

Visibility: An element whose display is set to hidden the element is still layed out but it is not shown i.e. the element consumes space. Hence an invisible element still affects the layout.


document.getElementById("DivContent").style.display='none';
and
document.getElementById("DivContent").style.display='block'

Thanks & Regards,

Arun Manglick || Tech Lead

Thursday, December 13, 2007

Easily Converting to Visual Studio 2008

Easily Converting to Visual Studio 2008

http://www.wintellect.com/cs/blogs/jrobbins/archive/2007/11/21/easily-converting-to-visual-studio-2008.aspx

VS2008 has launched and on the pace. You might get into 100 different project files and solutions. The thought of manually opening each one and going through the upgrade wizard didn't sound that appealing. Fortunately, there's an easier way to automatically upgrade a solution or project: the /upgrade switch on DEVENV.EXE (the Visual Studio IDE itself). Using the /upgrade switch is about a million times faster than the wizard!

Steps:

· Begin by opening a Visual Studio Command 2008 Prompt from the Start menu.

· Navigate to your project tree and execute "devenv /upgrade MySoln.sln" (without the quotes).

· Visual Studio will automatically upgrade the solution and all projects as well as create backups.

Thanks & Regards,

Arun Manglick || Tech Lead

Wednesday, December 12, 2007

ASP.NET MVC Framework

ASP.NET MVC Framework:

http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx

Over the years ASP.NET does not have built-in support for developing web applications using a model-view-controller (MVC) based architecture. Microsoft is working towards implementing such framework and will be releasing a public preview of this ASP.NET MVC Framework a little later this year. A fully supported ASP.NET feature in the first half of next year.

Before we proceed further, access the link to know what is MVC.

This is true that the as of now ASP.Net does not having the built-in support for MVC framework. However it is possible to achieve the MVC Framework as below.

· Model: - This section is represented by Data view, Dataset, Typed Dataset, Business components, business entity models etc.

· View: - ASPX, ASCX, or windows application UI like data grid etc. form the view part of it.

· Controller: - In ASP.NET the behind code is the controller. As the events are handled by that part. Controller communicates both with Model as well as view.

But the new built-in-support implements the MVC in slightly a new way and provides more flexibility and loose coupling b/w Model, View & Controller.

A few quick details to share in the meantime about the ASP.NET MVC framework:

  • Enables clean separation of concerns, testability, and TDD by default - All core contracts within the MVC framework are interface based and easily mockable (it includes interface based IHttpRequest/IHttpResponse intrinsics). You can unit test the application without having to run the Controllers within an ASP.NET process (making unit testing fast). You can use any unit testing framework you want to-do this testing (including NUnit, MBUnit, MS Test, etc).
  • Highly extensible and pluggable - Everything in the MVC framework is designed so that it can be easily replaced/customized (for example: you can optionally plug-in your own view engine, routing policy, parameter serialization, etc). It also supports using existing dependency injection and IOC container models (Windsor, Spring.Net, NHibernate, etc).
  • Powerful URL mapping - It includes a very powerful URL mapping component that enables you to build applications with clean URLs. URLs do not need to have extensions within them, and are designed to easily support SEO and REST-friendly naming patterns. For example, I could easily map the /products/edit/4 URL to the "Edit" action of the ProductsController class in my project above, or map the /Blogs/scottgu/10-10-2007/SomeTopic/ URL to a "DisplayPost" action of a BlogEngineController class.
  • Compatabilty with Existing:
    • The MVC framework supports using the existing ASP.NET .ASPX, .ASCX, and .Master markup files as "view templates".
    • It does not, however, use the existing post-back model for interactions back to the server. Instead, you'll route all end-user interactions to a Controller class instead - which helps ensure clean separation of concerns and testability (it also means no viewstate or page lifecycle with MVC based views).
    • Fully supports existing ASP.NET features like forms/windows authentication, URL authorization, membership/roles, output and data caching, session/profile state management, health monitoring, configuration system, the provider architecture, etc.

Thanks & Regards,

Arun Manglick || Tech Lead

MVC Architecture

MVC Framework:


What is a Model View Controller (MVC) Framework?

MVC is a framework methodology that divides an application's implementation into three component roles: models, views, and controllers.

  • "Models" in a MVC based application are the components of the application that are responsible for maintaining state. Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL).
  • "Views" in a MVC based application are the components responsible for displaying the application's user interface. Typically this UI is created off of the model data (for example: we might create an Product "Edit" view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object).
  • "Controllers" in a MVC based application are the components responsible for handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI. In a MVC application the view is only about displaying information - it is the controller that handles and responds to user input and interaction.

One of the benefits of using a MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application. Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated.

The MVC pattern can also help enable red/green test driven development (TDD) - where you implement automated unit tests, which define and verify the requirements of new code, first before you actually write the code itself.


Thanks & Regards,

Arun Manglick || Tech Lead

Monday, December 10, 2007

B'Bye 2007

Do not have time to breath:

While 2007 saw the launch of several different technologies, two primary themes emerged: AJAX and the Microsoft .NET Framework 3.0.

AJAX:

· AJAX was inaugurated as a first-class Web application development platform with the ASP.NET AJAX Extensions.

· The extensions library introduced both Client And Server Libraries that can abstract much of the plumbing detail involved in connecting JavaScript code running in the browser with business logic running on an application server.

· Additionally, the library established a new mechanism for dynamically associating script behaviors with existing controls, provided localization functionality on both client and server, and much more.

The .NET Framework 3.0 introduced three technologies that are already causing a dramatic shift in how applications are architected.

· Windows Presentation Foundation (WPF) took the traditional message-pump, clipping-rectangle architecture of Win32 and flipped it on its head, leveraging advancements in graphic cards and offering Windows developers the declarative, container-based mode of authoring user interface elements long enjoyed by Web application developers.

· Windows Communication Foundation (WCF) pulled together all the various methods of creating a distributed application under one abstraction layer.

· Windows Workflow Foundation (Windows WF) added a declarative workflow modeling language and execution engine, functionality that was previously available only as an additional app, not as a platform service.

Silverlight has been poised to take the world by storm, enabling a richer browser-based user experience than has yet been possible in Web application development. Moreover, with the release of LINQ in Visual Studio 2008 and the ADO.NET Entity Framework (following soon after the Visual Studio release), how we think about data access is about to undergo a major paradigm shift.

When you consider the enormity of the .NET Framework 3.0 and AJAX is it now a best practice to abandon all of those OO practices and split class hierarchies into pairs of logic service classes and data token classes that can easily be passed between remote services.

So if you think you’ll get two seconds to take a breath, keep holding on. J

Thanks & Regards,

Arun Manglick || Tech Lead

Hidden field doubts

Here I need to confirm – Changes made at client side of a Button click, survives after the Post back completes.

Lets assume in a page below is the set of controls.

· lblHide – Expectation is to hide in JS.

· lblDeclarativeValue - Expectation is to change its value in JS

· HiddenField1 - Expectation is to change its value in JS

· TextBox1 - Expectation is to change its value in JS

· Button2 – Makes a postback to see the above chagnes made in JS at Server side

<div id="divHidden" runat="server" style="border:solid 1px red;" title="Hidden Button" >

<asp:Label ID="lblHide" runat="server" Text="Try to Make - Invisible at Cleint Side"></asp:Label><br />

<asp:Label ID="lblDeclarativeValue" runat="server"></asp:Label><br />

<asp:HiddenField ID="HiddenField1" runat="server" />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button2" runat="server" Text="Postback" OnClientClick="SetValue();" OnClick="Button2_Click" />

</div>

Below is the required JS.

function SetValue()

{

// Make Invisible at Client Side and check at serverside if the declarative value has overridden the cleint changess

document.getElementById('lblHide').style.display='none';

// Change the value at Cleint Side and check at serverside if the declarative value has overridden the cleint changes

document.getElementById('lblDeclarativeValue').innerHTML='Changed Declarative';

document.getElementById('HiddenField1').value= 'Change';

document.getElementById('TextBox1').value="Text1";

}

Here check the effect when there is no declarative values been assigned to the controls on the page.

PostBack Results – At server side.

· Render Results - After the postback completes

    • lblHide – Always visible
    • lblDeclarativeValue – Invisible [as no declarative value is assigned]
    • HiddenField1 - 'Change'
    • TextBox1 - Text1

Now see the results when the declarative values are assigned.

<div id="divHidden" runat="server" style="border:solid 1px red;" title="Hidden Button" >

<asp:Label ID="lblHide" runat="server" Text="Try to Make - Invisible at Cleint Side"></asp:Label><br />

<asp:Label ID="lblDeclarativeValue" runat="server" Text="Declarative Label"></asp:Label><br />

<asp:HiddenField ID="HiddenField1" runat="server" Value=" Declarative Hidden " />

<asp:TextBox ID="TextBox1" runat="server" Text="Declarative Text"></asp:TextBox>

<asp:Button ID="Button2" runat="server" Text="Postback" OnClientClick="SetValue();" OnClick="Button2_Click" />

</div>

PostBack Results – At server side.

· Render Results - After the postback completes

    • lblHide – Always visible
    • lblDeclarativeValue – Declarative Label
    • HiddenField1 - 'Change'
    • TextBox1 - Text1

Summary:

· HiddenField/Textbox will survive the changes made at client side.

· Whereas label does not.

Thanks & Regards,

Arun Manglick || Tech Lead

Swap Myth - Value/Refrence Type

In the interview if I ask a simple question – How swap of Value and Referecne type works. Outcome is always unsatisfactory.

Here is the myth.

Suppose you have two Value types and two Reference Types. Lets try to swap them.

Value Types –

string str1 = "Hello";

string str2 = "World";

MessageBox.Show("Before Change: " + str1 + "," + str2); // Hello World

SwapMe( str1, str2);

MessageBox.Show("After Change: " + str1 + "," + str2); // Hello World [Does not works]

· The values of the parameters is passed as ‘By Value’.

· Hence swapping did not worked.

· To success, need to pass the value types By Reference using the ‘ref’ keyword.

SwapMe(ref str1, ref str2);

Reference Types –

Employee obj1 = new Employee(5);

Employee obj2 = new Employee(6);

MessageBox.Show("Before Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 5, 6

Swap.SwapMe(obj1, obj2);

MessageBox.Show("After Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 5, 6 [Does not works]

· Before we proceed, just notice that the value of the parameter is the address of the object (Pointer to the object) and not the value of object.

· The address is passed as ‘By Value’ and not ‘By Reference’. (Might be feeling strange).

· Hence swapping the address would not affect the actual addresses.

· But if you need to do this, pass it using ‘ref’.

Swap.SwapMe(ref obj1, ref obj2);

But at the same time instead of swapping if you want to change the values as below, then it will work perfectly fine without the use of ‘ref’ keyword.

Employee obj1 = new Employee(5);

Employee obj2 = new Employee(6);

MessageBox.Show("Before Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 5, 6

ChangeMe s(obj1, obj2);

MessageBox.Show("After Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 6, 5 [Does works]

· As above described value of the parameter is the address of the object (Pointer to the object) and not the value of object.

· The address is passed as ‘By Value’ and not ‘By Reference’.

· But code is dereferencing the objects and then changing its value.

· Hence it will change to the actual object, without the need of using ‘ref’.

Supporting fucntions:

// public static void SwapMe(ref Employee a, ref Employee b)

public static void SwapMe(Employee a, Employee b)

{

Employee temp;

temp = a;

a = b;

b = temp;

}

//public static void SwapMe(ref string a, ref string b)

public static void SwapMe(string a, string b)

{

string temp;

temp = a;

a = b;

b = temp;

}

public static void ChangeMe(Employee a, Employee b)

{

a.i = 55;

b.i = 66;

}

Thanks & Regards,

Arun Manglick || Tech Lead