mardi 27 septembre 2011
jQuery & ASP: creating a "more" button with AJAX (part 1)
Do you like this story?
On many web sites, we can see lists of news, updates, notifications and similiar elements with a "more" button. That button will add elements to the list when pressed. I believe you saw something similar, used especially on Social Networks like Facebook or Twitter.
In the following article we will create a "more" button using jQuery and getting the list of elements from a database, using ASP. The project has two ASP pages, one containing the list and the jQuery code (AJAX), and the second with the code needed to update the list.
Let's start building the first page.
The main page
Start creating a new document and calling it "main.asp". The page will contain all the code to create the list with the "more" button and the jQuery code that will make an AJAX request for updates. In the following example I assume we have a database (SQL Server or whatever you prefer) from which we will get the data. We will query a table containing only two fields: an ID and a name. The ID will be an important field because we are going to select records filtering them by ID. As a further development, we can use a date instead: the idea behind is the same, so it won't be difficult for you to change it to your liking.Getting the first 3 records
In our main.asp page, we start adding the ASP code to query our database:
<%
Dim first_data
Dim first_data_cmd
Set first_data_cmd = Server.CreateObject ("ADODB.Command")
first_data_cmd.ActiveConnection = your_connection_string
first_data_cmd.CommandText = "SELECT TOP(3) id, name FROM dbo.names ORDER BY id ASC" 
first_data_cmd.Prepared = true
Set first_data = first_data_cmd.Execute
%>The jQuery code
In the head of your main.asp page, add the following snippet:<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js" ></script>
<script type="text/javascript">
$(function() {
  $('.more').live('click',function() {
    var element = $(this);
    var msg = element.attr('id');
    $('#morebutton').html('Loading');
    $.ajax({
      type: 'POST',
      url: 'more.asp',
      data: 'lastmsg=' + msg,
      cache: false,
      success: function(html){
        $('#morebutton').remove();
        $('#more_updates').append(html);
      }
    });
  });
});
</script>The body section
In the body of our main.asp page, we add the following code (I will explain it piece by piece):
<div style="width:500px;"><%
dim last_id
last_id=0
%><% 
While (NOT first_data.EOF)
%><div id="<%=(first_data.Fields.Item("id").Value)%>">
  <span><%=(first_data.Fields.Item("name").Value)%></span>
</div><%
last_id=(first_data.Fields.Item("id").Value)
%><% 
first_data.MoveNext()
Wend
%><div id="more_updates"></div><div id="morebutton"><input name="more" type="button" class="more" id="<%=last_id%>" value="More">
</div></div>At the end of your main.asp page, we close the connection to our database:
<%
first_data.Close()
Set first_data = Nothing
%>In the next article we will build the more.asp page, used to collect the updates.
Enjoy yourself and let me know if you have problems.

This post was written by: Franklin Manuel
Franklin Manuel is a professional blogger, web designer and front end web developer. Follow him on Twitter
Inscription à :
Publier les commentaires (Atom)


0 Responses to “jQuery & ASP: creating a "more" button with AJAX (part 1)”
Enregistrer un commentaire