We all want when we visit a website to have some unique treatment (personalisation). Developers on the other side must devise mechanisms to remember our personal settings and preferences. This makes it more likely for us to return. ASP.Net developers must track users and save somehow their preferences between visits.
In this post I would like to present you with a very simple example on how to create a simple web service that access data from a database.
In this post I am going to explain how to get information about the user's browser. This is the browser that the user uses to access the web application.
In this post I will talk about ASP.Net State management. I will provide a hands on example with VS and VB.Net.
In this post i am going to try to give you an overview of the events that take place in asp.net lifecycle. Page Life Cycle in ASP.NET is essential knowledge for developing applications.
This is the third post in a series of posts regarding web server controls.
In this second post of presenting the basic web server controls in ASP.Net we will look into the Adrotator control. You can find the first post here.
I think that people who begin now with ASP.Net will find these posts useful.
We can use the Adrotator control and to serve up random banners in our web site.
I will use a hands-on an example with C# version.I will use an XML file that will serve as the source for my banners.
The AdRotator was included in ASP.Net 2.0 version.
I will be using VS 2010 Ultimate edition to create a simple asp.net application.
1) Launch Visual Studio 2010/2008/2005 (or express editions).
2) Create a new website and choose an approriate name.
3) From your toolbox just drag and drop an AdRotator Control in the Default.aspx page
5) Click on your project from the Solution explorer and create an Images folder
6) Place your random images in the folder above
7) Add a new item to your site. IName it “advs.xml”. Place it in the App_Data special folder that is already created in your project structure.
8) Copy and paste the following XML code inside the “advs.xml” file.
<?xml version=”1.0″ encoding=”utf-8″ ?>Make sure you change the name of the images in the <ImageUrl> attribute to reflect the images you placed in the Images folder in your project.
<AlternateText>ASP.NET AJAX is a free framework for quickly creating efficient and interactive Web applications that work across all popular browsers</AlternateText>
<AlternateText>ASP.NET is a free technology that allows anyone to create a modern web site</AlternateText>
<AlternateText>The .NET Framework is Microsoft’s comprehensive and consistent programming model for building applications </AlternateText>
9) Now select the AdRotaror control and from the properties window in VS select the property called AdvertisementFile .Click on the ellipsis button and navigate until you find the “advs.xml” file.
10) Run your application and observe all the banners specified in the .xml file appearing randomly according to their weight (Impressions attribute).
If you want to enable some sort of filtering in your ads, you can do so.
If you place a DropDownlist control in your default.aspx page and add as items these names
- asp.net ajax
Enable AutoPostBack property for this control.By clicking on this control in the Default.aspx.cs,
You will be presented with the event handling routine for the standard event(SelectedIndexChanged).
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
AdRotator1.KeywordFilter = DropDownList1.SelectedItem.Value;
We set the property Keywordfilter of the AdRotator control to the value of the selected item from the list, thus selecting only the banners that match the keyword.
Run your application and choose different items from the dropdownlist . In my example there are 3 banners with 3 different keywords so only one banner will be shown every time you choose a different item.
I would like to show you how you can retrieve the ads if we have a Sql Server database as our source file.
1) First we need to create the database . Select the App_Data special folder and add a new item, a Sql Server Database. Call it banners.mdf.We need to create a table and we will call it ads
The definition for this table, for fields and data types could be
ImageUrl = ~/images/asp-logo.png
AlternateText=ASP.NET is a free technology that allows anyone to create a modern web site
ImageUrl = ~/images/ajax-logo.gif
AlternateText=ASP.NET AJAX is a free framework for quickly creating efficient and interactive Web applications that work across all popular browsers
ImageUrl = ~/images/NET-logo.png
AlternateText=The .NET Framework is Microsoft’s comprehensive and consistent programming model for building applications
Now create a new .aspx page and make it the Start page.Name it as you want.
Place a new AdRotator control in the newly created page.
Now we must have a datasource control that points to the database we created and tie this datasource control with the rotator control.
Click on the little arrow in the rotator control and choose New Data Source, choose database from the window and then from the Configure DataSource window, select the banner database and click Next.
Then select the ads table and select all the fields from the table. Test the query and click Finish.
Run your application and see the ads to apper now from the database.
You can email me if you want the source code or leave a comment.
Hope it helps!!!!
I am going to write a new series of blog posts regarding the most common web server controls we use in ASP.Net applications.Lots of people that start now in ASP.Net want to know more about these standard controls.
I am thinking to continue the LINQ series of posts but I want to give a clear example on Linq to Objects.
I am going to write a new series of articles regarding LINQ to XML and how to use it in ASP.Net web sites.
In this post I will try to show you with a detailed example how to create an Ajax enabled Web Service and consume it from client scripts running in a different Web site. It will be a simple example but the principles apply to the most complicated ones.
In this post I would like to talk about cross-page postbacks in ASP.Net.
This is a second post in a series regarding ASP.Net and manipulation of files & folders. You can find the other one here .
I am going to start a new series of posts regarding files and how we can manipulate them in our ASP.Net applications. I will also present samples of .Net code that will exhibit how to get information on drives and folders.
Scott Gu in his latest post, announces a very serious security threat-vulnerability regarding ASP.Net sites.
I wanted to write a few thoughts of mine regarding the new lightweight development tool available from Microsoft called WebMatrix.
I know that this topic is also well documented in the web. But I decided to have a go, after so many people ask me the same question
In this post we are going to see a step by step example on how to read,write,delete cookies in asp.net websites.
This is my second post regarding caching in asp.net applications. You can find my other post in caching here.
The issue of "caching" keeps coming up in my asp.net seminars so I though it would be a good idea to write a post. I know it is a well documented feature and widely used but I really believe most people will be able to use this rather simple example and understand the concepts behind caching.
You can use any of the versions of Visual studio (VS 2005,VS2008,VS2010 express editions work fine).
I will use VS 2010 Ultimate edition and VB.net as the .Net language of choice.
1) Create an Asp.net web site and name it as you want.
2) In the default.aspx page add a new paragraph or header with content of your choice.In my case i have added something like this
<h2>Caching is really useful!!!!</h2>
3) Let's think simple at first. We do want to cache the entire page (default.aspx)
Just below the Page directive ( top of the page ) you add the following bit of code:
<%@ OutputCache Duration="20" VaryByParam="none" %>
We do cache the page for 20 seconds. That is determined by the Duration Attribute.
4) Save your work and run your application. The first time we see the output of the asp.net page, this is served directly from the asp.net engine.
If you hit refresh for a period for less than 20 seconds the page is served from the Cache.
5) Let's see a more complex scenario involving query strings and how we can make a decision on whether to cache the page or not based on the value of the query string. Add a new label control to the .aspx page
<asp:Label ID="mylabel" runat="server"></asp:Label>
In the Page_Load event handler routine add the code
If (Not Request("id") Is Nothing) Then
mylabel.Text = " The query string value is " + Request("id").ToString()
6) Change the OutputCache directive to
<%@ OutputCache Duration="20" VaryByParam="id" %>
7) Run your application and type in the url address bar http://localhost:6141/caching/Default.aspx?id=5. See the results. If you hit refresh right away the page is refreshed from the cache and it is not reloaded.
If you type this http://localhost:6141/caching/Default.aspx?id=6 ( within the 20 seconds period ) it senses that the id has changed now and it re-renders the page. It also sets a new caching period of 20 seconds.
8) What is really interesting to bear in mind ( and not many people know ) is that you can have different caching settings depending on the client's browser.The first step is to change the OutputCache directive to this
<%@ OutputCache Duration="20" VaryByParam="id" VaryByCustom="browser" %>
9) Basically we say "Continue to cache the page as long as the clients use the same browser". So if you have 3 people in a row that have IE8 hitting your page, will all get the same cached version of the page.
If someone hits our page with Firefox or Chrome, they will get a brand new rendered page. At this time it will create a 20 seconds cache window period specific for each browser.This functionality is built into the caching module, and will insert separate cached versions of the page for each browser name and major version.
10) Now if we need to apply the caching techniques we just learnt in all pages of our asp.net application we will have to make a few modifications in the web.config file.We will add something that is called "Cache Profiles"
Just below the <system.web> add this snippet
So we have created a new profile that is called "basic". Now let's apply this cache profile to one or more pages in our website.
So we switch back to our default.aspx page and in the OutputCache directive, type
<%@ OutputCache CacheProfile="basic" %>
Run your application again and you will see that we do have the same caching effects.
Obviously you can create different cache profiles and apply them to various pages.
I will have more posts on caching shortly.
Hope it helps!!!
This is a second post in a new series of posts regarding ASP.Net and JQuery. You can see the first post here.