Try Tuts+ Premium, Get Cash Back!
Screencast
videos

How to Search a Website Using ASP.NET 3.5 – screencast

I’m happy to say that today, we are posting our very first article on ASP.NET. In this screencast, I’ll show you how to implement a simple search functionality into your personal website. We’ll go over many of the new features in ASP.NET 3.5, such as LINQ and many of the AJAX controls that ship with Visual Studio/Web Developer.

*Click on the full screen icon maximize the video.

Mission Statement

We will be building a simple search functionality for our site. We’ll create a bare bones site that contains a single textbox and button. When the button is clicked, we’ll write some LINQ code that will retrieve the applicable information from our database and display it on the page. Additionally, we’ll allow for partial page post-backs by using the Update Panel and Update Progress controls.

What You Need to Know

In this screencast, I will assume that you have some knowledge of the framework. So, though I will explain everything to the best of my ability, I will expect you to know a few things. If you are a complete novice, leave a comment and we’ll work on getting a “From Scratch” article published sometime in the near future.

Step 1: Creating The Database

I’ll be creating a “Blog” database. For the sake of simplicity, I’ll only add a few columns: “BlogId”, “BlogTitle”, “BlogContents”. In a real world situation, you should add things like “BlogAuthor”, “BlogFeaturedImage”, “CommentsId”, etc. After filling these columns with some gibberish content, we’re ready to build our webform.

database image

Step 2: The Listview Control

The wonderful thing about the listview control is that it allows you to maintain 100% control over your mark up. Instead of having to deal with tables, I can specify anything that I like.

  <asp:ListView runat="server" ID="lv">
        
        <LayoutTemplate>
            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
        </LayoutTemplate>
        
        <ItemTemplate>
        <asp:HyperLink runat="server" ID="link" 
                        Text='<%#Eval("BlogTitle") %>' 
                        NavigateUrl='<%#Eval("BlogId", "entry.aspx?BlogId={0}") %>' /> 
</ItemTemplate> </asp:ListView>
  • LayoutTemplate: This template serves as the wrap for each item. For instance, if each item was inside of an “li” tag, you could add a “ul” tag in your layout template as a “wrap”.
  • ItemTemplate: This will describe the layout for each item in the database. If, for example, we have 10 blog entries in the database, there will subsequently be 10 items.

Within the item template, I’ve specified that the listview control should only display a hyperlink. This hyperlink will have its text attribute equal to whatever the value is in the database for the associated row. I’m also going to set the NavigateUrl property (the href) to a new page. This entry.aspx page will serve to be the template for each entry. We’ll specify which entry should be displayed via the querystring. (More on this in the screencast.)

Step 3: LINQ

LINQ is a programming model that allows you to access many different forms of data using the same syntax. With LINQ to SQL, it allows for a strongly typed way of communicating with your relational database. Imagine being able to use the same query to access XML, Objects, Relational Databases, APIs, etc. It’s an incredible model and is easily my favorite new feature in ASP.NET 3.5.

Rather than embedding SQL code directly into your code behind files, you can now treat each column in your database tables like any other object. This is accomplished by creating a LINQ to SQL Class. This class automatically creates the database objects for you.

LINQ
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        Dim db As New BlogDBDataContext()

        Dim q = From b In db.Blogs _
                Where b.BlogContents.Contains(txtSearch.Text.Trim()) Or _
                      b.BlogTitle.Contains(txtSearch.Text.Trim()) _
                Select b

        lv.DataSource = q
        lv.DataBind()

    End Sub

When the user clicks on the “Search” button, this code will retrieve only the entries from the database that contain the value that was entered into the search textbox. Those values will be returned and stored in the variable “q”. We then set the datasource of our listview control to “q” – and databind it.

UI

Step 4: AJAXifying Our Page

In this simple demonstration, it won’t truly make a difference whether the entire page posts back or not. However in a mid to large sized site, performing an entire post back can be a pain. We’re going to wrap the contents of our listview control within an update panel in order to only refresh this specific information.

  <asp:UpdatePanel runat="server" ID="up">
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnSubmit" />
    </Triggers>
        <ContentTemplate>
        
        <asp:ListView runat="server" ID="lv">
        
        <LayoutTemplate>
            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
        </LayoutTemplate>
        
        <ItemTemplate>
        <asp:HyperLink runat="server" ID="link" 
                        Text='<%#Eval("BlogTitle") %>' 
                        NavigateUrl='<%#Eval("BlogId", "entry.aspx?BlogId={0}") %>' /> 
</ItemTemplate> </asp:ListView> </ContentTemplate> </asp:UpdatePanel>

Note the “Triggers” tag. If our button was nested inside of the update panel, the update panel would automatically refresh when the button was clicked. However, in this case, our button is outside of the update panel. In such instances, we need to add an “ASyncPostBackTrigger” that points to the button that will trigger the update panel’s post back.

Step 5: User Feedback

Loading Icon
When implementing partial page refreshes, the user can sometimes become perplexed. It may seem to him that the page is simply not responding. To compensate, we’ll add the ubiquitous “loading icon” to the page. This will supply the user with some feedback to let him know that the page is in fact processing. We can use the “Update Progress” control to accomplish this task.

<asp:UpdateProgress runat="server" ID="uProgress">
    <ProgressTemplate>
        <img src="img/ajax-loader.gif" alt="Please Wait" />
    </ProgressTemplate>
</asp:UpdateProgress>

Within the Progress Template, I’ve added an image tag that contains my loading icon. So, while the update panel is refreshing, this loading icon will display. When the post back has completed, the icon will disappear.

You’re Finished

Though this article moved a bit quickly, the screencast describes every method step by step. If you have any additional questions, please leave a comment and we’ll do our best to assist you. What I’ve supplied today is a simple way to search your site. However, in a real world situation, you’ll most likely implement a more advanced search method. I’d love to hear your thoughts on the best ways to accomplish this.

If you’d like more ASP.NET tuts, be heard! Leave a comment and voice your opinion. This framework is too powerful to ignore. Digg it, SU it, DZone it! Thanks everybody! Bye bye!

Subscribe to the Weekly Screencasts

You can add our RSS feed to your ITUNES podcasts by doing the following:

  1. Once ITUNES has loaded, clicked the “Advanced Tab”
  2. Choose “Subscribe to Podcast”
  3. Enter “http://feeds.feedburner.com/NETTUTSVideos”

That should do it! The screencast will also be searchable on ITUNES in the next twenty four hours.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Jeff

    Awesome to see this. Maybe I will start to right some tutorials for .net

  • http://atcrawford.com Drew

    I like tutorials on this subject! Keep up the good work!

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    @Jeff – I wish you would! We pay $150 per accepted tut. :)

  • http://www.ben-griffiths.com Ben Griffiths

    Informative article, nice to see some ASP too :)

  • http://submitcss.com Greg

    This seems like a great resource!
    Thanks.
    - Greg

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    Everyone – Just leave a comment if you have any questions about the tut. There are a bunch of ASP devs on this site that will help you.

  • Jason

    I think a lot of the bad blood towards ASP comes from ASP Classic, .Net is worth checking out.

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    @Jason – I agree. Classic ASP had some problems. But 3.5 is quite incredible. It’s my favorite framework.

  • J Rinc

    Great tutorial! This is very helpful for ASP .NET beginners. I would like to see more AJAX tutorials :)

  • Stacy

    I enjoyed this tutorial. Please please please make more tuts for ASP.NET!

  • Jeff

    @Jeffery – sounds good I’ll start coming up with some ideas. I never bothered before becasue I didn’t think people on this site would be interested.

  • Yosi

    I enjoyed !!!
    Post more :)

    I like you screencasts,you can add jQuery instead of this Ajax??

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    @Jeff – Looking forward to it!
    @Yosi – Yes, you can do something similar with jQuery. To pull from the database, it would probably be best to use jQuery to call a web service. Maybe a future tut? We’ll see.

  • Jeff

    @Yosi – Microsoft has actually just announced that it is going to be distributing jquery with its dev enviroment. Many thanks to Scott Gu for this. Although it appears that they are mainly going to focus on using it for it’s selectors and easy animation many people use it for ajax.

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    @Jeff @Josi – Yeah, I just wrote about that on Theme Forest.

    http://blog.themeforest.net/general/intellisense-support-for-jquery-in-visual-studio/

    I’m very excited about it. It’s nice to see Microsoft embracing open source for a change.

  • http://mokshasolutions.com Moksha

    thanks for sharing, FIRST ASP.NET tutorial and its really nice, thanks again, its a really good news for ASP.net programmer. coz your tutorial are really high quality.

    thanks again

  • ZazaBronson

    Nice tutorial, keep ‘em coming!!!!

  • http://atcrawford.com Drew

    Are there any cheaper options besides Camtasia Studio for creating screencasts? $300 is pretty steep for my newlywed budget.

    I’m definitely going to be diving into the 3.5 framework and it would be fun to try my hand at making screencasts.

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    @Drew – Yeah, there are a few alternative. But, I’ve found Camtasia to be the best. That’s why so many people use it for web dev tuts. There’s a free 30 day trial on their site if you want to try it out. Otherwise, just google “free screencast software”.

  • http://enhance.qd-creative.co.uk James

    A great screencast Jeffrey! Who needs Lynda.com!? :D

    And this comes just in time too – I’m working on a .NET project at work!

  • http://www.detacheddesigns.com Jeffrey Way
    Author

    Thanks James!

  • http://www.allapis.com Vladimir

    At work I program with ASP.NET 2.0 we are migrating to 3.5. LINQ is very cool! Thank you for the post!

  • Joel

    Hi Jeff, this was a great first tutorial and screencast on ASP.NET; not too simple yet not too advanced that most beginners can’t follow. Please continue adding more in the future.

  • http://atcrawford.com Drew

    @Jeffrey Way — cool, thanks. I’ll give the 30 day trial a shot and we’ll see where it goes from there.

  • http://laminbarrow.com Lamin Barrow

    First ASP.NET tut on this site ever. Now we are talking. Please keep em coming.

  • Jason

    just starting to learn move from classic asp to .net, so keep the tutorials going. this one was very easy to understand.

  • http://www.najamsikander.com najam sikander awan

    hi jeffray

    Very nice tutorial I myself is asp.net developer and I am big fan of jquery. I am also thinking about creating some tutorials with asp.net 2.0 or 3.5.

    And I have some requests for you I am dying to learn how to write asp.net server controls and then how to write asp.net wrapper for existing jquery plugins because i need to call plugins functions from my code behind.

  • http://www.insicdesigns.info insic

    first ASP tut in nettuts, and its really helpful.

    • http://laranzjoe.blogspot.com lawrence77

      Its not ASP, its ASP.NET! :D

  • http://www.insicdesigns.info insic

    Thank you by the way.

  • http://www.freshclickmedia.com Shane

    Thanks for posting the first ASP.NET tutorial. I’ve gotta say I’m surprised at the amount of positive feedback; this is, if I’m right, pretty much the first non open-source article posted on the site, and I had suspected that many people would have been anti-Microsoft.

    You’ve managed to touch on two areas of ASP.NET 3.5 that make building dynamic websites relatively simple. Firstly, LINQ has revolutionised the way I write data-access code. Doing it by hand would take quite a while, but LINQ enables you to have full data access within moments. Would I be right in saying that LINQ to SQL (the method for accessing data from databases) only currently supports SQL Server variants? Though I’m pretty sure support for other DBs is on the way.

    Secondly, ASP.NET AJAX is a fantastic framework. The simplest way of getting things up and running is the UpdatePanel control as you’ve shown. I use it extensively on my sites, with a lot of jQuery too for effects and DOM manipulation. I didn’t know about Microsoft’s jQuery decision – that’s really interesting stuff. I use jQuery with ASP.NET AJAX client side stuff because I find some of the ASP.NET AJAX client side libraries a little verbose.

    Just 2 more things, the first being that I use C#, and whilst I don’t want to open a VB.NET/C# debate, I guess there will be some forthcoming ASP.NET tutorials that use C# and some that use VB.NET.

    Secondly is about the naming of controls – I prefer to stick to the convention classname + descriptor for my controls. So, where you’ve used ‘lv’ for your ListView, I’d use ListViewSearchResults. This makes it completely clear in any ‘code-behind’ that the item you’re referring to is a control, and prevents the misunderstanding of abbreviations.

  • Stefan

    brilliants thanks for this more asp.net tuts would be ace!

  • http://dmipartners.com Matt

    Nice to finally see tutorials on ASP .net 3.5…
    Look forward to seeing many more.

  • Heitor

    Great Heffrey ! :)
    Thank you.

    Maybe, I think if you write your posts in C# instead of Vb, can be a little bit better, because I think its more similiar to other common languages, like Java, JavaScript, PHP, such like this. :)

    Just a sugestion.

    But anyway, Awesome! :)

    Thank you for ASP.NET tutorials. Keep track of this.

    Good Bye :)

  • http://www.solutiondesigns.net Philo

    Nice Tutorial although ASP is not really my thing :P

  • http://www.freshclickmedia.com Shane

    Wow Jeffrey – looks like the ASP.NET tutorial went down well :)

    I’ll get on with writing my suggestion :)

  • Liam

    good video, great to see somrthing done from start to finish. you shoud do plenty more maybe linking in some jquery.

  • Joel

    @Shane
    Yes, LINQ to SQL only supports SQL Server. If you want to use an ORM framework with another (non-Microsoft) database as the backend of a .NET environment, you should look into NHibernate or even Entity Framework. The latter was release by Microsoft at the same time as LINQ to SQL.

  • Jeremy

    We work with ASP.NET exclusively at work, and though I dont necessarily share the “Microsoft sucks” attitude (I’ve always been a PC guy), I’ve never been too fond of the framework.

    I’d love to see a from-scratch series.

  • http://ramacrishna.blogspot.com Ram

    Nice article. Its really useful for me..

  • http://www.windows7themes.com Windows Themes

    Thank you so much for this tutorial. It`s very useful for me :)

  • Jeff

    I have decided on a topic for some more ASP.net centered tutorials. Can’t work on it today, but I plan to right it this weekend. I no authority to say they will use it, or if they do how long it takes to post but be on the lookout in case it does.

  • http://www.instantshift.com Roshan

    Some really great info listed here. Never been in this area but I like to explore.
    Thank you and keep posting good stuff.

    Roshan
    Freelance Developer
    http://www.instantshift.com

  • Nate Coventry

    I would really like if you could do a tutorial on “Asp.NET from scratch”. I’m really excited to learn more. I love the framework.

    Thanks,
    Nate

  • http://www.silver-solutions.co.uk/ Anthony Woods

    Wow, a useful tutorial couldn’t be as more important as the now, im doing my university course at the moment and were diving right into the ASP.NET end and this tutorial has given me a positive lift to helping me through some of the basic stuff. Thank you alot, and I will be looking forward to many more tutorials like these.

  • http://www.nine9design.co.uk/ Darren

    Top class Tutorial.

    Just what I needed. My first look at Linq and it now doesn’t look do daunting.

    Thank you very much.

    Will be coming back for more.

    Oh an Ajax Shopping cart Tut would be good too.

    Thanks again.

  • Jeff

    I know I said I would write a new tutorial this weekend. I got started on it but life happened. I should be able to finish it up and get it submitted tommorrow.

  • Yosi

    Hi,
    I got a problem with viewing.
    I want to do something like this -
    When I am going to Default.aspx?id=1 then it will pull to me where the page_id = request.querystring(“id”).
    The Problem is I don’t know how to do this in C#,
    I have -
    Database name:Database.mdf
    Table name:content
    Linq to sql class : ContentLINQ.dbml

    And this is my code..

    ContentLINQDataContext db = new ContentLINQDataContext();
    var content = from p in

    when i am writing db. or p.the helper don’t give me my table name (content)..
    can someone help me?

  • Yosi

    Hi,
    sorry about double posting..
    I fixed it here is the new problem =X :

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1955: Non-invocable member ‘System.Web.HttpRequest.QueryString’ cannot be used like a method.

    And how i can do if there isn’t content then show “Hello” for example.

    Thanks for helpers

  • Jeff

    @Yosi – I believe you want to call Request.QueryString["id"] and not Request.QueryString(“id”). QueryString is a collection not a method. You just need to call the right indexer. I hope that helps.

  • Rob

    I’m a newbie on ASP.NET but when I see this stuf, I can’t wait to work with it.
    I would like to see more of this video tutorials about ASP.NET