jump to navigation

ASP.NET 4.0,SEO and meta tags September 27, 2009

Posted by fofo in ASP.NET 4.0, Visual Studio 2010.
Tags: ,
add a comment

I am thinking to create a new series of posts regarding ASP.NET and SEO (Search Engine Optimisation). I am going to start with this post , talking about some new features that make our asp.net apps more SEO friendly. At the end of the day, there is no point having a great application and somehow “scare” the search engines away. This is going to be a short post so let’s quickly have a look at meta keywords and ASP.NET 4.0.

Meta keywords and description are important elements of a page and make it search engine friendly. ASP.Net 4.0 added 2 new properties on the Page object to let us define the Meta Keywords and Description.

Create a simple asp.net application using Visual Studio 2010. In the Default.aspx.cs code behind file type

Page.MetaKeywords = “asp.net,vb,c#,css,html,”;

Page.MetaDescription = “This is my blog that focuses on ASP.NET.”;

Alternatively we can add those two meta tags in the Page directive

<%@ Page Language=”C#” MetaKeywords=”asp.net,vb,c#,css,html” MetaDescription=”This is my blog that focuses on ASP.NET.” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

Run your application. Go to View->Source and see the meta tags

</title><meta name=”description” content=”This is my blog that focuses on ASP.NET.” /><meta name=”keywords” content=”asp.net,vb,c#,css,html” /></head>

Hope it helps!!!

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl


ASP.NET 4.0 Entity DataSource and GridView September 27, 2009

Posted by fofo in ASP.NET 4.0, c# 4.0, Visual Studio 2010.
Tags: , , , ,

Recently I had the time to examine thoroughly my blog stats. From that it was evident people liked a lot the posts regarding the new features in .Net 4.0. So in this post I am going to discuss the new Entity DataSource web server control which is  similar to the LINQ to SQL DataSource, except it allows you to use your Entity Framework data object model instead. I will also demonstrates the new features of the GridView control. I have talked about EF and how to build an application with EF and C# in one of my previous posts. In this post I will talk more about the Entity Datasource object and enhancements made in the GridView control.

I will create a sample website as always so it is easier for you to understand.In order to follow along you must have installed in your PC VS 2010 and the .NET 4.0 framework.Obviously we are going to need a data store for our application. I will use the AdventureWorksLt which you can download from this site .

1) Launch Visual Studio 2010 and create a new web site.

2) Choose Asp.Net application from the available templates and C# as the development language

3) You now have your files in the Solution Explorer window.

4) Choose the default.aspx page and go to your Toolbox and under the Data Controls drag and drop an Entity Datasource control on the page.

5) Now you need an Entity Data model that the Entity Datasource can bind to. In the Solution Explorer add (right-click) a new special folder , App_Code

6) Select the App_Code and add a new item(right-click) , ADO.NET Entity Data Model.Leave the default name.

7) In the next window select “Generate from database” and click Necxt.

8) In the next window of the Entity model wizard create a new connection to you database (AdventureWorksLT)or select an existing connection that points to the same db.

9) Save the entity connection settings in the web.config file

10) Click Next on the wizard window. From all the available db objects we want to include in our model only tables and more specifically only the Products table.

11) Select the table and click Finish. So now we have a Product Entity with all the mappings to the database.

12) Select the Entity Datasource control and click the arrow on the right-top corner and select Configure Data Source.

13) Select in the Named Connection option of the wizard and select the AdventureWorkLTEntities and click Next

14) In the EntitySetName select Product and from the available Entity values select ProductID,Name,ListPrice,Size,Weightand and click Finish.

15) Drag and drop on the default.aspx page a Gridview control (control in your Toolbox data controls area).Give it some formatting from the AutoFormat options

16) Set the datasource of the Gridview control to the Entity datasource.

17) Enable Sorting,Paging and Selection for the Gridview control

18) Run your application by hitting F5 from your keyboard. If everything is ok you will be able to see a list of products in your page.

The imporevement in the GridView control is about selecting rows.If you selected an item in GridView(the third one for example) and then browsed through the pages the third item in every page will also be selected. That was not always what we wanted. The GridView control did that based on an index it had and used to find the third item on that page.

We can overcome that by enabling the EnablePersistenSelection property and setting it to True.

Then we need to select the DataKeynames property and set it to ProductID.

19) Now if run our application again and select a product in the first page(6th product), and browse through the pages the product in position 6(index 6) is not selected anymore.Now the selection is made over the underlying primary key and not the index in the view of the GridViewControl

Hope it helps!!!

If you need the source code just email me or just comment on this post.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl

ASP.NET 4.0 and ClientID Mode September 20, 2009

Posted by fofo in ASP.NET 4.0, C#, c# 4.0, Visual Studio 2010.
Tags: ,
1 comment so far

In this post I will be continuing my series of posts regarding ASP.NET 4.0 enhancements.It is now possible to set the name of your controls ClientID attribute, thanks to the ClientIDMode property. Developers requested a better way of setting names of the web server controls and with the new version of ASP.NET 4.0, this is now a reality.

We can demonstrate this with a new project.I will be using C# and VS 2010.

1) Launch Visual Studio 2010

2) Create a new website, select ASP.NET Web site from the available templates. Save your website by giving it an appropriate name. e.g Clientid

3) Create a master page and add it to the page.Leave the default name.

4) Create a new web user control.Leave the default name.

5) In the WebUserControl.ascx add two web server controls,a label control and a textbox control

6) Add the web user control to the asp.net page by typing in the directives section(top of the page)

<%@ Register src=”WebUserControl.ascx” tagname=”WebUserControl1″
tagprefix=”uc1″ %>

7) Run your application. When the default.aspx page is render by the browser, go to View->Source and you will see something like this:


<span id=”WebUserControl11_Label1″>what is your name?</span>
<input name=”WebUserControl11$TextBox1″ type=”text” id=”WebUserControl11_TextBox1″ />


This is how the controls take their name. The first part is the name of the parent control and the second part is the name of the control itself. If you have many nested elements you end up with messy control names.If you wanted to access these controls in external Javascript files, you had no other alternative but to use these names.

8) Switch to the WebUserControl.ascx file in VS 2010, and change the ids of the label and textbox control. For example

<asp:Label ID=”namelabel”
<asp:TextBox ID=”txtlabel”

9) Then we set the ClientIDMode to static for both controls. Save your work and run the application

10) When the default.aspx page is render by the browser, go to View->Source and you will see something like this:

<span id=”namelabel”>what is your name?</span>
<input name=”WebUserControl11$txtlabel” type=”text” id=”txtlabel” />

It is evident that now we have the ID values that we want , and it much easier and handy to use.

The other values for the ClientIDMode are:

  • Legacy: This is equivalent to the ClientID property behavior for earlier versions of ASP.NET. This is also the default value.
  • Predictable:This is used in data controls that use repeating templates. It uses ID attributes of the parent control’s naming containers, but generated IDs do not have names that contain strings like “ctlxxx”.
  • Inherit: This specifies that a control’s ID generation is the same as its parent.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl

Viewstate management and ASP.Net 4.0 June 27, 2009

Posted by fofo in ASP.NET 4.0, VS 2010.
Tags: ,

For people who have been following this blog for some time, they know my favourite subject is ASP.NET.

I think this is the best platform for building web applications nowadays. In ASP.NET 4.0 we have some very interesting new enhancements and new features.

I will try to talk about all of them in separate posts since some people have been complaining that my posts are too long.

In this post I will talk about Viewstate management in ASP.NET 4.0.

Let’s have a look first what really Viewstate is and what it really does.Unless you move to a new paradigm for building web applications like ASP.NET MVC you must have a good knowledge of Viewstate.

Viewstate represents the state of the page when it was last processed on the server. We use it to retain values across two successive requests for the same page. It is a hidden field added to the page and is restored on the server before the page request is processed. The Viewstate information travels back and forth with the page itself, but contains no visual elements that need to be rendered. So you must be thinking that this is fantastic news for the web developer. But (there is always a but), the extra information in the Viewstate adds to the size of the page and the information that travels down the wire. I have seen web pages that have an additional 20Kbytes of Viewstate information.

Every .aspx page derives from the Page class or Page control and extends it. The default value for the EnableViewState property for the page is True. Through that property we can get or set(enable-disable)  the view state of the control.

If we disable ViewState on page level, you cannot enable it on child controls. if you go to the web.config and type

<pages enableViewState=”false”>

then if you try to enable it on child controls is impossible.

In ASP.NET 4.0 we have the ViewState property. There are 3 values for this property:enabled, disabled, inherit .

We can use this property to enable Viewstate for an individual control even if Viewstate is disabled for the page. We can disable the Viewstate for a control even if it is enabled on a page level.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl