Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Mar 9

Written by: Steve Gray
3/9/2011 6:25 PM  RssIcon

Following is an example of the ComponentArt grid in Callback Mode.

Now, if you use the CA grid at all you know that they install examples right on your machine for you to learn from.

The reason that I’m coding these examples is:

  • Their examples are sometimes too verbose
  • Their examples aren’t coded the way that I have the grids coded – I use a class that abstracts a lot of the grid setup so that I don’t have to look at it, and so that it is consistent across a project.

These examples are coded in a master page, I’m providing only the needed details here.

So, here is a complete working example of grid editing in client mode.

An on-line example is provided here: http://componentart.devshed.us/Examples/Grid/EditingCallbackMode.aspx

There is a reference to the ComponentArtGrid class in the code behind, that’s here: http://devshed.us/Blogs/tabid/227/EntryId/1004/ComponentArt-Grid-Class.aspx

And the style sheet is here (gridStyle.css) http://devshed.us/Blogs/tabid/227/EntryId/1005/ComponentArt-Grid-Css.aspx

The aspx page:

Code Snippet
  1. <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="EditingCallbackMode.aspx.vb" Inherits="ComponentArt.EditingCallbackMode" %>
  2.  
  3. <%@ Register Assembly="ComponentArt.Web.UI" Namespace="ComponentArt.Web.UI" TagPrefix="ComponentArt" %>
  4. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
  5.   <script type="text/javascript">
  6.  
  7.       function Grid1_onItemBeforeInsert(sender, eventArgs) {
  8.           if (document.getElementById('ctl00$MainContent$chkConfirmInsert').checked)
  9.               if (!confirm("Insert record?"))
  10.                   eventArgs.set_cancel(true);
  11.       }
  12.  
  13.       function Grid1_onItemBeforeUpdate(sender, eventArgs) {
  14.           if (document.getElementById('MainContent_chkConfirmUpdate').checked)
  15.               if (!confirm("Update record?"))
  16.                   eventArgs.set_cancel(true);
  17.       }
  18.  
  19.       function Grid1_onItemBeforeDelete(sender, eventArgs) {
  20.           if (document.getElementById('ctl00$MainContent$chkConfirmDelete').checked)
  21.               if (!confirm("Delete record?"))
  22.                   eventArgs.set_cancel(true);
  23.       }
  24.  
  25.       function Grid1_onCallbackError(sender, eventArgs) {
  26.           if (confirm('Invalid data has been entered. View details?')) alert(eventArgs.get_errorMessage());
  27.           Grid1.page(0);
  28.       }
  29.  
  30.       function editGrid(rowId) {
  31.           Grid1.edit(Grid1.getItemFromClientId(rowId));
  32.       }
  33.  
  34.       function editRow() {
  35.           Grid1.editComplete();
  36.       }
  37.  
  38.       function insertRow() {
  39.           Grid1.editComplete();
  40.       }
  41.  
  42.       function deleteRow(rowId) {
  43.           Grid1.deleteItem(Grid1.getItemFromClientId(rowId));
  44.       }
  45.  
  46.  
  47.  
  48.   </script>
  49.  
  50. </asp:Content>
  51. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  52.     <div >
  53.         <span>This grid is in Callback Mode. Edits are updated immediately</span>
  54.         <br />
  55.         <br />
  56.         <ComponentArt:DataGrid ID="Grid1"
  57.             runat="server">
  58.             <ClientEvents>
  59.                 <CallbackError EventHandler="Grid1_onCallbackError" />
  60.                 <ItemBeforeInsert EventHandler="Grid1_onItemBeforeInsert" />
  61.                 <ItemBeforeUpdate EventHandler="Grid1_onItemBeforeUpdate" />
  62.                 <ItemBeforeDelete EventHandler="Grid1_onItemBeforeDelete" />
  63.             </ClientEvents>
  64.             <Levels>
  65.                 <ComponentArt:GridLevel
  66.                     DataKeyField="lRowID"
  67.                     EditCommandClientTemplateId="EditCommandTemplate"
  68.                     InsertCommandClientTemplateId="InsertCommandTemplate"
  69.                     >
  70.                     <Columns>
  71.                         <ComponentArt:GridColumn AllowEditing="false" DataField="lRowID" Visible="false" />
  72.                         <ComponentArt:GridColumn DataField="Itemnmbr" HeadingText="Item"  AllowEditing="false" />
  73.                         <ComponentArt:GridColumn DataField="Quantity" HeadingText="Quantity" />
  74.                         <ComponentArt:GridColumn DataField="UnitCost" HeadingText="Unit Cost" AllowEditing="false" />
  75.                         <ComponentArt:GridColumn AllowSorting="false" HeadingText="Edit Command" DataCellClientTemplateId="EditTemplate" EditControlType="EditCommand" Width="100" Align="Center" />
  76.                     </Columns>
  77.                 </ComponentArt:GridLevel>
  78.             </Levels>
  79.             <ClientTemplates>
  80.                 <ComponentArt:ClientTemplate ID="EditTemplate"><a href="javascript:Grid1.edit(Grid1.getItemFromClientId('## DataItem.ClientId ##'));">
  81.                     Edit</a> | <a href="javascript:Grid1.deleteItem(Grid1.getItemFromClientId('## DataItem.ClientId ##'))">
  82.                         Delete</a> </ComponentArt:ClientTemplate>
  83.                 <ComponentArt:ClientTemplate ID="EditCommandTemplate"><a href="javascript:Grid1.editComplete();">
  84.                     Update</a> | <a href="javascript:Grid1.editCancel();">Cancel</a> </ComponentArt:ClientTemplate>
  85.                 <ComponentArt:ClientTemplate ID="InsertCommandTemplate"><a href="javascript:Grid1.editComplete();">
  86.                     Insert</a> | <a href="javascript:Grid1.editCancel();">Cancel</a> </ComponentArt:ClientTemplate>
  87.             </ClientTemplates>
  88.         </ComponentArt:DataGrid>
  89.  
  90.         <br/>
  91.  
  92.       <table width="730" cellpadding="0" cellspacing="0" border="0">
  93.           <tr>
  94.               <td width="270" valign="top">
  95.                   <span class="hint">Insert, update, and delete any number of records. All changes are
  96.                       applied immediately through callbacks. </span>
  97.                   <br>
  98.                   <br>
  99.                   <asp:CheckBox ID="chkConfirmInsert" Text="Confirm Before Insert" Checked="false"
  100.                       runat="server" />
  101.                   <br>
  102.                   <asp:CheckBox ID="chkConfirmUpdate" Text="Confirm Before Update" Checked="false"
  103.                       runat="server" />
  104.                   <br>
  105.                   <asp:CheckBox ID="chkConfirmDelete" Text="Confirm Before Delete" Checked="false"
  106.                       runat="server" />
  107.                   <br>
  108.                   <br>
  109.               </td>
  110.               <td valign="top">
  111.                   <asp:Label ID="lblFeedback" runat="server" />
  112.                   <td>
  113.                       <td align="right" valign="top">
  114.                           <input type="button" onclick="Grid1.Table.AddRow()" value="Add row" />
  115.                       </td>
  116.           </tr>
  117.       </table>
  118.  
  119.     </div>
  120.  
  121. </asp:Content>

 

The code behind

Code Snippet
  1. Public Class EditingCallbackMode
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  5.         buildGrid()
  6.         Grid1.DataBind()
  7.  
  8.         If Not Page.IsPostBack Then
  9.         Else
  10.  
  11.             lblFeedback.Text = "<b>Actions Performed:</b><br/>"
  12.         End If
  13.     End Sub
  14.  
  15.     Private Sub buildGrid()
  16.         Dim grd As New ComponentArtGrid(Web.UI.GridRunningMode.Callback)
  17.         Grid1 = grd.setup(Me.Grid1)
  18.         Grid1.Width = 500
  19.  
  20.  
  21.         Grid1.DataSource = getDataSource()
  22.     End Sub
  23.  
  24.     Function getDataSource() As DataTable
  25.         Dim oDT As New DataTable
  26.  
  27.         'create columns
  28.         oDT.Columns.Add(New DataColumn("lRowID", System.Type.GetType("System.Int32")))
  29.         oDT.Columns.Add(New DataColumn("Itemnmbr", System.Type.GetType("System.String")))
  30.         oDT.Columns.Add(New DataColumn("Quantity", System.Type.GetType("System.Double")))
  31.         oDT.Columns.Add(New DataColumn("UnitCost", System.Type.GetType("System.Double")))
  32.  
  33.         'create the rows
  34.         oDT.Rows.Add(1, "Big Hammer", 200, 5.99)
  35.         oDT.Rows.Add(2, "Box of Nails", 300, 15.95)
  36.         oDT.Rows.Add(3, "Bandages", 1, 9.99)
  37.  
  38.         Return oDT
  39.     End Function
  40.  
  41.     Private Sub UpdateDb(ByVal item As ComponentArt.Web.UI.GridItem, ByVal command As String)
  42.         Dim sql As String = ""
  43.  
  44.         Try
  45.             'get the grid columns like this:
  46.             sql += item("itemnmbr").ToString
  47.  
  48.             Select Case (command)
  49.                 Case "INSERT"
  50.                     'insert code here
  51.                 Case "UPDATE"
  52.                     'update code here
  53.                 Case "DELETE"
  54.                     'delete code here
  55.             End Select
  56.  
  57.         Catch ex As Exception
  58.             lblFeedback.Text = "SQL Error: " + ex.Message
  59.         End Try
  60.  
  61.     End Sub
  62.  
  63.  
  64.     Private Sub Grid1_InsertCommand(ByVal sender As Object, ByVal e As ComponentArt.Web.UI.GridItemEventArgs) Handles Grid1.InsertCommand
  65.         lblFeedback.Text += "- Inserted " + e.Item("Itemnmbr").ToString() + "<br/>"
  66.         UpdateDb(e.Item, "INSERT")
  67.     End Sub
  68.  
  69.     Private Sub Grid1_UpdateCommand(ByVal sender As Object, ByVal e As ComponentArt.Web.UI.GridItemEventArgs) Handles Grid1.UpdateCommand
  70.         lblFeedback.Text += "- Updated " + e.Item("Itemnmbr").ToString() + "<br/>"
  71.         UpdateDb(e.Item, "UPDATE")
  72.     End Sub
  73.     Private Sub Grid1_DeleteCommand(ByVal sender As Object, ByVal e As ComponentArt.Web.UI.GridItemEventArgs) Handles Grid1.DeleteCommand
  74.         lblFeedback.Text += "- Deleted " + e.Item("Itemnmbr").ToString() + "<br/>"
  75.         UpdateDb(e.Item, "DELETE")
  76.     End Sub
  77.  
  78.     Public Sub OnNeedRebind(ByVal sender As Object, ByVal oArgs As EventArgs) Handles Grid1.NeedRebind
  79.         Grid1.DataBind()
  80.     End Sub
  81.  
  82.  
  83.     Public Sub OnNeedDataSource(ByVal sender As Object, ByVal oArgs As EventArgs) Handles Grid1.NeedDataSource
  84.         buildGrid()
  85.     End Sub
  86.  
  87.  
  88. End Class

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us