Create Client Controls in ASP.NET 2.0 AJAX 1.0 Extensions
page 5 of 9
by Bilal Haidar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 40151/ 77

Server side AJAX service

Displaying, adding, deleting, and editing contact records require an AJAX service to be able to interact with the database. The database is very simple and contains a single table named Contacts.

The contacts table contains, ContactID, FirstName, LastName, and Email columns.

The AJAX service is defined as follows:

Listing 17

namespace Bhaidar.Services
{
  [ScriptService]
  public class ContactsService : System.Web.Services.WebService
  {

The major thing to notice is the addition of the attribute ScriptService. This attribute is the one that allows the server to be a script-callable service.

The Web service contains the following web methods.

Listing 18

public System.Collections.Generic.List<Contact> GetContactList
(int startIndex, int maximumRows)
{
  return ContactsManager.Instance.GetContactList(startIndex, maximumRows);
}

The above web method simply returns a generic list of contacts from the database. It has a start index and maximum rows input parameters that allow you, if you want, to implement paging inside the grid client control.

The web method calls the ContactsManager class that contains all the details of connecting to the database. The code is very simple, you can follow it by downloading the accompanying code of this article.

Listing 19

[WebMethod]
public int AddNewContact(string firstName, string lastName, string email)
{
  return ContactsManager.Instance.AddNewContact(firstName, lastName, email);
}

The above Web method is used to add a new contact to the database. It takes as input the first name, last name and email input parameters and internally calls the ContactsManager’s AddNewContact method to add the new contact to the database.

Listing 20

[WebMethod]
public bool UpdateContact(int contactID, string email)
{
  return ContactsManager.Instance.UpdateContact(contactID, email);
}

The above Web method is used to update an existing record in the database. The method takes as input the contact ID and the email address, which means, only the email address is allowed to be updated. Internally, the method calls the ContactsManager’s UpdateContact method to do the actual update on the database.

The last Web method is the DeleteContact.

Listing 21

[WebMethod]
public bool DeleteContact(int contactID)
{
  return ContactsManager.Instance.DeleteContact(contactID);
}

It takes as input the contact ID to be deleted and calls internally the ContactsManager’s DeleteContact method.


View Entire Article

User Comments

Title: gsdfg   
Name: nikesh
Date: 2009-12-24 6:21:47 AM
Comment:
not bad
Title: Mr   
Name: Malcolm Swaine
Date: 2008-07-09 6:24:45 PM
Comment:
"A control is already associated with the element." - try removing the Ajax Functionality and this goes away ... quick workaround
Title: edit data error   
Name: jfliuyun
Date: 2007-06-21 4:26:44 PM
Comment:
when first click editimage everything is ok,but before you submit the data ,you click editimage throw error "A control is already associated with the element." can you tell me what happened,and how to result it,thanks
Title: Yes it does!   
Name: Muhammad Mosa
Date: 2007-06-21 8:10:15 AM
Comment:
Yes it does! at least you closed the door. I was thinking of another way and want to make sure there is no other way
Title: Re: Muhammad   
Name: Bilal Haidar [MVP]
Date: 2007-06-20 6:54:11 AM
Comment:
In AJAX 1.0 you only have two ways to interact with methods on the server:
1- WS methods
2- Page static methods

In the second option, you can place a static method on the page, inside it you can directly access your API the way you want. Does this help you?

Regards
Title: Bind The Control with a data layer   
Name: Muhammad Mosa
Date: 2007-06-20 3:56:54 AM
Comment:
No, this is not my target. sorry for this. what I mean I have Business Logic Rule Library. I want to use this library's methods to bind data to the AJAX control. Just as in your sample, but instead of using WS I want to use normal library
Title: Re: Bind The Control with a data layer   
Name: Bilal Haidar [MVP]
Date: 2007-06-19 3:47:13 PM
Comment:
I guess I didn't get your question right!

Do you mean you want to use WS not developed in .NET? Please correct me if I am wrong!

Thanks
Title: Bind The control with a data layer   
Name: Muhammad Mosa
Date: 2007-06-19 1:26:28 PM
Comment:
Well, we are on the same boat then. Ok in this sample, do you have any idea how can I bind the control to a data layer method that is not a WS method?!
Keep the good work brother
Title: Re: Carlito   
Name: Bilal Haidar [MVP]
Date: 2007-06-19 1:09:59 PM
Comment:
Carlito,
I will be checking on that and get back to you here on this article!
Regards
Title: Re: Muhammad Mosa   
Name: Bilal Haidar [MVP]
Date: 2007-06-19 1:07:59 PM
Comment:
Hello Muhammad,
We never use WS as a data layer. I can speak on myself, it is only for demonstration purpose that I embed the nasty DB work inside my WS. Just to save some time!
But in real projects, WS should only be an interface for your application and all your logic (BL and DAL) should be in a seperate API!!

Hope this answers your question!

Regards
Title: Mozilla compatibility   
Name: Carlito
Date: 2007-06-19 12:30:41 PM
Comment:
Hi, I was just trying your sample code which seems to work well with IE but not so well with Firefox.
It seems whenever I click on 'New Contact', the new contact sections shows for a second then disappears and the page reloads the contact list again...
Title: Nice By why always Web Services?!   
Name: Muhammad Mosa
Date: 2007-06-19 9:41:23 AM
Comment:
Wonderful as usual Bilal. I have one comment but it is not on the article it is general. Every time I read something about ASP.NET AJAX -well not everything- they just present Web Services as the data service to retrive data! While in most cases we don't use web services unless it is required. I mean I'm not going to use Web Service just to act as a data layer, I already have my data layer library!
In this I'm not going to use Web Service! you may suggest to use static web methods on the page itself, well is there are any other alternatives?!






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 2:30:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search