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 pageStart 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:
A few notes on the above code. First of all, remember to change your_connection_string, with an appropriate connection string to your database. Secondly, notice that with the above query we are retrieving only the first 3 records from the table (TOP(3)). We get the name and the id from the table called names. We order the records by id (ascending).
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 codeIn the head of your main.asp page, add the following snippet:
With it, we include the jQuery library and we create a function which will be executed when the "more" button is clicked. We are going to use the ID of the button as parameter for the AJAX request. I don't know if you are familiar with jQuery and AJAX, but basically with the above code we are sending an AJAX request to another document (more.asp) sending lastmsg as parameter to be used in a query. With a successful response to the AJAX request, we are going to append the collected data to a div, while removing the "more" button (don't worry, after removing it, we are going to add it again in case we are not at the end of the recordset).
var element = $(this);
var msg = element.attr('id');
data: 'lastmsg=' + msg,
The body section
In the body of our main.asp page, we add the following code (I will explain it piece by piece):
This is the outer container. You can style it, if you like.
We create a variable that will contain the last retrieved ID (last_id).
We start the repeat region.
While (NOT first_data.EOF)
We insert the data already collected from the database. The div will have an ID with a value equal to the ID collected from the database.
We update the last_id variable, with the last retrieved ID.
We close the repeat region.
We create a div that will contain the updates.
We create the "more" button - nearly there!
<div id="morebutton"><input name="more" type="button" class="more" id="<%=last_id%>" value="More">
We close the outer container.
At the end of your main.asp page, we close the connection to our database:
And that's it.
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.