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

Radgrid in master page does not update when the form template is contained in the content page

1 Answer 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matteo
Top achievements
Rank 1
Matteo asked on 28 Oct 2020, 02:19 PM

Hi,
I have a master page with a grid that inherits the form template from a content page. The fields are displayed correctly in edit and all valued but when I press update it says that the fields cannot be null.
This problem occurs only if the form template is on the content page while if you put the template directly on the master page then everything works.
I used a code similar to the demo https://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx as an example.
What I do wrong and how I can fix it?
Thanks

 

ASPX Content Page

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MasterTKTest2.master" AutoEventWireup="true" CodeFile="ChildTKTest2.aspx.cs" Inherits="ChildTKTest2" %>
 
<asp:Content ID="mycontent" ContentPlaceHolderID="FormTemplatePlaceHolder" runat="server">
     
   <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
    style="border-collapse: collapse;">
    <tr class="EditFormHeader">
        <td colspan="2">
            <b>Cat1</b>
        </td>
    </tr>
    <tr>
        <td>
            <table id="Table3" width="450px" border="0" class="module">
                <tr>
                    <td class="title" style="font-weight: bold;" colspan="2">Company Info:</td>
                </tr>
                <tr>
                    <td>Descrizione:
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("CatDesc1") %>'>
                        </asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Libero 1a:
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Libero1a") %>' TabIndex="1">
                        </asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Libero 1b:
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Libero1b") %>' TabIndex="2">
                        </asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <b>Campi default:</b>
                    </td>
                </tr>
                <tr>
                    <td>Data ora ins:
                    </td>
                    <td>
                        <telerik:RadDatePicker RenderMode="Lightweight" ID="BirthDatePicker" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("DataOraIns") %>'
                            TabIndex="4">
                        </telerik:RadDatePicker>
                    </td>
                </tr>
                <tr>
                    <td>Data ora mod:
                    </td>
                    <td>
                        <telerik:RadDatePicker RenderMode="Lightweight" ID="HireDatePicker" DbSelectedDate='<%# Bind( "DataOraMod") %>'
                            runat="server" TabIndex="10">
                        </telerik:RadDatePicker>
                    </td>
                </tr>
                <tr>
                    <td>Utente:
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox4" Text='<%# Bind( "IDUtente") %>' runat="server" TabIndex="11">
                        </asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox2" Text='<%# Bind( "IDUtenteUltimaMod") %>' runat="server" TabIndex="12">
                        </asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox3" Text='<%# Bind( "NumMod") %>' runat="server" TabIndex="13">
                        </asp:TextBox>
                    </td>
                </tr>
            </table>
        </td>
                                 
    </tr>
    <tr>
        <td colspan="2"></td>
    </tr>
    <tr>
        <td></td>
        <td></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>
            
        
</asp:Content>

 

CS Master Page

public partial class MasterPages_MasterTKTest2 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.ConnectionString = Siseco.GatEnv.Environment.Database.GetConnectionString(string.Empty, false);
    }
 
    protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            DisplayMessage(true, "Cat1 " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IDCat1"] + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "Cat1 " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IDCat1"] + " updated");
        }
    }
 
    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            e.KeepInInsertMode = true;
            DisplayMessage(true, "Cat1 cannot be inserted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "Cat1 inserted");
        }
    }
 
    protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            DisplayMessage(true, "Cat1 " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IDCat1"] + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "Cat1 " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IDCat1"] + " deleted");
        }
    }
 
    private void DisplayMessage(bool isError, string text)
    {
        Label label = (isError) ? this.Label1 : this.Label2;
        label.Text = text;
    }
 
 
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
        {
            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
            editColumn.Visible = false;
        }
        else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
        {
            e.Canceled = true;
        }
        else
        {
            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
            if (!editColumn.Visible)
                editColumn.Visible = true;
        }
 
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RadGrid1.EditIndexes.Add(0);
            RadGrid1.Rebind();
        }
    }
}

 

ASPX Master Page

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterTKTest2.master.cs" Inherits="MasterPages_MasterTKTest2" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title>Telerik ASP.NET Example</title>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function RowDblClick(sender, eventArgs) {
                sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
            }
        </script>
    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <p id="divMsgs" runat="server">
        <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#FF8080">
        </asp:Label>
        <asp:Label ID="Label2" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#00C000">
        </asp:Label>
    </p>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    <telerik:AjaxUpdatedControl ControlID="divMsgs"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
    <telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" DecorationZoneID="demo" DecoratedControls="All" EnableRoundedCorners="false" />
    <div id="demo" class="demo-container no-bg">
        <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" CssClass="RadGrid" GridLines="None"
            AllowPaging="True" PageSize="20" AllowSorting="True" AutoGenerateColumns="False"
            ShowStatusBar="true" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
            AllowAutomaticUpdates="True" DataSourceID="SqlDataSource1" OnItemDeleted="RadGrid1_ItemDeleted"
            OnItemInserted="RadGrid1_ItemInserted" OnItemUpdated="RadGrid1_ItemUpdated" OnItemCommand="RadGrid1_ItemCommand"
            OnPreRender="RadGrid1_PreRender">
            <MasterTableView CommandItemDisplay="TopAndBottom" DataSourceID="SqlDataSource1"
                DataKeyNames="IDCat1">
                <Columns>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn UniqueName="IDCat1" HeaderText="ID" DataField="IDCat1">
                        <HeaderStyle Width="70px"></HeaderStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="CatDesc1" HeaderText="Desc" DataField="CatDesc1">
                        <HeaderStyle Width="80px"></HeaderStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="Libero1a" HeaderText="Libero1a" DataField="Libero1a">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="Libero1b" HeaderText="Libero1b" DataField="Libero1b">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="DataOraIns" HeaderText="DataOraIns" DataField="DataOraIns"
                        DataFormatString="{0:d}">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="DataOraMod" HeaderText="DataOraMod" DataField="DataOraMod"
                        DataFormatString="{0:d}">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="IDUtente" HeaderText="IDUtente" DataField="IDUtente">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="IDUtenteUltimaMod" HeaderText="IDUtenteUltimaMod" DataField="IDUtenteUltimaMod">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="NumMod" HeaderText="NumMod" DataField="NumMod">
                    </telerik:GridBoundColumn>
                     
                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="column">
                    </telerik:GridButtonColumn>
                </Columns>
                <EditFormSettings EditFormType="Template">
                    <FormTemplate>
                        <asp:ContentPlaceHolder ID="FormTemplatePlaceHolder" runat="server">
                        </asp:ContentPlaceHolder>
                    </FormTemplate>
                </EditFormSettings>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
            </ClientSettings>
        </telerik:RadGrid>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        DeleteCommand="DELETE FROM [Cat1] WHERE [IDCat1] = @IDCat1"
        InsertCommand="INSERT INTO [Cat1] ([CatDesc1], [Libero1a], [Libero1b], [DataOraIns], [DataOraMod], [IDUtente], [IDUtenteUltimaMod], [NumMod]) VALUES (@CatDesc1, @Libero1a, @Libero1b, @DataOraIns, @DataOraMod, @IDUtente, @IDUtenteUltimaMod, @NumMod)"
        SelectCommand="SELECT * FROM [Cat1]" UpdateCommand="UPDATE [Cat1] SET [CatDesc1] = @CatDesc1, [Libero1a] = @Libero1a, [Libero1b] = @Libero1b, [DataOraIns] = @DataOraIns, [DataOraMod] = @DataOraMod, [IDUtente] = @IDUtente, [IDUtenteUltimaMod] = @IDUtenteUltimaMod, [NumMod] = @NumMod WHERE [IDCat1] = @IDCat1">
        <DeleteParameters>
            <asp:Parameter Name="IDCat1" Type="Int32"></asp:Parameter>
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="CatDesc1" Type="String"></asp:Parameter>
            <asp:Parameter Name="Libero1a" Type="String"></asp:Parameter>
            <asp:Parameter Name="Libero1b" Type="String"></asp:Parameter>
            <asp:Parameter Name="DataOraIns" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="DataOraMod" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="IDUtente" Type="String"></asp:Parameter>
            <asp:Parameter Name="IDUtenteUltimaMod" Type="String"></asp:Parameter>
            <asp:Parameter Name="NumMod" Type="String"></asp:Parameter>
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="CatDesc1" Type="String"></asp:Parameter>
            <asp:Parameter Name="Libero1a" Type="String"></asp:Parameter>
            <asp:Parameter Name="Libero1b" Type="String"></asp:Parameter>
            <asp:Parameter Name="DataOraIns" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="DataOraMod" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="IDUtente" Type="String"></asp:Parameter>
            <asp:Parameter Name="IDUtenteUltimaMod" Type="String"></asp:Parameter>
            <asp:Parameter Name="NumMod" Type="String"></asp:Parameter>
            <asp:Parameter Name="IDCat1" Type="Int32"></asp:Parameter>
        </UpdateParameters>
    </asp:SqlDataSource>
    <br />
    </form>
</body>
</html>

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 03 Dec 2020, 10:03 AM

Hi Matteo,

Thank you for the provided code!

In general, the Master and the Content page act as separate containers for their respective controls. The purpose of the MasterPage is to act as a Layout for rendering different content pages.

With the current structure, you can either have the Template declared on the same page as the RadGrid itself or embed it in a WebUserControl and use it as an external EditForm.

Specifically for cases like this RadGrid exposes the option to use WebUserControl as EditFormType. That way, the same UserControl can be reused in multiple RadGrid controls. You can check out the article and demo explaining this approach in the following links:

With this approach, you will need to handle the updates manually as explained in the Updating Values Using UserControl and FormTemplate article.

For your convenience, I have attached an isolated runnable version of the demo so you can give it a try.

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Matteo
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or