Different Autogenerated Edit Form For Insert And Edit Operations

2 posts, 0 answers
  1. Shinu
    Shinu avatar
    17762 posts
    Member since:
    Mar 2007

    Posted 28 Sep 2007 Link to this post

    Requirements

    RadControls version

    5.0.0

    .NET version

    2.0

    Visual Studio version

    2005

    programming language

    C#

    browser support

    all browsers supported by RadControls


     
    PROJECT DESCRIPTION
    This project explains how to use Different Autogenerated EditForm For Insert And Edit Operations.

    .aspx:
     <radG:RadGrid ID="RadGrid1" Skin="Brick" GridLines="None" EnableAJAX="True" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" Width="97%" 
                AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SessionDataSource1" 
                OnItemUpdated="RadGrid1_ItemUpdated" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted" OnDataBound="RadGrid1_DataBound" OnItemCommand="RadGrid1_ItemCommand" > 
                <PagerStyle Mode="NextPrevAndNumeric" /> 
                <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="ProductID" DataSourceID="SessionDataSource1" HorizontalAlign="NotSet">  
                    <Columns> 
                        <radG:GridBoundColumn DataField="ProductID" HeaderText="ID" ReadOnly="True" 
                            SortExpression="ProductID" UniqueName="ProductID" Visible="False">  
                            <HeaderStyle Width="20px" ForeColor="Silver" />   
                            <ItemStyle ForeColor="Silver" /> 
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" 
                            UniqueName="ProductName">  
                        </radG:GridBoundColumn> 
                        <radG:GridDropDownColumn DataField="CategoryID" DataSourceID="AccessDataSource2" 
                            HeaderText="Category" ListTextField="CategoryName" ListValueField="CategoryID" 
                            UniqueName="CAtegoryID">   
                        </radG:GridDropDownColumn> 
                        <radG:GridBoundColumn DataField="UnitsInStock" HeaderText="Units In Stock" SortExpression="UnitsInStock" 
                            UniqueName="UnitsInStock">  
                        </radG:GridBoundColumn> 
                        <radG:GridBoundColumn DataField="QuantityPerUnit" HeaderText="Quantity Per Unit" SortExpression="QuantityPerUnit" 
                            UniqueName="QuantityPerUnit" Visible="False" EditFormColumnIndex="1">  
                        </radG:GridBoundColumn> 
                        <radG:GridCheckBoxColumn DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" 
                            UniqueName="Discontinued" EditFormColumnIndex="1">  
                        </radG:GridCheckBoxColumn> 
                        <radG:GridTemplateColumn HeaderText="UnitPrice" SortExpression="UnitPrice" UniqueName="TemplateColumn" EditFormColumnIndex="1">  
                            <ItemTemplate> 
                                <asp:Label runat="server" ID="lblUnitPrice" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label> 
                            </ItemTemplate> 
                            <EditItemTemplate> 
                                <asp:TextBox runat="server" ID="tbUnitPrice" Text='<%# Bind("UnitPrice") %>'></asp:TextBox><span style="color:Red">*</span><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="tbUnitPrice" ErrorMessage="Required" runat="server" > 
                                </asp:RequiredFieldValidator> 
                            </EditItemTemplate> 
                        </radG:GridTemplateColumn> 
                        <radG:GridButtonColumn ConfirmText="Delete this product?" ButtonType="ImageButton" ImageUrl="RadControls/Grid/Skins/Brick/Delete.gif" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">  
                            <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> 
                        </radG:GridButtonColumn> 
                          
                        <radg:GridEditCommandColumn ButtonType="ImageButton" UpdateImageUrl="RadControls/Grid/Skins/Brick/Update.gif" 
                        EditImageUrl="RadControls/Grid/Skins/Brick/Edit.gif" InsertImageUrl="RadControls/Grid/Skins/Brick/Insert.gif" 
                        CancelImageUrl="RadControls/Grid/Skins/Brick/Cancel.gif">  
                            <ItemStyle CssClass="MyImageButton" /> 
                        </radg:GridEditCommandColumn> 
                    </Columns> 
                    <EditFormSettings ColumnNumber="2" CaptionDataField="ProductName" CaptionFormatString="Edit properties of Product {0}">  
                        <FormTableItemStyle Wrap="False"></FormTableItemStyle> 
                            <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle> 
                            <FormMainTableStyle CellSpacing="0" CellPadding="3" BackColor="White" Width="100%" /> 
                            <FormTableStyle CellSpacing="0" CellPadding="2" Height="110px" 
                                BackColor="White" /> 
                            <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                            <EditColumn ButtonType="ImageButton" UpdateImageUrl="RadControls/Grid/Skins/Brick/Update.gif" EditImageUrl="RadControls/Grid/Skins/Brick/Edit.gif" InsertImageUrl="RadControls/Grid/Skins/Brick/Insert.gif" CancelImageUrl="RadControls/Grid/Skins/Brick/Cancel.gif" InsertText="Insert Order" UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit"></EditColumn> 
                            <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle> 
                    </EditFormSettings> 
                    <ExpandCollapseColumn Resizable="False" Visible="False">  
                        <HeaderStyle Width="20px" /> 
                    </ExpandCollapseColumn> 
                    <RowIndicatorColumn Visible="False">  
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                </MasterTableView>   
                 <ExportSettings> 
                     <Pdf PageBottomMargin="" PageFooterMargin="" PageHeaderMargin="" PageHeight="11in" 
                         PageLeftMargin="" PageRightMargin="" PageTopMargin="" PageWidth="8.5in" /> 
                 </ExportSettings> 
            </radG:RadGrid><br /> 
            
            <cc1:SessionDataSource ID="SessionDataSource1" runat="server" DataFile="~/App_Data/NWind.mdb" 
                SelectCommand="SELECT ProductID, ProductName, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, Discontinued FROM [Products]"   
                DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = ?"   
                InsertCommand="INSERT INTO Products(ProductName, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, Discontinued) VALUES (?, ?, ?, ?, ?, ?)"   
                UpdateCommand="UPDATE [Products] SET [ProductName] = ?, [CategoryID] = ?, [QuantityPerUnit] = ?, [UnitPrice] = ?, [UnitsInStock] = ?, [Discontinued] = ? WHERE [ProductID] = ? AND [ProductName] = ? AND [CategoryID] = ? AND [QuantityPerUnit] = ? AND [UnitPrice] = ? AND [UnitsInStock] = ? AND [Discontinued] = ?"   
                AutoincrementFields="ProductID" OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues">  
                <DeleteParameters> 
                    <asp:Parameter Name="original_ProductID" Type="Int32" /> 
                </DeleteParameters> 
                <UpdateParameters> 
                    <asp:Parameter Name="ProductName" Type="String" /> 
                    <asp:Parameter Name="QuantityPerUnit" Type="String" /> 
                    <asp:Parameter Name="UnitPrice" Type="Decimal" /> 
                    <asp:Parameter Name="UnitsInStock" Type="Int16" /> 
                    <asp:Parameter Name="Discontinued" Type="Boolean" /> 
                    <asp:Parameter Name="original_ProductID" Type="Int32" /> 
                    <asp:Parameter Name="original_ProductName" Type="String" /> 
                    <asp:Parameter Name="original_QuantityPerUnit" Type="String" /> 
                    <asp:Parameter Name="original_UnitPrice" Type="Decimal" /> 
                    <asp:Parameter Name="original_UnitsInStock" Type="Int16" /> 
                    <asp:Parameter Name="original_Discontinued" Type="Boolean" /> 
                </UpdateParameters> 
                <InsertParameters> 
                    <asp:Parameter Name="ProductName" Type="String" /> 
                    <asp:Parameter Name="QuantityPerUnit" Type="String" /> 
                    <asp:Parameter Name="UnitPrice" Type="Decimal" /> 
                    <asp:Parameter Name="UnitsInStock" Type="Int16" /> 
                    <asp:Parameter Name="Discontinued" Type="Boolean" /> 
                </InsertParameters> 
            </cc1:SessionDataSource> 
           
    <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/Nwind.mdb" 
    SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:AccessDataSource>   
         

    //C#:
    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Collections;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Web.UI.HtmlControls;  
    using Telerik.WebControls;  
     
    public partial class _Default : System.Web.UI.Page   
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
     
        }  
        protected void RadGrid1_ItemUpdated(object source, Telerik.WebControls.GridUpdatedEventArgs e)  
        {  
            if (e.Exception != null)  
            {  
                e.KeepInEditMode = true;  
                e.ExceptionHandled = true;  
                SetMessage("Product " + e.Item["ProductID"].Text + " cannot be updated. Reason: " + e.Exception.Message);  
            }  
            else  
            {  
                SetMessage("Product " + e.Item["ProductID"].Text + " updated");  
            }  
        }  
     
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)  
        {  
            if (e.Exception != null)  
            {  
                e.ExceptionHandled = true;  
                SetMessage("Product cannot be inserted. Reason: " + e.Exception.Message);  
            }  
            else  
            {  
                SetMessage("Product inserted");  
            }  
        }  
     
        protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)  
        {  
            if (e.Exception != null)  
            {  
                e.ExceptionHandled = true;  
                SetMessage("Product " + e.Item["ProductID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);  
            }  
            else  
            {  
                SetMessage("Product " + e.Item["ProductID"].Text + " deleted");  
            }  
        }  
     
     
     
     
        private void DisplayMessage(string text)  
        {  
            RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));  
        }  
     
        private void SetMessage(string message)  
        {  
            gridMessage = message;  
     
        }  
     
        private string gridMessage = null;  
        protected void RadGrid1_DataBound(object sender, EventArgs e)  
        {  
            if (!string.IsNullOrEmpty(gridMessage))  
            {  
                DisplayMessage(gridMessage);  
            }  
        }  
        //Selecting the EditMode accordinf Insert/Edit mode  
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            if (e.CommandName == "InitInsert")  
            {  
                //Clearing all the Edit forms  
                RadGrid1.MasterTableView.ClearEditItems();  
                //Chainging the "EditMode" to "EditForm"  
                RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms;  
                RadGrid1.Rebind();  
                //Set the RadGrid in Insert Mode   
                RadGrid1.MasterTableView.IsItemInserted = true;  
            }  
            if (e.CommandName == "Edit")  
            {  
                //Clear the insert form   
                RadGrid1.MasterTableView.IsItemInserted = false;  
                //Changing the "EditMode"  to "InPlace"  
                RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace;  
                RadGrid1.Rebind();  
                //Set the clicked row to "Edit" mode.  
                RadGrid1.MasterTableView.Items[e.Item.ItemIndex].Edit = true;  
     
            }  
     
        }  
    }  
     

  2. Sebastian
    Admin
    Sebastian avatar
    9807 posts
    Member since:
    Sep 2012

    Posted 03 Oct 2007 Link to this post

    Hi Shinu,

    Thank you for submitting this sample project in our code library section. It can be indeed helpful for people which would like to switch the auto-generated edit form type when performing editing/inserting operation. I have updated your Telerik points for the involvement.

    Best regards,
    Stephen
    the Telerik team


    Instantly find answers to your questions at the new Telerik Support Center

Back to Top