Jun 20

Add
First Name
Last Name
Command

Step 1: Register your Web service

First you have to register your Web service to use it in your page:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="./ContactService.asmx" />
    </Services>
</asp:ScriptManager>

Step 2: Create header and the row to insert

Secondly, you have to create the header and insert row. Note that the link buttons save and cancel, call client methods and return false to interrupt the post back.

<%--Header--%>
<div style="width: 500px" class="HeaderStyle">
    <div style="width: 200px; float: left;">
        First Name
    </div>
    <div style="width: 200px; float: left;">
        Last Name
    </div>
    <div style="width: 100px; float: left;">
        Command
    </div>
</div>
<%--Row to insert--%>
<div runat="server" id="divInsert" style="display: none">
    <div style="width: 200px; float: left;">
        <asp:TextBox runat="server" ID="txtFirstName" Width="180"></asp:TextBox>
    </div>
    <div style="width: 200px; float: left;">
        <asp:TextBox runat="server" ID="txtLastName" Width="180"></asp:TextBox>
    </div>
    <div style="width: 100px; float: left;">
        <asp:LinkButton runat="server" ID="lnkSave" OnClientClick="Save();return false;"
            Text="Save"></asp:LinkButton>
        <asp:LinkButton runat="server" ID="lnkCancel" OnClientClick="Cancel();return false;"
            Text="Cancel"></asp:LinkButton>
    </div>
</div>

Step 3: Create the grid view

To accomplish this task I use an external component named AjaxDataControls

The Ajax Data Controls is a DotNetSlackers project. Developed on top of Asp.net Ajax Extension, the main goal of this project is to provide rich set of data controls for Client Centric Development Model. Since the data controls of Asp.net like GridView, DataList, Repeater etc does not have any Client Side Object Model thus it is not possible to work with these controls with Web Service / Page Methods call.

  1. Add the reference to AjaxDataControls.dll
  2. Register the control:

If you want to do that in the page:

<%@ Register Assembly="AjaxDataControls" Namespace="AjaxDataControls" TagPrefix="AjaxData" %> 

You can also do that in the Web.Config:

<add tagPrefix="AjaxData" namespace="AjaxDataControls" assembly="AjaxDataControls"/> 

    Add the following code to create the gridview:

    <div style="clear: both; width: 500px;">
        <ajaxdata:gridview id="grvContacts" runat="server" cssclass="DataWebControlStyle" cellspacing="0" cellpadding="3"> 
        <AlternatingRowStyle CssClass="AlternatingRowStyle" /> 
        <RowStyle CssClass="RowStyle" /> 
        <HeaderStyle CssClass="HeaderStyle" /> 
        <EmptyDataTemplate> 
            There is no records available. 
        </EmptyDataTemplate> 
        <EmptyDataRowStyle HorizontalAlign="Center" /> 
        <Columns> 
            <AjaxData:GridViewBoundColumn DataField="FirstName" ItemStyle-Width="200" /> 
            <AjaxData:GridViewBoundColumn DataField="LastName" HeaderStyle-Width="200" /> 
            <AjaxData:GridViewBoundColumn HeaderStyle-Width="100" /> 
        </Columns> 
        </ajaxdata:gridview>
    </div>

    Step 4: Add the javascript event

    On page load, you bind the grid. When you click the save button you call the web service insert method and onSaveSuccess you bind the grid again.

    <script type="text/javascript">
    
        function pageLoad(sender, e) {
            TestGridView.ContactService.GetContacts(onLoadSuccess);
        }
    
        function onLoadSuccess(result) {
            var gridView = $find('<%= grvContacts.ClientID %>');
            gridView.set_dataSource(result);
            gridView.dataBind();
        }
    
        function Add() {
            $get("<%= divInsert.ClientID %>").style.display = 'block';
        }
    
        function Save() {
            TestGridView.ContactService.InsertContact(
                $get("<%= txtFirstName.ClientID %>").value,
                $get("<%= txtLastName.ClientID %>").value,onSaveSuccess);
        }
    
        function Cancel() {
            $get("<%= txtFirstName.ClientID %>").value = '';
            $get("<%= txtLastName.ClientID %>").value = '';
            $get("<%= divInsert.ClientID %>").style.display = 'none';
        }
    
        function onSaveSuccess(result) {
            $get("<%= txtFirstName.ClientID %>").value = '';
            $get("<%= txtLastName.ClientID %>").value = '';
            TestGridView.ContactService.GetContacts(onLoadSuccess);
            $get("<%= divInsert.ClientID %>").style.display = 'none';
        } 
    
    </script>

    Web Service for this example

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class ContactService : System.Web.Services.WebService
    {
        [WebMethod]
        public List<Contact> GetContacts()
        {
            ContactBAL contactBAL = new ContactBAL();
            return contactBAL.GetContacts();
        }
    
        [WebMethod]
        public void InsertContact(string firstName, string lastName)
        {
            ContactBAL contactBAL = new ContactBAL();
            Contact contact = new Contact();
            contact.FirstName = firstName;
            contact.LastName = lastName;
            contactBAL.InsertContact(contact);
        }
    }

    Don't forget this line to enable JavaScript call:

    [System.Web.Script.Services.ScriptService]

    Download the full code

    Comments

    rootnode

    Posted on Thursday, 6 August 2009 04:22

    I've got a question. What is 'TestGridView' that you refer to in the javascript?

    Helori

    Posted on Thursday, 6 August 2009 10:35

    TestGridView is my webservice namespace.
    Regards,
    Helori.

    lalit

    Posted on Saturday, 8 August 2009 08:39

    Hi i am trying to develope this application but not working. Aslo cannot Downloading the complete code from given link. So please send me this full code if some one downloaded before on my e mail id . My id is "lalitcdhake@gmail.com"...i need it very much...help me please

    clyde

    Posted on Tuesday, 11 August 2009 10:57

    can you re-link or repost the code.  I would like to review this and get the sample working.

    Helori

    Posted on Wednesday, 12 August 2009 15:32

    The link is ok now.

    Thanks,
    Helori.

    Deals

    Posted on Thursday, 21 January 2010 04:21

    nice post.
    Thanks frenz.
    dont stop posting!

    Walker Langarica

    Posted on Wednesday, 9 June 2010 03:19

    Good article.

    Another thing, loving your blog design. I had a similar blog to yours before I sold it at the end of last year.

    Debora Aramboles

    Posted on Tuesday, 13 July 2010 00:23

    I've subscribed to your news feed after reading this piece! Could you write more regarding this subject in future articles?

    Houses

    Posted on Tuesday, 13 July 2010 07:22

    This is good! How did you learn this stuff when you were first getting into it?

    Ricki Paradis

    Posted on Tuesday, 13 July 2010 18:10

    This is nice! How did you learn this stuff when you were getting started?

    Elden Schlauch

    Posted on Wednesday, 14 July 2010 06:10

    I watched a program regarding this on TV yesterday. Thanks for putting more meat on the bones

    Jayme Duderstadt

    Posted on Sunday, 18 July 2010 06:54

    Hello,

    I wanted to say that I have been watching for a a couple of months on and off and I would like to sign up for the daily feed. I am not to computer smart so I'll give it a try but I might need some help. This is a terrific find and I would hate to lose contact, and maybe never discover it again.

    Anyway, thanks again and I look forward to reading/posting again in the future!

    Best laptops under 500

    Posted on Sunday, 18 July 2010 22:22

    Just to let you know... your blog looks extremely bizarre in Mozilla on a mac

    Best laptops under 500

    Posted on Sunday, 18 July 2010 22:22

    I was looking for articles about this on Bing and stumbled on your article. I found it to be well explained. Thanks

    Add comment




      Country flag

    biuquote
    Loading