This is a migrated thread and some comments may be shown as answers.

Form Template for Manual Insert and Update

1 Answer 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arun Kumar
Top achievements
Rank 2
Arun Kumar asked on 17 Aug 2013, 04:27 PM
Hi all,

I am trying to achieve manual insert and update using form template. But it does not work. I am using linq-to-sql datasource. Datasource is set in NeedDataSource Event. Both InsertCommand and UpdateCommand events are not firing.

Code:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="dgv">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="dgv" />
                <telerik:AjaxUpdatedControl ControlID="lblStatus" />
                <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
 
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Silk" />
 
<div class="dbStatus">
    <asp:Label runat="server" ID="lblStatus" EnableViewState="false" Font-Bold="true" />
</div>
 
<telerik:RadGrid ID="dgv" runat="server" AutoGenerateColumns="False" PageSize="10" AllowPaging="True" AllowSorting="True"
    OnNeedDataSource="dgv_NeedDataSource" OnUpdateCommand="dgv_UpdateCommand" OnInsertCommand="dgv_InsertCommand"
    OnDeleteCommand="dgv_DeleteCommand" OnItemInserted="dgv_ItemInserted">
 
    <MasterTableView TableLayout="Auto" DataKeyNames="Record_Id" ShowHeadersWhenNoRecords="true" EditMode="PopUp">
 
        <NoRecordsTemplate>
            <div>
                There are no records
            </div>
        </NoRecordsTemplate>
 
        <Columns>
 
            <telerik:GridBoundColumn UniqueName="colRecordId" DataField="Record_Id" DataType="System.Int64" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,CountryId%>">
                <%--<HeaderStyle Width="120" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colContinent" DataField="Continent" DataType="System.String" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,Continent%>">
                <%--<HeaderStyle Width="160" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colContinentBI" DataField="ContinentBI" DataType="System.String" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,ContinentBI%>">
                <%--<HeaderStyle Width="160" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colDescription" DataField="Description" DataType="System.String" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,Description%>">
                <%--<HeaderStyle Width="200" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colDescriptionBI" DataField="DescriptionBI" DataType="System.String" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,DescriptionBI%>">
                <%--<HeaderStyle Width="200" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colCreatedBy" DataField="CreatedBy" DataType="System.String" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,CreatedBy%>">
                <%--<HeaderStyle Width="120" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colCreatedON" DataField="CreatedOn" DataType="System.DateTime" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,CreatedDate%>">
                <%--<HeaderStyle Width="160" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colModifiedBy" DataField="ModifiedBy" DataType="System.String" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,ModifiedBy%>">
                <%--<HeaderStyle Width="120" />--%>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn UniqueName="colModifiedOn" DataField="ModifiedOn" DataType="System.DateTime" HeaderText="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,ModifiedDate%>">
                <%--<HeaderStyle Width="160" />--%>
            </telerik:GridBoundColumn>
 
        </Columns>
 
        <EditFormSettings EditFormType="Template" PopUpSettings-Modal="true">
            <FormTemplate>
 
                <table>
                    <tr>
                        <td colspan="3">
                            <br />
                            <asp:Label runat="server" ID="lblStatus" Font-Bold="true" />
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td class="tdLabel">
                            <asp:Label runat="server" ID="lblCountryId" Text="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,CountryId%>"
                                AssociatedControlID="txtCountryId" />
                        </td>
                        <td class="tdControl">
                            <telerik:RadTextBox ID="txtCountryId" runat="server" Width="300" ReadOnly="true" Text='<%# Bind("Record_Id") %>' />
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td class="tdLabel">
                            <asp:Label runat="server" ID="lblContinent" Text="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,Continent%>"
                                AssociatedControlID="txtContinent" />
                        </td>
                        <td class="tdControl">
                            <telerik:RadTextBox ID="txtContinent" runat="server" Width="300" MaxLength="255" Text='<%# Bind("Continent") %>' />
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td class="tdLabel">
                            <asp:Label runat="server" ID="lblContinentBI" Text="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,ContinentBI%>"
                                AssociatedControlID="txtContinentBI" />
                        </td>
                        <td class="tdControl">
                            <telerik:RadTextBox ID="txtContinentBI" runat="server" Width="300" MaxLength="255" Text='<%# Bind("ContinentBI") %>' />
                        </td>
                        <td></td>
                    </tr>
 
                    <tr>
                        <td class="tdLabel">
                            <asp:Label runat="server" ID="lblDescription" Text="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,Description%>"
                                AssociatedControlID="txtDescription" />
                        </td>
                        <td class="tdControl">
                            <telerik:RadTextBox ID="txtDescription" runat="server" Width="300" MaxLength="255" ValidationGroup="CountryValidation" Wrap="false"
                                EmptyMessage="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,Description%>"
                                Text='<%# Bind("Description") %>' />
                        </td>
                        <td>
                            <asp:RequiredFieldValidator ID="rfvDescription" runat="server" ValidationGroup="CountryValidation" ControlToValidate="txtDescription"
                                ErrorMessage="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,DescriptionRequiredValidationMessage%>" Text="*" />
                        </td>
                    </tr>
                    <tr>
                        <td class="tdLabel">
                            <asp:Label runat="server" ID="Label1" Text="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,DescriptionBI%>"
                                AssociatedControlID="txtDescriptionBI" />
                        </td>
                        <td class="tdControl">
                            <telerik:RadTextBox ID="txtDescriptionBI" runat="server" Width="300" MaxLength="255" ValidationGroup="CountryValidation" Wrap="False"
                                EmptyMessage="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,DescriptionBI%>"
                                Text='<%# Bind("DescriptionBI") %>' />
                        </td>
                        <td>
                            <asp:RequiredFieldValidator ID="rfvDescriptionBI" runat="server" ValidationGroup="CountryValidation" ControlToValidate="txtDescriptionBI"
                                ErrorMessage="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,DescriptionBIRequiredValidationMessage%>" Text="*" />
                        </td>
                    </tr>
                </table>
 
                <br />
                <br />
 
                <table>
                    <tr>
                        <td class="tdButton">
                            <telerik:RadButton ID="btnSubmit" runat="server" Width="100%" ValidationGroup="CountryValidation"
                                Text="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,Submit%>"
                                CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />
                        </td>
                        <td class="tdButton">
                            <telerik:RadButton ID="btnCancel" runat="server" Width="100%"
                                Text="<%$Resources:AM.BLIS.SP.Common.Resources.BLISResource,Cancel%>"
                                CommandName="Delete" />
                        </td>
                    </tr>
                </table>
 
 
            </FormTemplate>
        </EditFormSettings>
 
    </MasterTableView>
</telerik:RadGrid>
 
<telerik:RadWindowManager ID="RadWindowManager1" runat="server" />

CountryMasterBPO cm = new CountryMasterBPO();
 
protected void dgv_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    this.dgv.DataSource = cm.GetAllCountries();
}
 
 
protected void dgv_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
 
    RadAjaxManager1.ResponseScripts.Add(string.Format("Insert Command"));
 
    var editableItem = ((GridEditableItem)e.Item);
    var newCountry = new tbCountry_Master();
 
    Hashtable values = new Hashtable();
    e.Item.OwnerTableView.ExtractValuesFromItem(values, editableItem);
 
    newCountry.Continent = (string)values["Continent"];
    newCountry.ContinentBI = (string)values["ContinentBI"];
    newCountry.Description = (string)values["Description"];
    newCountry.DescriptionBI = (string)values["DescriptionBI"];
 
    newCountry.Exp1 = "0";
    newCountry.Exp2 = "0";
    newCountry.CreatedBy = SPContext.Current.Web.CurrentUser.LoginName;
    newCountry.CreatedOn = DateTime.Now;
    newCountry.ModifiedBy = SPContext.Current.Web.CurrentUser.LoginName;
    newCountry.ModifiedOn = DateTime.Now;
 
    var result = cm.SaveCountry(newCountry);
 
    DisplayMessage(!result, result ? "Sucessfully Saved" : "Error!!! Try Again");
 
}
 
 
protected void dgv_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
 
    RadAjaxManager1.ResponseScripts.Add(string.Format("Update Command"));
 
    var editableItem = ((GridEditableItem)e.Item);
    var recordId = (long)editableItem.GetDataKeyValue("Record_Id");
 
    RadAjaxManager1.ResponseScripts.Add(string.Format("Record Id: {0}", recordId));
 
    var country = cm.GetCountry(recordId);
    if (country != null)
    {
 
        RadAjaxManager1.ResponseScripts.Add(string.Format("Description: {0}", country.Description));
 
        editableItem.UpdateValues(country);
 
        country.ModifiedBy = SPContext.Current.Web.CurrentUser.LoginName;
        country.ModifiedOn = DateTime.Now;
 
        var result = cm.SaveCountry(country);
 
        DisplayMessage(!result, result ? "Sucessfully Updated" : "Error!!! Try Again");
 
    }
 
}
 
 
protected void dgv_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
 
    RadAjaxManager1.ResponseScripts.Add(string.Format("Delete Command"));
 
    var editableItem = ((GridEditableItem)e.Item);
    var recordId = (long)editableItem.GetDataKeyValue("Record_Id");
 
    bool isReferenced = false;
    var result = cm.DeleteCountry(recordId, out  isReferenced);
 
    DisplayMessage(!result, result ? "Sucessfully Deleted" : "Error!!! Try Again");
 
}
 
 
protected void dgv_ItemInserted(object sender, GridInsertedEventArgs e)
{
    if (e.Exception != null)
    {
        e.ExceptionHandled = true;
        e.KeepInInsertMode = true;
 
        DisplayMessage(true, e.Exception.Message.ToString());
 
    }
}
 
 
private void DisplayMessage(bool isError, string text)
{
    this.lblStatus.ForeColor = (isError) ? Color.Red : Color.Green;
    this.lblStatus.Text = text;
}

PS: This is sharepoint 2010 visual web part.

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 22 Aug 2013, 03:58 AM
Hi Arun,

Most probably the problem you are observing is caused by the way you are Ajax-ifying the Grid. Since this code is used in a web part there are some rules you need to follow. More information could be found in this help topic. Although the help topic refers to MOSS 2007 the approach could be used in SP 2010 as well.

You could test the standalone project I made and which is working as expected on my side.

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Arun Kumar
Top achievements
Rank 2
Answers by
Konstantin Dikov
Telerik team
Share this question
or