Open radgrid form template in Insert Mode on Page Load

20 posts, 1 answers
  1. Jennifer
    Jennifer avatar
    11 posts
    Member since:
    Mar 2008

    Posted 25 Jun 2008 Link to this post

    Hello,

    I have a radgrid on a page.  On pageload I would like to open the form template of the radgrid in insert mode so that a user can insert without having to click the "Add new record" link.  Is this possible?  If so, how can it be done?  Below is the radgrid code:

    <

    rad:RadGrid ID="grd" runat="server" AutoGenerateColumns="false" AllowSorting="true" Skin="None"

    CssClass="" GridLines="Horizontal" OnNeedDataSource="NeedDataSource" OnItemCommand="ItemCommand"

    OnUpdateCommand="UpdateCommand" OnDeleteCommand="DeleteCommand" OnInsertCommand="InsertCommand"

    EnableAJAX="true">

    <MasterTableView DataKeyNames="ID" CommandItemDisplay="Bottom" >

    <HeaderStyle CssClass="" />

    <AlternatingItemStyle CssClass="" />

    <Columns>

    <rad:GridTemplateColumn UniqueName="ID"  SortExpression="ID" >

    <ItemStyle CssClass="" />

    <ItemTemplate>

    <%

    #  %>

    </ItemTemplate>

    </rad:GridTemplateColumn>

    <rad:GridTemplateColumn UniqueName="" HeaderText="" SortExpression="">

    <ItemStyle CssClass="" />

    <ItemTemplate>

    <%

    #  %>

    </ItemTemplate>

    </rad:GridTemplateColumn>

    <rad:GridTemplateColumn UniqueName=n" >

    <ItemStyle CssClass="" />

    <ItemTemplate>

    <%

    # %>

    </ItemTemplate>

    </rad:GridTemplateColumn>

    <rad:GridTemplateColumn UniqueName=""  SortExpression="">

    <ItemStyle CssClass="" />

    <ItemTemplate>

    <%

    # Eval("")%>

    </ItemTemplate>

    </rad:GridTemplateColumn>

    <rad:GridTemplateColumn UniqueName="">

    <ItemStyle CssClass=""/>

    <ItemTemplate>

    <asp:ImageButton runat="server" ID="btnEdit"  CommandName="Edit"/>

    <asp:ImageButton runat="server" ID="btnDelete"  CommandName="Delete"

    OnClientClick="" CommandArgument='<%# Eval("")%>'/>

    </ItemTemplate>

    </rad:GridTemplateColumn>

    </Columns>

    <EditFormSettings EditFormType="Template">

    <FormTemplate>

    <div class="">

    <asp:Label runat="server" ID="lblAddEditTitle" ></asp:Label>

    </div>

    <div class="editRow">

    <div class="editTitleColumn">

     

    </div>

    <div class="editColumn">

    <asp:DropDownList runat="server" ID=""></asp:DropDownList>

    </div>

    <div class="editTitleColumn">

     

    </div>

    <div class="editColumn">

    <asp:TextBox runat="server" ID="" CssClass="" Text=''></asp:TextBox>

    </div>

    </div>

    <div class="editRow">

    <div class="editTitleColumn">

     

    </div>

    <div class="editColumn">

    <asp:DropDownList runat="server" ID=""></asp:DropDownList>

    </div>

    <div class="editTitleColumn">

     

    </div>

    <div class="editColumn">

    <asp:TextBox runat="server" ID="" TextMode="MultiLine" Rows="3" CssClass=""></asp:TextBox>

    </div>

    </div>

    <div class="row">

    <div class="column">

    <asp:ImageButton runat="server" ID="btnUpdate"

    CommandName='' />&nbsp;

    </div>

    <div class="column">

    <asp:ImageButton runat="server" ID="btnCancel"  CommandName="Cancel" />

    </div>

    </div>

    </FormTemplate>

    </EditFormSettings>

    <CommandItemTemplate>

    <div class="commandItem">

    <asp:LinkButton runat="server" ID="btnAddEntry" Text="Add New Entry" CommandName="InitInsert"></asp:LinkButton>

    </div>

    </CommandItemTemplate>

    </MasterTableView>

    </rad:RadGrid>

    Thank you,

    Jennifer

  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Jun 2008 Link to this post

    Hi Jennifer,

    Check out the code snippet given below.

    CS:
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
        { 
            if (!Page.IsPostBack) 
            { 
                RadGrid1.MasterTableView.IsItemInserted = true
                RadGrid1.Rebind(); 
            } 
        } 

    Thanks
    Princy.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jennifer
    Jennifer avatar
    11 posts
    Member since:
    Mar 2008

    Posted 27 Jun 2008 Link to this post

    Hi Princy,

    Thank you very much for your response.  The code snippet worked perfectly.

    Jennifer
  5. Thomas Hundley
    Thomas Hundley avatar
    9 posts
    Member since:
    Feb 2008

    Posted 27 Jun 2008 Link to this post

    Hi.  I'm about to start playing around with the code, but I need to do the exact same thing, but in Edit mode.  I'll grab the datakey of the object from the querystring and would like to have the page open in edit mode for that object.

    I can see some problems with pagination in this scenario too.

    Any quick thoughts or examples?

    Thanks!
    Tom
  6. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 30 Jun 2008 Link to this post

    Hello Thomas,

    My suggestion in this case is to traverse directly all underlying records from the grid source server-side and identify the record of interest by its key value. Some pointers about the approach you can find in this documentation topic:

    http://www.telerik.com/help/aspnet-ajax/grdtotalsingridfooters.html (paragraph Displaying totals for all grid pages)

    When you find the relevant item in the source, intercept the PreRender event of the grid, switch to the corresponding page (setting the CurrentPageIndex property of the grid), reference the grid row using the FindItemByKeyValue(keyname, keyvalue) method, set the Edit property of the row to true and refresh the grid invoking its Rebind() method.

    Best regards,
    Stephen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  7. Thomas Hundley
    Thomas Hundley avatar
    9 posts
    Member since:
    Feb 2008

    Posted 08 Jul 2008 Link to this post

    Thank you Stephen.  I had to poke around a bit for a few more references, but I got it working like a charm.  I'll post the code here in case it helps someone else out.  I needed this code both to 1) select the page and row after a record was inserted on a sorted/paginated grid and 2) select the page and row in edit mode from another page (like a querystring or whatever...)

    If there is a better way to do this, I'd love to know, but for now this works great.

        protected void rgBusinesses_PreRender(object sender, EventArgs e)  
        {  
            if (Session[sessionNewRecord] != null)  
            {  
                Guid businessRefId = new Guid(Session[sessionNewRecord].ToString());  
                Session[sessionNewRecord] = null;  
     
                int pageIndex = 0;  
                int rowIndex =0 ;  
     
                for (int i =0; i < ((VList<BusinessFlat>)rgBusinesses.DataSource).Count; i++)  
                {  
                    if (((VList<BusinessFlat>)rgBusinesses.DataSource)[i].BusinessRefId == businessRefId)  
                    {  
                        rowIndex = i;  
                        pageIndex = Convert.ToInt32(Decimal.Round((rowIndex / rgBusinesses.PageSize),0));  
                    }  
                }  
                  
                rgBusinesses.CurrentPageIndex = pageIndex;  
                rgBusinesses.Rebind();  
     
                //do this if you just want to select it, such as after inserting on a sorted grid  
                rgBusinesses.SelectedIndexes.Add(rgBusinesses.MasterTableView.FindItemByKeyValue("BusinessRefId", businessRefId).ItemIndex);  
                  
                //do this if you want to open the row in edit mode  
                //rgBusinesses.MasterTableView.FindItemByKeyValue("BusinessRefId", businessRefId).Edit = true;  
                //rgBusinesses.Rebind();  
            }  
        }  
     
  8. Thomas Hundley
    Thomas Hundley avatar
    9 posts
    Member since:
    Feb 2008

    Posted 11 Jul 2008 Link to this post

    For what it's worth, I found a few bugs in the code I provided- here are the fixes.  Hope it helps someone.

       protected void rgStores_PreRender(object sender, EventArgs e)  
        {  
            if (!string.IsNullOrEmpty(rgStores.MasterTableView.FilterExpression))  
            {  
                Session[sessionFilterExpression] = rgStores.MasterTableView.FilterExpression;  
                foreach (Object key in filterHashTable.Keys)  
                {  
                    GridColumn column = rgStores.MasterTableView.GetColumnSafe(key.ToString());  
                    Pair filterPair = (Pair)filterHashTable[key];  
                    column.CurrentFilterValue = filterPair.Second.ToString();  
                }  
                rgStores.Rebind();  
            }  
     
            if (Session[sessionObjectToFind] != null)  
            {  
                Guid storeRefId = new Guid(Session[sessionObjectToFind].ToString());  
                Session[sessionObjectToFind] = null;  
     
                int pageIndex = 0;  
                int rowIndex = 0;  
     
                for (int i = 0; i < ((VList<StoreFlat>)rgStores.DataSource).Count; i++)  
                {  
                    if (((VList<StoreFlat>)rgStores.DataSource)[i].StoreRefId == storeRefId)  
                    {  
                        rowIndex = i;  
                        pageIndex = Convert.ToInt32(Decimal.Truncate((rowIndex / rgStores.PageSize)));  
                    }  
                }  
     
                rgStores.CurrentPageIndex = pageIndex;  
                rgStores.Rebind();  
     
                try  
                {  
                    rgStores.SelectedIndexes.Add(rgStores.MasterTableView.FindItemByKeyValue("StoreRefId", storeRefId).ItemIndex);  
                }  
                catch (Exception ex)  
                {  
                    //this is here for filtering  
                    //TODO: figure out a good way to new filtered items  
                }  
            }  
     
            if (Session[GridMessage.SessionDisplayMessage] != null)  
            {  
                GridMessage.DisplayMessage(rgStores, (DisplayMessage)Session[GridMessage.SessionDisplayMessage], false, this.Context);  
            }  
        } 

    And then you need sorting logic in your NeedDataSource handler:
        protected void rgStores_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            VList<StoreFlat> stores = new DataRepository.StoreFlatProvider.GetAll();  
       
            if (rgStores.MasterTableView.SortExpressions.Count == 0)  
            {  
                stores.Sort("DivisionName, StoreName");  
            }  
            else  
            {  
                string sortExpression = string.Empty;  
                for (int i = 0; i < rgStores.MasterTableView.SortExpressions.Count; i++)  
                {  
                    sortExpression += rgStores.MasterTableView.SortExpressions[i].ToString();  
                    if (i + 1 < rgStores.MasterTableView.SortExpressions.Count)  
                    {  
                        sortExpression += ",";  
                    }  
                }  
                stores.Sort(sortExpression);  
            }  
     
            rgStores.DataSource = stores;  
        } 
  9. Cathy
    Cathy avatar
    7 posts
    Member since:
    Apr 2009

    Posted 07 Apr 2009 Link to this post

    Hello,

    I'm trying to do the same thing as described in the original post.

    When I use the suggested code:

    protected void RadGrid1_PreRender(object sender, EventArgs e) 
        { 
            if (!Page.IsPostBack) 
            { 
                RadGrid1.MasterTableView.IsItemInserted = true
                RadGrid1.Rebind(); 
            } 
        } 

    what I get is an empty editform without any of the columns from the radgrid and inserts can be made.  However, the editform does contain the Insert Cancel command in it.  This occurs whether I use the popup, in-place, or editform. 

    The columns for my radgrid are programmatically added. 

    Has anyone encounter this?  What I'm I doing wrong?

    thanks in advance,
    cathy 
  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Apr 2009 Link to this post

    Hi,

    Are you using an autogenerated editform or FormTemplate/UserControl editform? If you are using any edit form other than the autogenerated one make sure that you have added enough TextBox controls in the edit form.

    Shinu
  11. Cathy
    Cathy avatar
    7 posts
    Member since:
    Apr 2009

    Posted 08 Apr 2009 Link to this post

    Hi,

    Thanks for the quick reply.

    I'm using an autogenerated editform. 

    Any suggestions?

    thanks,
    cathy


    Correction to my original post:  "what I get is an empty editform without any of the columns from the radgrid and inserts can't be made.  However, the editform does contain the Insert Cancel command in it." 
  12. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Apr 2009 Link to this post

    Hi Cathy,

    If you are using AutoGeneratedEditform it will show TextBox for a GridBoundColumn while in edit/Insert mode. I am not sure what exactly is causing this issue. Can you check whether you have set the ReadOnly property of the Grid columns to true? If so try setting it to false since setting the readonly property will hide the GridColumn from appearing in the Edit/Insert form.

    Shinu
  13. Cathy
    Cathy avatar
    7 posts
    Member since:
    Apr 2009

    Posted 09 Apr 2009 Link to this post

    Thanks for getting back to me.

     

    I have tried explicitely setting the columns' ReadOnly property to false.  It had no effect.

    You write that "using AutoGeneratedEditform it will show TextBox for GridBoundColumn while in edit/insert mode.". 
    Should it also work for other columns types as well?  The columns that I programmatically add to the Radgrid are customized columns that extend the GridBoundColumn.  Specficially, they implement the google-like filtering funtions.

    thanks,
    cathy

     

     

     

     

  14. Cathy
    Cathy avatar
    7 posts
    Member since:
    Apr 2009

    Posted 09 Apr 2009 Link to this post

    Hi Shinu,

    Yes, it was indeed the setting for ReadOnly property of the Grid columns.   They were set else in the program and I didn't catch them.

    Thank you very much for your quick replies.

    I would still like to confirm that this should work for all columns types even the customized ones that inherits from other columns types?

    thanks again,
    cathy
  15. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Apr 2009 Link to this post

    Hi Cathy,

    As far as I know all the properties set for a Base class will be inherited by the Inherited class. Hence this should work for the inherited column as well.

    Thanks
    Shinu
  16. Cathy
    Cathy avatar
    7 posts
    Member since:
    Apr 2009

    Posted 15 Apr 2009 Link to this post

    Great!   Thank you very much for your quick response and help.  Much appreciate it.

    cathy
  17. Carl-Johan
    Carl-Johan avatar
    22 posts
    Member since:
    May 2008

    Posted 24 Mar 2010 Link to this post

    Hi,

    I'm using the same code. For me it doesn't work. I'm also using an ajax UpdatePanel. Can that combination be a problem?
  18. Milind Raje
    Milind Raje avatar
    37 posts
    Member since:
    May 2010

    Posted 07 Nov 2010 Link to this post

    Please disregard the following. I figured it out. (InsertCaption was what I was looking for)

    I am using the following code to edit my grid. The Caption Format String lets me add text to the edit form. How do I add similar text to when I am adding (inserting) new record? I mean which attributes need to be set for insert caption?
    Thanks
    milind

    <EditFormSettings ColumnNumber="2" CaptionDataField="service_name"  CaptionFormatString="Edit properties of Item {0}. Changes will be visible to public." FormCaptionStyle-Wrap="true">
                  <FormTableItemStyle Wrap="false"></FormTableItemStyle>
                    <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
                                       <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" BackColor="White"
                                           Width="100%" />
                                       <FormTableStyle CellSpacing="0" CellPadding="2" BackColor="ButtonFace" />
                                       <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
                                       <EditColumn ButtonType="ImageButton" InsertText="Insert Coupon" UpdateText="Update coupon"
                                           UniqueName="EditCommandColumn1" CancelText="Cancel edit">
                                       </EditColumn>
                                       <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
                                   </EditFormSettings>
  19. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 08 Nov 2010 Link to this post

    Hi Milind,

    Yes, the EditFormSettings -> InsertCaption property is what you are looking for. It allows you to specify default insert caption for the newly inserted record. CaptionFormatString is not meaningful for the insert caption.

    Best,
    Sebastian
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  20. Manav
    Manav avatar
    25 posts
    Member since:
    Oct 2010

    Posted 24 Dec 2010 Link to this post

    I have a similar question. i need to open the record i inserted in  radgrid immediately after the insert is fired. Any help in this would be great.
    we have different set of controls that open in add and edit mode. so i'd like to be able to open the same record in edit mode after the Insert command is fired.
  21. Marin
    Admin
    Marin avatar
    1044 posts

    Posted 29 Dec 2010 Link to this post

    Hello Mark,

    You can achieve this by putting again the relevant item in edit mode in the Page_PreRender event. In order to easily find the just inserted item you can keep a reference of some of its inserted values and then you can search for it by using them. Note that you also have to rebind the grid after the item is put in edit mode for the changes to take effect.

    Here is a sample code snippet illustrating the approach:

    string productName = "";
            bool isItemInserted = false;
            protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
            {
                isItemInserted = true;
                Hashtable ht = new Hashtable();
                e.Item.OwnerTableView.ExtractValuesFromItem(ht, e.Item);
                productName = ht["ProductName"].ToString();
      
            }
      
      
    protected void Page_PreRender(object sender, EventArgs e)
            {
                foreach (GridDataItem item in RadGrid1.Items)
                {
                    if (item["ProductName"].Text==productName && isItemInserted)
                    {
                        item.Edit = true;
                        isItemInserted = false;
                        productName = string.Empty;
                        RadGrid1.Rebind();
                    }
                }
                 
            }


    Kind regards,
    Marin
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017