Tuesday, 26 April 2011

Visual Studio 2010 and .Net 4.0


Now, VS2010 and .Net 4.0 have been launched. So, for the study purpose my following blog will be helpful.
The following are the new features, which help to .net developers to work with .net 4.0 and visual studio 2010.
1. Implicit Line Continuation (New to VB.NET)
Dim text As String = “Wouldn’t it be nice” & _
“If you didn’t have to” & _
“put an underscore to” & _
“continue to the next line?”
Dim text As String = “Wouldn’t it be nice” &
“If you didn’t have to” &
“put an underscore to” &
“continue to the next line?” &
“Sweet! Now you can!”
2. Named And Optional Parameter (New to C#)
public void CreateBook(string title=”No Title”, int pageCount=0, string isbn = “0-00000-000-0″)
{
this.Title = title;
this.PageCount = pageCount;
this.ISBN = isbn;
}
CreateBook();
CreateBook(“Some Book Title”);
CreateBook(“Some Book Title”, 600);
CreateBook(“Some Book Title”, 600, “5-55555-555-5″);
CreateBook(isbn: “5-55555-5555-5″);
CreateBook(“Book Title”, isbn: “5-55555-5555-5″);
CreateBook(isbn: “5-55555-5555-5″, title: “Book Title”, pageCount: 600);
3. From Keyword (New to VB.NET)
Dim Colors As New List(Of String)
Colors.Add(“Red”)
Colors.Add(“Green”)
Colors.Add(“Blue”)
It can now be reduced down to this:
Dim Colors As New List(Of String) From {“Red”, “Green”, “Blue”}
4. Array Type Inference (New to VB.Net)

Dim Numbers = {1, 1, 2, 3, 5, 8, 13, 21, 34} /*Here, Numbers is integer type*/
Dim Numbers = {1, 1, 2, 3, 5.0, 8, 13, 21, 34} /*Here, Numbers is double type*/
/*No need to Explicit Type cast*/
5. Compiling without Primary Interop Assemblies (New to C# and VB.NET)
Primary Interop Assemblies (PIA) are vendor-provided assemblies that act as a go between for COM components and the .NET Framework, one of the most widely known being the Microsoft Office Primary Interop Assemblies.
Any time you deploy an assembly that references a PIA, you have to remember to deploy the PIA along with it or provide instructions for how to go about getting it on the system. A new feature for both C# and VB.NET allows you to embed a Primary Interop Assembly directly into your assembly, which makes deployment extremely simple. PIAs also tend to be relatively large, so including the whole thing could easily bloat your assembly. Fortunately, the compiler is optimized to embed only those parts of the PIA that you actually use, which can reduce the PIA footprint if you are only using a subset of what it contains.
6. Omitting Ref Parameters (New to C#)
Another by-product of COM APIs is that a great number of method parameters have to be passed by reference.
Most of the time you really just want to pass a value into the method and you don’t really care what comes out. Nonetheless, you still have to create a number of temporary values to hold the result. The tedium of this task could easily be passed to an intern to do claiming its “real world experience”.
In C# 4.0, you can now pass parameters by value into COM method calls and the compiler will automatically generate temporary variables for you, which saves a great deal of time, and saves the intern for other “real world” tasks.
For Example,
Before
static void Main()
{
Word.Application app = new Word.Application();
object m = Type.Missing;
app.Documents.Add(ref m, ref m, ref m, ref m);
}
With Framework 4.0
static void Main()
{
Word.Application app = new Word.Application();
// (1) Your optional-parameter-omitted initial call.
app.Documents.Add();
}
7. What Is DLR ?
DLR (Dynamic language runtime) is set of services which add dynamic programming capability to CLR. DLR makes dynamic languages like LISP, Javascript, PHP,Ruby to run on .NET framework.
>>Dynamic KeyWord
Dynamic keyword allows you to declare and use types that are not type-checked during compilation. They are resolved at run time using the DLR.
It can be useful while we are doing programming with com,JavaScript which can have run time properties.
Advantages :
• Helps you interop between dynamic languages.
• Eliminates bad reflection code and simplifies code complexity.
• Improves performance with method call caching.
Disadvantages:-
• Will hit performance if used with strongly typed language.
>> Dynamic Metadata
With the release of Microsoft .Net Framework 4.0 it is now possible to change meta tags programmatically in the html code of any ASP.NET page. Meta tags are HTML elements used to store metadata about a web page. They are mostly used by search engines like Google or Bing.
In the new ASP.NET 4.0 you can change description and keywords metatags in your C#/VB code. In order to do it all you have to use is two properties of the Page class called “MetaDescription” and “MetaKeywords”.
protected void Page_Load(object sender, EventArgs e)
{
Page.MetaDescription = “This page contains information about asp.net 4.0″;
Page.MetaKeywords = “ASP.NET 4.0,Search Engine Optimization”;
}

This is rendered as
<meta name=”description” content=”This page contains information about asp.net 4.0″ />
<meta name=”keywords” content=”asp.net 4.0, Search Engine Optimization” />
It’s much easier to use this approach because you can always change metadata dynamically based on the information you’re getting from users or data storage.
>> Advantage /Disadvantage of DLR
Advantages :-
• Helps you interop between dynamic languages.
• Eliminates bad reflection code and simplifies code complexity.
• Improves performance with method call caching.
Disadvantages:-
• Will hit performance if used with strongly typed language.
8. Improvement in Code Expression
We commonly use the following syntax to output a value from aspx page called code expression.
<%= HTMLOutput %>
Asp.Net 4.0 version that will html encode the value.
<%: HTMLOutput %>
The above code expression will output the value after doing html encode. i.e. the above expression is equivalent to
<%= HttpUtility.HtmlEncode(HTMLOutput) %>
9. Snippets for HTML and JavaScript
HTML, ASPX and JavaScript files now have full support for Visual Studio’s snippets.  No more having to type runat=”server” on every control.  There are hundreds of new snippets included in Visual Studio 2010.
10. Packaging and Deployment
Visual Studio 2010 now has the ability to completely package and deploy a web application including all it’s IIS settings, Databases and Application logic.
You can even include custom database scripts to run when your package is installed.
As part of the packaging process you can also run custom transforms on web.config that allow you update any section of web.config with custom settings unique to the deployment.
For example you can replace database connection strings and web service end points.
11. ViewState Mode
Till ASP.Net 3.x, we have EnableViewState property both at Page level and server control level to control the view state. Disabling the ViewState at page level will disable the viewstate to all the page controls.
To overcome this difficulty, asp.net 4.0 added a new property to Page object and controls called ViewStateMode.
1.      Enabled
This value will enable the view state. This is the default value for the Page object.
2.      Disabled
This value will disable the ViewState
3.      Inherit
This value will make the control to inherit the setting of the parent. This is the default value for a control.
With this property, we can disable the ViewState for the page and enable it for the control if only required.
12. ClientID Generation for ASP.Net Controls
In order to access a server control from a client side script, a developer will require getting its client id. Predicting the ClientID of any control that is packed inside a parent like UserControls, MasterPage or any DataBound controls is a challenging task till day.
ASP.Net 4.0 addresses this difficulty by providing a new property called ClientIDMode for every controls and Page object. We can also set the property in configuration files.
This property will take the following 4 values,
1.      Static
Setting this value will make the ClientID same as the ID. It will not concatenate ID of the parent naming containers.
2.      Predictable
This will be useful to predict the ClientID of child controls in data controls. Setting this property will prevent the “ctlxxx” prefix in the ClientID.
3.      Legacy
This value will make the ClientID generation same as earlier versions of ASP.Net
4.      Inherit   – Default Property
This value will make the control to inherit the parent control’s setting.
For Example :
<%@ Page Title=”" Language=”C#” MasterPageFile=”~/MasterPage.master” ClientIDMode=”Static”  AutoEventWireup=”true” CodeFile=”ClientIDs.aspx.cs” Inherits=”Default2″ %>
To set this property in Web.Config file/Machine.config file,
<pages clientIDMode=”Static”></pages>
13. Clean Web.Config Files
You can find it in new project template
14. Permanent Redirect in Asp.Net 4.0
One of the most important things for web 2.0 sites are to be searchable,For search engine Optimization, every small aspect of the page is Important. One of the important things to do for this is to make sure all permanent redirection in your site is made with the 301 Host headers.
The HTTP response status code 301 Moved Permanently is used for permanent redirection.
This status code should be used with the location header.
public static void PermanentRedirect(this HttpResponse response, string newUrl)
{
response.Status = “301 Moved Permanently”;
response.StatusCode = 301;
response.AddHeader(“Location”, newUrl);
}
Now in .Net 4.0 we can redirect by following code
Respose.PermanentRedirect(newUrl);
15. CheckBoxList and RadioButtonList Control Enhancements
In ASP.NET 3.5, you can specify layout for the CheckBoxList and RadioButtonList using the following two settings:
Flow.  The control renders span elements to contain its content.
Table.  The control renders a table element to contain its content.

<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” RepeatLayout=”Flow”> <asp:ListItem Text=”CheckBoxList” Value=”cbl” /> </asp:CheckBoxList> <asp:RadioButtonList runat=”server” RepeatLayout=”Table”> <asp:ListItem Text=”RadioButtonList” Value=”rbl” /> </asp:RadioButtonList>
Render HTML

<span id=”CheckBoxList1″><input id=”CheckBoxList1_0″ type=”checkbox” name=”CheckBoxList1$0″ /><label for=”CheckBoxList1_0″>CheckBoxList</label></span> <table id=”RadioButtonList1″ border=”0″> <tr> <td><input id=”RadioButtonList1_0″ type=”radio” name=”RadioButtonList1″ value=”rbl” /><label for=”RadioButtonList1_0″>RadioButtonList</label></td> </tr> </table>

In ASP.NET 4.0, the CheckBoxList and RadioButtonList controls support the following new values for the RepeatLayout property:
OrderedList — The content is rendered as li elements within an ol element.
UnorderedList — The content is rendered as li elements within a ul element.
<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” RepeatLayout=”OrderedList”> <asp:ListItem Text=”CheckBoxList” Value=”cbl” /> </asp:CheckBoxList> <asp:RadioButtonList ID=”RadioButtonList1″ runat=”server” RepeatLayout=”UnorderedList”> <asp:ListItem Text=”RadioButtonList” Value=”rbl” /> </asp:RadioButtonList>

Render HTML
<ol id=”CheckBoxList1″> <li><input id=”CheckBoxList1_0″ type=”checkbox” name=”CheckBoxList1$0″ value=”cbl” /><label for=”CheckBoxList1_0″>CheckBoxList</label></li> </ol> <ul id=”RadioButtonList1″> <li><input id=”RadioButtonList1_0″ type=”radio” name=”RadioButtonList1″ value=”rbl” /><label for=”RadioButtonList1_0″>RadioButtonList</label></li> </ul>
Note: If you set RepeatLayout to OrderedList or UnorderedList, the RepeatDirection property can no longer be used and will throw an exception at run time if the property has been set within your markup or code. The property would have no value because the visual layout of these controls is defined using CSS instead.
16. Control Rendering
In .Net 4.0 Framework a new property called controlRenderingCompatibilityVersion is added to the Pages element of configuration files.
<pages controlRenderingCompatibilityVersion=”3.5|4.0″/>
The value “3.5” indicates the controls are rendered in legacy ways. The value “4.0” indicates the control will be rendered with the latest improvements of 4.0 framework.
ASP.Net 4.0  rendering
disabled attribute for only INPUT tags and
a CSS class called “aspNetDisabled” for other controls
to disable it when controlRenderingCompatibilityVersion property is set to “4.0”.
Below you can find a HTML rendered for a Label and TextBox control when controlRenderingCompatibilityVersion property is set to “4.0”.
<span id=”Label1″>Label</span>
<input name=”TextBox1″ type=”text” id=”TextBox1″ disabled=”disabled” />
In HTML 4.0 and XHTML 1.1, the span element does not support the disabled attribute.
17. Filtering With QueryExtender Control
QueryExtender control supports a variety of filter options.
  • Search
  • Range
  • PropertyExpression
  • CustomExpression
  1. Search Filter
    For the search option, the QueryExtender control uses provided text to find records in specified fields. In the following example, the control uses the text that is entered in the TextBoxSearch control and searches for its contents in the ProductName and Supplier.CompanyName  columns in the data returned from the LinqDataSource control. <asp:LinqDataSource ID=”dataSource” runat=”server”> TableName=”Products”>
    </asp:LinqDataSource>
    <asp:QueryExtender TargetControlID=”dataSource” runat=”server”>
    <asp:SearchExpression DataFields=”ProductName, Supplier.CompanyName”
    SearchType=”StartsWith”>
    <asp:ControlParameter ControlID=”TextBoxSearch” />
    </asp:SearchExpression>
    </asp:QueryExtender>
  2. Range Filter
    The range option is similar to the search option, but specifies a pair of values to define the range. In the following example, the QueryExtender control searches the UnitPrice column in the data returned from the LinqDataSource  control. The range is read from the TextBoxFrom and TextBoxTo controls on the page. <asp:LinqDataSource ID=”dataSource” runat=”server” TableName=”Products”>
    </asp:LinqDataSource>
    <asp:QueryExtender TargetControlID=”dataSource” runat=”server”>
    <asp:RangeExpression DataField=”UnitPrice” MinType=”Inclusive”
    MaxType=”Inclusive”>
    <asp:ControlParameter ControlID=”TextBoxFrom” />
    <asp:ControlParameter ControlID=”TexBoxTo” />
    </asp:RangeExpression>
    </asp:QueryExtender>
  3. PropertyExpression
    The property expression option lets you define a comparison to a property value. If the expression evaluates to true, the data that is being examined is returned. In the following example, the QueryExtender control filters data by comparing the data in the Discontinued column to the value from the CheckBoxDiscontinued control on the page.

    <asp:LinqDataSource ID=”dataSource” runat=”server” TableName=”Products”>
    </asp:LinqDataSource>
    <asp:QueryExtender TargetControlID=”dataSource” runat=”server”>
    <asp:PropertyExpression>
    <asp:ControlParameter ControlID=”CheckBoxDiscontinued” Name=”Discontinued” />
    </asp:PropertyExpression>
    </asp:QueryExtender>
  4. CustomExpression
    Finally, you can specify a custom expression to use with the QueryExtender  control. This option lets you call a function in the page that defines custom filter logic. The following example shows how to declarative specify a custom expression in the QueryExtender control.

    <asp:LinqDataSource ID=”dataSource” runat=”server” TableName=”Products”>
    </asp:LinqDataSource>
    <asp:QueryExtender TargetControlID=”dataSource” runat=”server”>
    <asp:CustomExpression OnQuerying=”FilterProducts” />
    </asp:QueryExtender>
    protected void FilterProducts(object sender, CustomExpressionEventArgs e)
    {
    e.Query = from p in e.Query.Cast<Product>()
    where p.UnitPrice >= 10
    select p;
    }
18. Visual Studio 2010
Zooming facility as well as intelligence is really good in visual studio 2010.

No comments:

Post a Comment