Custom module RadGrid not inserting

Posted by Community Admin on 04-Aug-2018 18:59

Custom module RadGrid not inserting

All Replies

Posted by Community Admin on 03-Jun-2011 00:00

Hello,

I have been following Josh Morales' blog post on creating 'intra-site modules' and have created on based on the Hello World module that he created.  I added a RadGrid to the backend pages, used LinqToSql to retrieve/insert/update data.  I also have an embedded form in the <EditFormSettings> section of the RadGrid that includes 2 RadEditors, several textboxes, a checkbox, a drop down list (manually populated) and a RadNumericTextBox.

The RadGrid shows the database information with no issues, but when I try to insert/update a record I get no error messages, but the record isn't inserted/updated.  Also I have several methods (RadGrid1_ItemInserted, RadGrid1_ItemUpdated, ...) that never get hit.  I have 'AllowAutomaticInserts', 'AllowAutomaticDeletes', and AllowAutomaticUpdates' set to true in the RadGrid.  The MasterTableView has the LinqToSql datasource as well as the RadGrid.  Some help would be greatly appreciated as to what could be wrong.

=============================================================================
EDIT: I can delete the record from the database by clicking on the 'DELETE' button.  Strange that I can delete but not insert.
=============================================================================

Here is the AdminIndex code (the backend code)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AdminIndex.ascx.cs" Inherits="SitefinityWebApp.Modules.Promos.Admin.AdminIndex" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
<telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
 
<telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1" />
<telerik:RadGrid runat="server" ID="RadGrid1" DataSourceID="LinqDataSource1" AllowAutomaticUpdates="true"
    AllowAutomaticInserts="true" AllowAutomaticDeletes="true" AutoGenerateColumns="false"
    OnItemUpdated="RadGrid1_ItemUpdated" OnItemInserted="RadGrid1_ItemInserted"
    OnItemDeleted="RadGrid1_ItemDeleted" OnPreRender="RadGrid1_PreRender">
    <MasterTableView DataKeyNames="ID" DataSourceID="LinqDataSource1" CommandItemDisplay="Top" AllowPaging="false">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" HeaderText="Promo ID" ReadOnly="true"
                ForceExtractValue="Always" />
            <telerik:GridBoundColumn DataField="PromoHeader" HeaderText="Promo Header" />
            <telerik:GridBoundColumn DataField="PromoCollapsed" HeaderText="Promo Collapsed" />
            <telerik:GridBoundColumn DataField="PromoExpanded" HeaderText="Promo Expanded" />
            <telerik:GridBoundColumn DataField="PromoImageUrl" HeaderText="Promo Image Url" />
            <telerik:GridBoundColumn DataField="PromoType" HeaderText="Promo Type" />
            <telerik:GridBoundColumn DataField="PromoOrder" HeaderText="Promo Order" />
            <telerik:GridBoundColumn DataField="PromoActive" HeaderText="Promo Active?" />
            <telerik:GridEditCommandColumn ButtonType="ImageButton" />
            <telerik:GridButtonColumn ConfirmText="Delete this Promo?" ConfirmDialogType="RadWindow"
                ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
        </Columns>
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
            <table>
                <tr>
                    <td>Promo Header</td>
                    <td>Promo Collapsed Text</td>
                </tr>
                <tr>
                    <td><asp:TextBox ID="PromoHeader" runat="server" Text='<%# Bind("PromoHeader") %>'></asp:TextBox></td>
                    <td rowspan="3">
                        <telerik:RadEditor id="PromoCollapsed" runat="server" Content='<%# Bind("PromoCollapsed") %>' Height="200px">
                            <Tools>
                                <telerik:EditorToolGroup>
                                    <telerik:EditorTool Name="PasteHtml" Text="Paste Html" />
                                    <telerik:EditorTool Name="InsertTableLight" Text="Insert Table Light" />
                                    <telerik:EditorTool Name="InsertLink" Text="Insert Link Dialog" />
                                    <telerik:EditorTool Name="InsertImage" Text="Insert Image Dialog" />
                                    <telerik:EditorTool Name="ImageManager" ShortCut="CTRL+M" Text="ImageManager with ImageEditor" />
                                    <telerik:EditorTool Name="StyleBuilder" Text="Style Builder" />
                                    <telerik:EditorTool Name="XhtmlValidator" Text="Xhtml Validator Dialog" />
                                    <telerik:EditorTool Name="TrackChangesDialog" Text="Track Changes Dialog" />
                                    <telerik:EditorTool Name="FormatCodeBlock" Text="Format Code Block Dialog" />
                                </telerik:EditorToolGroup>
                                <telerik:EditorToolGroup>
                                    <telerik:EditorTool Name="SetImageProperties" />
                                    <telerik:EditorTool Name="ImageMapDialog" />
                                    <telerik:EditorTool Name="FlashManager" />
                                    <telerik:EditorTool Name="MediaManager" />
                                    <telerik:EditorTool Name="DocumentManager" />
                                    <telerik:EditorTool Name="TemplateManager" />
                                    <telerik:EditorSeparator />
                                    <telerik:EditorTool Name="FindAndReplace" />
                                    <telerik:EditorTool Name="TableWizard" />
                                    <telerik:EditorTool Name="LinkManager" />
                                    <telerik:EditorTool Name="PageProperties" />
                                    <telerik:EditorSeparator />
                                    <telerik:EditorTool Name="AboutDialog" />
                                    <telerik:EditorTool Name="Help" />
                                </telerik:EditorToolGroup>
                            </Tools>
                        </telerik:RadEditor>
                    </td>
                </tr>
                <tr>
                    <td>Promo Type</td>
                </tr>
                <tr>
                    <td>
                        <asp:DropDownList ID="PromoType" runat="server">
                            <asp:ListItem Value="casino" Text="Casino" Selected="True" />
                            <asp:ListItem Value="dining" Text="Dining" />
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>Promo Order</td>
                    <td>Promo Expanded Text</td>
                </tr>
                <tr>
                    <td><telerik:RadNumericTextBox ShowSpinButtons="true" IncrementSettings-InterceptArrowKeys="true"
                            IncrementSettings-InterceptMouseWheel="true" runat="server"
                            ID="PromoOrder" Width="200px" Text='<%# Bind("PromoOrder") %>'>                           
                        </telerik:RadNumericTextBox></td>
                    <td rowspan="3">
                        <telerik:RadEditor id="PromoExpanded" runat="server" Content='<%# Bind("PromoExpanded") %>' Height="200px">
                            <Tools>
                                <telerik:EditorToolGroup>
                                    <telerik:EditorTool Name="PasteHtml" Text="Paste Html" />
                                    <telerik:EditorTool Name="InsertTableLight" Text="Insert Table Light" />
                                    <telerik:EditorTool Name="InsertLink" Text="Insert Link Dialog" />
                                    <telerik:EditorTool Name="InsertImage" Text="Insert Image Dialog" />
                                    <telerik:EditorTool Name="ImageManager" ShortCut="CTRL+M" Text="ImageManager with ImageEditor" />
                                    <telerik:EditorTool Name="StyleBuilder" Text="Style Builder" />
                                    <telerik:EditorTool Name="XhtmlValidator" Text="Xhtml Validator Dialog" />
                                    <telerik:EditorTool Name="TrackChangesDialog" Text="Track Changes Dialog" />
                                    <telerik:EditorTool Name="FormatCodeBlock" Text="Format Code Block Dialog" />
                                </telerik:EditorToolGroup>
                                <telerik:EditorToolGroup>
                                    <telerik:EditorTool Name="SetImageProperties" />
                                    <telerik:EditorTool Name="ImageMapDialog" />
                                    <telerik:EditorTool Name="FlashManager" />
                                    <telerik:EditorTool Name="MediaManager" />
                                    <telerik:EditorTool Name="DocumentManager" />
                                    <telerik:EditorTool Name="TemplateManager" />
                                    <telerik:EditorSeparator />
                                    <telerik:EditorTool Name="FindAndReplace" />
                                    <telerik:EditorTool Name="TableWizard" />
                                    <telerik:EditorTool Name="LinkManager" />
                                    <telerik:EditorTool Name="PageProperties" />
                                    <telerik:EditorSeparator />
                                    <telerik:EditorTool Name="AboutDialog" />
                                    <telerik:EditorTool Name="Help" />
                                </telerik:EditorToolGroup>
                            </Tools>
                        </telerik:RadEditor>
                    </td>
                </tr>
                <tr>
                    <td>Is Promo Active?</td>
                </tr>
                <tr>
                    <td><asp:TextBox ID="PromoActive" runat="server" Text='<%# Bind("PromoActive") %>'/> Yes
                     
                    <br /><%# DataBinder.Eval(Container, "DataItem.PromoActive") %></td>
                </tr>
                <tr>
                    <td>Image Url</td>
                    <td></td>
                </tr>
                <tr>
                    <td><asp:TextBox ID="PromoImageUrl" runat="server" Text='<%# Bind("PromoImageUrl") %>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" colspan="2">
                        <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                </asp:Button
                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
                    </td>
                </tr>
            </table>
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
    <PagerStyle AlwaysVisible="true" />
</telerik:RadGrid>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server" />
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="SitefinityWebApp.Modules.Promos.Data.PromosDataContext"
                    EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="azb_promos">
</asp:LinqDataSource>

And here is the Codebehind for the AdminIndex page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
namespace SitefinityWebApp.Modules.Promos.Admin
    public partial class AdminIndex : System.Web.UI.UserControl
    
        protected void Page_Load(object sender, EventArgs e)
        
 
        
 
        protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)
        
            if (e.Exception != null)
            
                e.ExceptionHandled = true;
                ShowErrorMessage();
            
        
 
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        
            if (e.Exception != null)
            
                e.ExceptionHandled = true;
                ShowErrorMessage();
            
        
 
        protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
        
            if (e.Exception != null)
            
                e.ExceptionHandled = true;
                ShowErrorMessage();
            
        
 
        protected void RadGrid1_PreRender(object sender, EventArgs e)
        
            if (!IsPostBack && RadGrid1.MasterTableView.Items.Count > 0)
            
                RadGrid1.MasterTableView.Items[0].Expanded = true;
            
        
 
        private void ShowErrorMessage()
        
            RadAjaxManager1.ResponseScripts.Add(string.Format("window.radalert(\"Please enter valid data!\")"));
        
    


Thank you,

Tim

Posted by Community Admin on 07-Jun-2011 00:00

Hello Rushman,

1. Enable the ViewState on the page.
2. Check whether the commands are triggered

protected void Page_Load(object sender, EventArgs e)
        
 
 
            RadGrid1.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(RadGrid1_ItemCommand);
            RadGrid1.NeedDataSource +=new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
 
 
        
 
 
 
 
 
        void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        
            var grid = sender as RadGrid;
            grid.DataSource = CreateDataSource();
        
 
        void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        
            if (e.CommandName == "Edit")
            
            
 
            if (e.CommandName == "Delete")
            
 
            
 
            if (e.CommandName == "InitInsert")
            
            
        
 
 
        
    



Regards,
Ivan Dimitrov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed