|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis tutorial doesn’t cover the theoretical basics of the new features and controls, instead it concentrates on the practical example of using the new features. This tutorial covers the following features:
Requirements
ProjectOur project is quite simply, it's just a guestbook, but it's good for demonstrating the cool features of ASP.NET 3.5 and .NET 3.5 in action.
So, just open Visual Studio and create a new C# website. Database designWe need to create a database when the comments will be stored. We’ll just create a database with a single table that will keep the comments posted by the authors. We can create it in Server Explorer without leaving Visual Studio.
Don’t forget to set the Id field as an identity. Let’s populate the database as well, so that we’ll have some data to display, just execute these queries: INSERT INTO Comments
(Author, Text)
VALUES('John', 'ASP.NET 3.5 rocks!');
INSERT INTO Comments
(Author, Text)
VALUES('Mary', 'ASP.NET 3.5 is so cool!');
LINQThen, we need to create a
Then we open
App_Code\Comments.dbml\Comments.designer.cs
LinqDataSource
Ok, it’s a time for create your webform that will hold all the
content. Actually, it is already created by Visual Studio, we just have
to add some content there.
Drag it to the webform. You will get the following code: <asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>
Give it a more appopriate name, something like
Choose the only avaliable data context object and click Next.
Then leave everything by default by click on the
Click Ok and finish, switch to the Source view and look at the code we get: <asp:LinqDataSource ID="dsComments" runat="server"
ContextTypeName="CommentsDataContext" EnableInsert="True" OrderBy="Id desc"
TableName="Comments">
</asp:LinqDataSource>
Pretty simple, I would say. ListView It’s time to add the <asp:ListView ID="lvComments" runat="server" DataSourceID="dsComments">
</asp:ListView>
<asp:ListView ID="lvComments" runat="server" DataSourceID="dsComments">
<LayoutTemplate>
<h1>ASP.NET Guestbook</h1>
<div id="itemPlaceholder" runat="server"></div>
</LayoutTemplate>
</asp:ListView>
Please, note the we have the div tag with id="itemPlaceholder" runat="server". They denote that ListView content will be placed in the place of that div, itemPlaceholder is the default ID of the content placeholder.
To display out data we should add the <ItemTemplate>
<div>
<b><%# Eval("Author") %></b> says<br />
<%# Eval("Text") %>
</div>
</ItemTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>
First, add <InsertItemTemplate>
Name:<br />
<asp:TextBox ID="txtBox" runat="server" Text='<%# Bind("Author") %>' /><br />
Text:<br />
<asp:TextBox ID="txtText" runat="server" Text='<%# Bind("Text") %>'
TextMode="MultiLine" /><br />
<asp:Button runat="server" CommandName="Insert" Text="Post" /><br />
</InsertItemTemplate>
Now compile the project and try posting some comments. It’s working that is good. DataPager
It’s time to add a pagination and see another new control in action. It
is called <asp:DataPager runat="server" PagedControlID="lvComments" PageSize="5">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
I
suppose it’s pretty self-explanatory, I guess you noticed that it has an ugly postback in its URL. You can easily get rid of it by adding the <asp:DataPager runat="server" PagedControlID="lvComments" PageSize="5"
QueryStringField="page">
It looks much better now and you can send the link to the 10th page of your guestbook to a friend. ASP.NET AJAXAt the final stage, let’s add some AJAX, so that the page doesn’t reload each time you post a comment.
<asp:UpdatePanel runat="server">
<ContentTemplate>
<% List View code %>
</ContentTemplate>
</asp:UpdatePanel>
Points of InterestThere are a lot of cool features in ASP.NET that help you make your applications much faster than in ASP.NET 2.0. We have build a guestbook without writing a line of C# code. Of course this tutorial wasn't mean to cover all the features of ASP.NET 3.5, it doesn't cover the basics things, however you can find them on MSDN and on ww.asp.net.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||