In this article we will explain and demonstrate how to insert new item and update existing item with FormBinder.
Having in mind the design of RESTful services, the item that we are about to insert needs to have its identity (or primary key) defined before we call the service. While this does not present a problem in majority of the cases, it does present a certain challenge when identity is a GUID. To overcome this, recommended approach is to set the primary key to an empty GUID and then generate a GUID on the server (WCF service). The following code represents the complete implementation of the insert/update functionality for FormBinder:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Stores.aspx.cs" Inherits="Stores" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register TagPrefix="sitefinity" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
<
head
runat
=
"server"
>
<
title
>Stores with Generic Collection client binder</
title
>
</
head
>
<
body
xmlns:sys
=
"javascript:Sys"
xmlns:dataview
=
"javascript:Sys.UI.DataView"
sys:activate
=
"*"
xmlns:code
=
"http://schemas.microsoft.com/aspnet/code"
>
<
form
id
=
"form1"
runat
=
"server"
>
<
asp:ScriptManager
ID
=
"scriptManager1"
runat
=
"server"
>
<
Scripts
>
<
asp:ScriptReference
Name
=
"MicrosoftAjax.js"
Path
=
"~/Sitefinity/Scripts/MicrosoftAjax.js"
/>
<
asp:ScriptReference
ScriptMode
=
"Inherit"
Path
=
"~/Sitefinity/Scripts/MicrosoftAjaxTemplates.js"
/>
<
asp:ScriptReference
ScriptMode
=
"Inherit"
Path
=
"~/Sitefinity/Scripts/MicrosoftAjaxAdoNet.js"
/>
</
Scripts
>
</
asp:ScriptManager
>
<
select
id
=
"storesDropDown"
runat
=
"server"
>
</
select
>
<
input
type
=
"button"
onclick
=
"OpenStore();"
value
=
"Open"
/>
<
input
type
=
"button"
onclick
=
"NewStore();"
value
=
"New store"
/>
<
sitefinity:GenericCollectionBinder
id
=
"storesBinder"
runat
=
"server"
TargetId
=
"storesDropDown"
ServiceUrl
=
"~/Sitefinity/Services/Commerce/Stores.svc"
BindOnLoad
=
"true"
DataKeyNames
=
"Id"
DataMembers
=
"Name, Id"
>
<
Containers
>
<
sitefinity:BinderContainer
ID
=
"BinderContainer1"
runat
=
"server"
RenderContainer
=
"false"
TemplateHolderTag
=
"SELECT"
>
<
option
value
=
"{{Id}}"
>{{Name}}</
option
>
</
sitefinity:BinderContainer
>
</
Containers
>
</
sitefinity:GenericCollectionBinder
>
<
div
id
=
"storeForm"
runat
=
"server"
>
</
div
>
<
sitefinity:FormBinder
ID
=
"storeFormBinder"
runat
=
"server"
TargetId
=
"storeForm"
ServiceUrl
=
"~/Sitefinity/Services/Commerce/Stores.svc"
BindOnLoad
=
"false"
OnClientSaved
=
"StoreSaved"
DataKeyNames
=
"Id"
DataMembers
=
"Id, Name, Description"
>
<
Containers
>
<
sitefinity:BinderContainer
ID
=
"BinderContainer2"
runat
=
"server"
>
<
fieldset
>
<
legend
>Store info:</
legend
>
<
ul
>
<
li
>
Store name:
<
br
/>
<
input
name
=
"Name"
type
=
"text"
sys:value
=
"{{Name}}"
/>
</
li
>
<
li
>
Store description:
<
br
/>
<
input
name
=
"Description"
type
=
"text"
sys:value
=
"{{Description}}"
/>
</
li
>
</
ul
>
<
input
type
=
"button"
sys:value
=
"Save changes"
onclick
=
"SaveChanges();"
/>
</
fieldset
>
</
sitefinity:BinderContainer
>
</
Containers
>
</
sitefinity:FormBinder
>
<
script
type
=
"text/javascript"
>
function OpenStore() {
var storeId = $('#' + '<%= storesDropDown.ClientID %>').val();
var storeFormBinder = $find('<%= storeFormBinder.ClientID %>');
storeFormBinder.get_globalDataKeys()["Id"] = storeId;
storeFormBinder.DataBind();
}
function NewStore() {
var storeFormBinder = $find('<%= storeFormBinder.ClientID %>');
storeFormBinder.get_globalDataKeys()["Id"] = storeFormBinder.GetEmptyGuid();
storeFormBinder.DataBind();
}
function SaveChanges() {
var storeFormBinder = $find('<%= storeFormBinder.ClientID %>');
storeFormBinder.SaveChanges();
}
function StoreSaved() {
var storeFormBinder = $find('<%= storeFormBinder.ClientID %>');
storeFormBinder.ClearTarget();
var storesBinder = $find('<%= storesBinder.ClientID %>');
storesBinder.DataBind();
}
</
script
>
</
body
>
</
html
>
By setting the first element's name attribute to value "Name" we have specified that the value of that input element will be used to populate the Name property of the Store data item. Similarly, we have set the name attribute of the second element to "Description", therefore instructing the binder to take the value of that element as the value of the Description property.
When we have opened the item, we have set the identity (or primary) key of the item on the binder and therefore binder has all the information it needs.