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

FormTemplate insert and update command buttons do nothing

1 Answer 186 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 16 Oct 2008, 12:25 AM

We are using RadGrid (2008.2.1001) and are having quite a bit of difficulty getting a form template to work within the grid.

We tried both the form template and user control method, but apparently the user control method doesn't work with a LinqDataSource.

We also found that even when we used the NeedDataSource event to ensure data was continuously available, the grid would disappear if we attempted to sort for instance (or even pressed a command button within a form template.)

So we ended up adding a LinqDataSource and at least the grid stopped disappearing.

We've checked the forums, we've looked at the code, but we are a bit stumped. It seems that the Insert/Upate commands are not being fired. We click the buttons and the grid form simply closes. Using trace commands or even debugging seems like the commands aren't being fired for some reason.

We tried using a SqlDataSource just in case it may be the LinqDataSource but the results were the same.

It may be worth noting that the grid is in a user control in our case, which is in an Aspx page that utilizes a master page if that makes any difference. The ScriptManager is on the Aspx page.

Here is the applicable section of our ASCX page:

    <div class="form">  
        <div class="innerForm">  
            <p>This optional tab allows you to add a list of rooms or room types and specify the rates and conditions for each.</p> 
            <asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="Config" runat="server" HeaderText="<strong>Form Errors:</strong>" DisplayMode="List" /><asp:Literal ID="FormMessage" runat="server" /> 
                <telerik:RadGrid ID="RadGrid1" Skin="Hay" AllowFilteringByColumn="false" ShowStatusBar="true" 
                AllowPaging="true" AllowSorting="true" runat="server" AutoGenerateColumns="false" 
                 DataSourceID="SqlDataSource1" AllowAutomaticDeletes="true" AllowAutomaticInserts="true"   
                AllowAutomaticUpdates="true" OnItemCommand="RadGrid1_ItemCommand" 
                 OnItemInserted="RadGrid1_ItemInserted" OnItemUpdated="RadGrid1_ItemUpdated"   
                oninsertcommand="RadGrid1_InsertCommand">  
                    <PagerStyle Mode="NextPrevAndNumeric" /> 
                    <MasterTableView CommandItemDisplay="Top" DataSourceID="SqlDataSource1" DataKeyNames="RoomRateId">  
                        <Columns> 
                            <telerik:GridEditCommandColumn ButtonType="LinkButton" EditText="Edit" UniqueName="EditCommandColumn1" /> 
                            <telerik:GridBoundColumn DataField="RoomName" HeaderText="Room Name" 
                                SortExpression="RoomName" UniqueName="RoomName">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Rate1Persons" HeaderText="Persons" UniqueName="Rate1Persons" 
                                SortExpression="Rate1Persons">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Rate1Amount" HeaderText="Rate1 Amount" UniqueName="Rate1Amount" 
                                SortExpression="Rate1Amount">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Rate2Amount" HeaderText="Rate2 Amount" UniqueName="Rate2Amount" 
                                SortExpression="Rate2Amount">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridCheckBoxColumn DataField="Active" HeaderText="Active" UniqueName="Active" SortExpression="Active">  
                            </telerik:GridCheckBoxColumn> 
                            <telerik:GridCheckBoxColumn DataField="SelectedOffer" HeaderText="Selected" UniqueName="SelectedOffer" 
                                SortExpression="SelectedOffer">  
                            </telerik:GridCheckBoxColumn> 
                        <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete" /> 
                        </Columns> 
                        <EditFormSettings EditFormType="Template">  
                            <FormTemplate> 
                                    <p><label class="req" style="width:93px; margin-left:5px;"><asp:Literal ID="litRoomNameLabel" runat="server" Text="Room Name:" EnableViewState="true" /></label><small class="note">Ex: Standard (non-smoking)</small> 
                                    <asp:TextBox ID="txtRoomName" Text='<%# Bind( "RoomName" ) %>' CssClass="input-text" runat="server" MaxLength="50"></asp:TextBox></p>  
                                    <p><label style="width:93px; margin-left:5px;"><asp:Literal ID="Literal1" runat="server" Text="Description:" EnableViewState="true" /></label><small class="note">Ex: Standard (non-smoking)</small> 
                                    <asp:TextBox ID="txtDescription" Text='<%# Bind( "Description" ) %>' CssClass="input-text" runat="server" MaxLength="50"></asp:TextBox></p>  
                                    <p><label style="width:93px; margin-left:5px;" class="req"><asp:Literal ID="Literal2" runat="server" Text="Persons:" EnableViewState="true" /></label>  
                                    <asp:TextBox ID="txtRate1Persons" Text='<%# Bind( "Rate1Persons" ) %>' CssClass="input-text" runat="server" MaxLength="50"></asp:TextBox></p>  
                                    <p><label style="width:93px; margin-left:5px;" class="req"><asp:Literal ID="litRate1AmountLabel" runat="server" Text="Rate1Amount:" EnableViewState="true" /></label><small class="note">Ex: 79.95</small> 
                                    <asp:TextBox ID="txtRate1Amount" Text='<%# Bind( "Rate1Amount" ) %>' CssClass="input-text" runat="server" MaxLength="50"></asp:TextBox></p>  
                                    <p><label style="width:93px; margin-left:5px;"><asp:Literal ID="litRate2AmountLabel" runat="server" Text="Rate2Amount:" EnableViewState="true" /></label><small class="note">Ex: 79.95</small> 
                                    <asp:TextBox ID="txtRate2Amount" Text='<%# Bind( "Rate2Amount" ) %>' CssClass="input-text" runat="server" MaxLength="50"></asp:TextBox></p>  
                                    <p><label style="width:93px; margin-left:5px;"><asp:Literal ID="litReservationURL" runat="server" Text="ReservationURL:" EnableViewState="true" /></label><small class="note">Ex: http://myhotel.com</small> 
                                    <asp:TextBox ID="txtReservationURL" Text='<%# Bind( "ReservationURL" ) %>' CssClass="input-text" runat="server" MaxLength="50"></asp:TextBox></p>  
                                    <p><label style="width:93px; margin-left:5px;">Selected:</label> 
                                    <asp:CheckBox ID="chkIsSelected" Checked='<%# Bind( "SelectedOffer" ) %>' CssClass="input-text" runat="server"></asp:CheckBox></p>  
                                    <p><label style="width:93px; margin-left:5px;">Activate:</label> 
                                    <asp:CheckBox ID="chkIsActive" Checked='<%# Bind( "Active" ) %>' CssClass="input-text" runat="server"></asp:CheckBox></p>  
                                    <div class="buttons">  
                                        <asp:ImageButton id="btnCancel" CssClass="input-button" style="margin-right:10px" ImageUrl="~/images/btn-admin-cancel.gif" text="Cancel" runat="server" causesvalidation="false" commandname="Cancel"></asp:ImageButton> 
                                        <asp:ImageButton id="btnUpdate" CssClass="input-button"   
                                            style="margin-right:10px" ImageUrl="~/images/btn-admin-update.gif"   
                                            text="Update" ValidationGroup="AddEditFormGroup" runat="server"   
                                            CommandName="Update"   
                                            Visible='<%# !(Container is GridEditFormInsertItem) %>'></asp:ImageButton> 
                                        <asp:ImageButton id="btnInsert" CssClass="input-button" style="margin-right:10px" ImageUrl="~/images/btn-admin-submit.gif" text="Insert" ValidationGroup="AddEditFormGroup" runat="server" CommandName="PerformInsert" Visible='<%# (Container is GridEditFormInsertItem) %>'></asp:ImageButton> 
                                    </div> 
                            </FormTemplate> 
                        </EditFormSettings> 
                    </MasterTableView> 
                </telerik:RadGrid> 
        </div> 
    </div> 
      
<asp:LinqDataSource ID="LinqData" runat="server" EnableDelete="true" 
EnableInsert="true" EnableUpdate="true" 
ContextTypeName="contextinfowashere"   
TableName="RoomRates" /> 
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server"   
    ConnectionString="Data Source=connectionstringwashere"   
    DeleteCommand="DELETE FROM [RoomRate] WHERE [RoomRateId] = @RoomRateId"   
    InsertCommand="INSERT INTO [RoomRate] ([RoomName], [Description], [Rate1Persons], [Rate1Amount], [Rate2Persons], [Rate2Amount], [ReservationURL], [SelectedOffer], [Active], [ListingId], [Version]) VALUES (@RoomName, @Description, @Rate1Persons, @Rate1Amount, @Rate2Persons, @Rate2Amount, @ReservationURL, @SelectedOffer, @Active, @ListingId, @Version)"   
    ProviderName="System.Data.SqlClient"   
    SelectCommand="SELECT * FROM [RoomRate] WHERE ([ListingId] = @ListingId)"   
    UpdateCommand="UPDATE [RoomRate] SET [RoomName] = @RoomName, [Description] = @Description, [Rate1Persons] = @Rate1Persons, [Rate1Amount] = @Rate1Amount, [Rate2Persons] = @Rate2Persons, [Rate2Amount] = @Rate2Amount, [ReservationURL] = @ReservationURL, [SelectedOffer] = @SelectedOffer, [Active] = @Active, [ListingId] = @ListingId, [Version] = @Version WHERE [RoomRateId] = @RoomRateId">  
    <SelectParameters> 
        <asp:QueryStringParameter Name="ListingId" QueryStringField="id" Type="Int32" /> 
    </SelectParameters> 
    <DeleteParameters> 
        <asp:Parameter Name="RoomRateId" Type="Int32" /> 
    </DeleteParameters> 
    <UpdateParameters> 
        <asp:Parameter Name="RoomName" Type="String" /> 
        <asp:Parameter Name="Description" Type="String" /> 
        <asp:Parameter Name="Rate1Persons" Type="String" /> 
        <asp:Parameter Name="Rate1Amount" Type="Decimal" /> 
        <asp:Parameter Name="Rate2Persons" Type="String" /> 
        <asp:Parameter Name="Rate2Amount" Type="Decimal" /> 
        <asp:Parameter Name="ReservationURL" Type="String" /> 
        <asp:Parameter Name="SelectedOffer" Type="Boolean" /> 
        <asp:Parameter Name="Active" Type="Boolean" /> 
        <asp:Parameter Name="ListingId" Type="Int32" /> 
        <asp:Parameter Name="Version" Type="Object" /> 
        <asp:Parameter Name="RoomRateId" Type="Int32" /> 
    </UpdateParameters> 
    <InsertParameters> 
        <asp:Parameter Name="RoomName" Type="String" /> 
        <asp:Parameter Name="Description" Type="String" /> 
        <asp:Parameter Name="Rate1Persons" Type="String" /> 
        <asp:Parameter Name="Rate1Amount" Type="Decimal" /> 
        <asp:Parameter Name="Rate2Persons" Type="String" /> 
        <asp:Parameter Name="Rate2Amount" Type="Decimal" /> 
        <asp:Parameter Name="ReservationURL" Type="String" /> 
        <asp:Parameter Name="SelectedOffer" Type="Boolean" /> 
        <asp:Parameter Name="Active" Type="Boolean" /> 
        <asp:Parameter Name="ListingId" Type="Int32" /> 
        <asp:Parameter Name="Version" Type="Object" /> 
    </InsertParameters> 
</asp:SqlDataSource> 
 

Here is the relevant portion of the codebehind file:

    protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)  
    {  
        if (e.Exception != null)  
        {  
            e.KeepInEditMode = true;  
            e.ExceptionHandled = true;  
            AddMessage("RoomRate " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RoomRateId"] + " cannot be updated. Reason: " + e.Exception.Message, FormMessage);  
        }  
        else 
        {  
            AddMessage("RoomRate " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RoomRateId"] + " updated", FormMessage, false);  
        }  
    }  
 
    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)  
    {  
        Trace.Warn("ItemInserted""We made it!");  
        if (e.Exception != null)  
        {  
            e.ExceptionHandled = true;  
            e.KeepInInsertMode = true;  
            AddMessage("Error: " + e.Exception.Message, FormMessage);  
        }  
        else 
        {  
            AddMessage("RoomRate " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RoomRateId"] + " inserted", FormMessage, false);  
        }  
    }  
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
    {  
        if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked  
        {  
           Trace.Write("ItemCommand""Add New button was clicked");  
            e.Canceled = true;  
 
            //Prepare an IDictionary with the predefined values  
            System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();  
 
            //set default checked state for the checkbox inside the EditItemTemplate  
            newValues["SelectedOffer"] = false;  
            newValues["Active"] = true;  
 
            //Insert the item and rebind  
            e.Item.OwnerTableView.InsertItem(newValues);  
        }  
        else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)  
        {  
            e.Canceled = true;  
        }  
        else 
        {  
               Trace.Write("ItemCommand""Edit button was clicked");  
        }  
    }  
 
    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)  
    {  
        Trace.Write("InsertCommand""Attempting to insert");  
    } 

Any insight, advice or solutions would be greatly appreciated.

1 Answer, 1 is accepted

Sort by
0
Accepted
Nikolay Rusev
Telerik team
answered on 17 Oct 2008, 08:31 AM
Hello Jonathan,

For your convenience I've prepared sample project. It demonstrates how to create RadGrid and bind it via LinqDataSource. You can see that grid control supports insert/update/delete operations completely codeless.
Also all command event will be fired.

You can find similar example here.

Please find the project attached to this forum post.

Regards,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Anthony
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or