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

RadGrid in Repeater (disappearing on postback)

4 Answers 209 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 19 Jul 2010, 02:20 PM
Hi,

I have used a RadGrid inside a repeater, but having issues getting values out of it. Initially I realised I was rebinding on every postback, oops, but now changed that to only bind on initial load but when I click edit etc on the grid that particular GridView disappears.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Config.ascx.cs" Inherits="Admin_Config" ClassName="Admin.Config" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<h1>Config</h1>
<script type="text/javascript">
    function RowDblClick(sender, eventArgs) {
        sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
    }
</script>
<asp:Repeater ID="rptGroups" OnItemDataBound="rptGroups_ItemDataBound" runat="server">
    <ItemTemplate>
        <h2><asp:Literal ID="litHeader" runat="server" /></h2>
        <asp:UpdatePanel ID="upKeys" UpdateMode="Conditional" runat="server">
            <ContentTemplate>
                <asp:Literal ID="litTest" runat="server" />
                <telerik:RadGrid ID="rgKeys" AutoGenerateColumns="false" AllowSorting="True" AutoGenerateEditColumn="true" AllowAutomaticUpdates="True" Skin="Telerik"
                    OnUpdateCommand="rgKeys_UpdateCommand" runat="server">
                    <MasterTableView EditMode="PopUp" CommandItemDisplay="Bottom" CommandItemSettings-AddNewRecordText="Add new key"
                        CommandItemSettings-ShowRefreshButton="false" DataKeyNames="name" AllowSorting="true">
                        <Columns>
                            <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Alias" HeaderText="Alias" SortExpression="Alias" UniqueName="Alias"></telerik:GridBoundColumn>
                            <telerik:GridCheckBoxColumn DataField="Active" HeaderText="Active" SortExpression="Active" UniqueName="Active"></telerik:GridCheckBoxColumn>
                            <telerik:GridBoundColumn DataField="Value1" HeaderText="Value 1" SortExpression="Value1" UniqueName="Value1"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Value2" HeaderText="Value 2" SortExpression="Value2" UniqueName="Value2"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Value3" HeaderText="Value 3" SortExpression="Value3" UniqueName="Value3"></telerik:GridBoundColumn>
                        </Columns>
                        <EditItemStyle ForeColor="Gray"></EditItemStyle>
                        <EditFormSettings InsertCaption="Add new key" CaptionFormatString="Edit details key '{0}'" CaptionDataField="name" PopUpSettings-Modal="true">
                            <FormCaptionStyle Font-Bold="true" />   
                        </EditFormSettings>     
                    </MasterTableView>
                    <ClientSettings>
                        <ClientEvents OnRowDblClick="RowDblClick" />
                    </ClientSettings>
                </telerik:RadGrid>
            </ContentTemplate>
        </asp:UpdatePanel>
    </ItemTemplate>
</asp:Repeater>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using Telerik.Web.UI;
 
public partial class Admin_Config : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            XDocument Config = Cache.XDoc.Load("Config.xml");
            rptGroups.DataSource = Config.Element("config").Elements("group").Where(x => x.Attribute("hidden").Value == "false");
            rptGroups.DataBind();
        }
    }
    protected void rptGroups_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        XElement ConfigGroup = (XElement)e.Item.DataItem;
        ((Literal)e.Item.FindControl("litHeader")).Text = ConfigGroup.Attribute("alias").Value;
 
        var Keys = from x in ConfigGroup.Elements("key")
                   where x.Attribute("hidden").Value == "false"
                   orderby Convert.ToInt32(x.Attribute("sortId").Value) ascending
                   select new
                        {
                            Name = x.Attribute("name").Value,
                            Alias = x.Attribute("alias").Value,
                            Description = x.Attribute("description").Value,
                            Active = x.Attribute("active").Value,
                            SortId = x.Attribute("sortId").Value,
                            Value1 = x.Attribute("value1").Value,
                            Value2 = x.Attribute("value2").Value,
                            Value3 = x.Attribute("value3").Value
                        };
         
        ((RadGrid)e.Item.FindControl("rgKeys")).DataSource = Keys;
        ((RadGrid)e.Item.FindControl("rgKeys")).DataBind();
    }
    protected void rgKeys_UpdateCommand(object source, GridCommandEventArgs e)
    {
        Hashtable NewValues = new Hashtable();
        e.Item.OwnerTableView.ExtractValuesFromItem(NewValues, (GridEditableItem)e.Item);
 
        // Cant get any values out here...
    }
}

4 Answers, 1 is accepted

Sort by
0
Carl
Top achievements
Rank 1
answered on 20 Jul 2010, 07:59 PM
Any joy with this? I'm sure it must be something obvious and simple I'm missing lol
0
Iana Tsolova
Telerik team
answered on 22 Jul 2010, 09:34 AM
Hi Carl,

Indeed, the described behavior is rather expected because you are using simple data-binding (you can find more information on it here). However I suggest that instead of binding the grid in the Repeater ItemDataBound event, handle the RadGrid NeedDataSource event. There the sernder is the particular grid and its NamingContainer is the Repeater item.

Try it out and let me know if it works for you.

All the best,
Iana
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
0
Carl
Top achievements
Rank 1
answered on 31 Jul 2010, 05:09 PM
Hi,

Thanks for your assistance, that helped for sure. The problem I have now however is I cannot get the RadGrid's nested in the repeater to work with Ajax.

Ideally I want each individual RadGrid within the Repeater to be in its own UpdatePanel/AjaxPanel so that the Ajax postbacks are efficient and not reloading many seperate RadGrid's.

I have tried many different options including the use of:

  • UpdatePanels around each RadGrid within each RepeaterItem
  • RadAjaxManager within each RepeaterItem
  • RadAjaxManager outside of Repeater, and RadAjaxManagerProxy within each RepeaterItem
  • RadAjaxPanels around each RadGrid within each RepeaterItem

But all that happens is it stops the edit/insert popup from appearing, you click and nothing happens. However if I remove all the Ajax stuff it works again.

Here is the code as it as at the moment:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Config.ascx.cs" Inherits="Admin_Config" ClassName="Admin.Config" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<h1>Config</h1>
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server"></telerik:RadScriptManager>
 
<asp:Repeater ID="rptGroups" OnItemDataBound="rptGroups_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:HiddenField ID="hfGroupName" runat="server" />
        <h2><asp:Literal ID="litHeader" runat="server" /></h2>
 
        <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
             
            <asp:Literal ID="litTest" runat="server" />
                <telerik:RadGrid ID="rgKeys" AutoGenerateColumns="false" AllowSorting="True" AutoGenerateEditColumn="true" AllowAutomaticUpdates="True" Skin="Telerik"
                    OnNeedDataSource="rgKeys_NeedDataSource" runat="server">
                    <MasterTableView EditMode="PopUp" CommandItemDisplay="Bottom" CommandItemSettings-AddNewRecordText="Add new key"
                        CommandItemSettings-ShowRefreshButton="false" AllowSorting="true">
                        <Columns>
                            <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Alias" HeaderText="Alias" SortExpression="Alias" UniqueName="Alias"></telerik:GridBoundColumn>
                            <telerik:GridCheckBoxColumn DataField="Active" HeaderText="Active" SortExpression="Active" UniqueName="Active"></telerik:GridCheckBoxColumn>
                            <telerik:GridBoundColumn DataField="Value1" HeaderText="Value 1" SortExpression="Value1" UniqueName="Value1"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Value2" HeaderText="Value 2" SortExpression="Value2" UniqueName="Value2"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Value3" HeaderText="Value 3" SortExpression="Value3" UniqueName="Value3"></telerik:GridBoundColumn>
                        </Columns>
                        <EditFormSettings InsertCaption="Add new key" CaptionFormatString="Edit key" EditFormType="Template" PopUpSettings-Modal="true" FormCaptionStyle-Font-Bold="true">
                            <FormTemplate>
 
                                <asp:ValidationSummary ID="valSmry" CssClass="valSmry" EnableClientScript="false" HeaderText="Error validating key" runat="server" />
                                <asp:RequiredFieldValidator ID="valName" EnableClientScript="false" ErrorMessage="Please enter a Name" Display="None" ControlToValidate="txtName" runat="server" />
 
                                <table cellspacing="5" cellpadding="0" border="0">
                                    <tr>
                                        <td>name:</td>
                                        <td><asp:TextBox ID="txtName" runat="server" /></td>
                                    </tr>
                                    <tr>
                                        <td>Description:</td>
                                        <td><asp:TextBox ID="txtDescription" TextMode="MultiLine" Rows="3" runat="server" /></td>
                                    </tr>
                                    <tr>
                                        <td>Active:</td>
                                        <td><asp:CheckBox ID="chkActive" runat="server" /></td>
                                    </tr>
                                    <tr>
                                        <td>Value 1:</td>
                                        <td><asp:TextBox ID="txtValue1" runat="server" /></td>
                                    </tr>
                                    <tr>
                                        <td>Value 2:</td>
                                        <td><asp:TextBox ID="txtValue2" runat="server" /></td>
                                    </tr>
                                    <tr>
                                        <td>Value 3:</td>
                                        <td><asp:TextBox ID="txtValue3" runat="server" /></td>
                                    </tr>
                                    <tr>
                                        <td> </td>
                                        <td>
                                            <asp:Button ID="btnAdd" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' runat="server" />
                                            <asp:Button ID="btnCancel" Text="Cancel" CausesValidation="False" CommandName="Cancel" runat="server" />
                                        </td>
                                    </tr>                       
                                </table>
                            </FormTemplate>
                        </EditFormSettings>
                    </MasterTableView>
                </telerik:RadGrid>
 
        </telerik:RadAjaxPanel>   
    </ItemTemplate>
</asp:Repeater>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using Telerik.Web.UI;
 
public partial class Admin_Config : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            XDocument Config = Cache.XDoc.Load("Config.xml");
            rptGroups.DataSource = Config.Element("config").Elements("group").Where(x => x.Attribute("hidden").Value == "false");
            rptGroups.DataBind();
        }
    }
    protected void rptGroups_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            XElement ConfigGroup = (XElement)e.Item.DataItem;
            ((HiddenField)e.Item.FindControl("hfGroupName")).Value = ConfigGroup.Attribute("name").Value;
        }
    }
    protected void rgKeys_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid rgKeys = (RadGrid)source;
        RepeaterItem ConfigGroupItem = (RepeaterItem)((RadGrid)source).Parent.Parent;
        XElement Group = Cache.XDoc.Load("Config.xml").Element("config").Elements("group").Where(x => x.Attribute("name").Value == ((HiddenField)ConfigGroupItem.FindControl("hfGroupName")).Value).SingleOrDefault();
 
        rgKeys.DataSource = from x in Group.Elements("key")
                            select new
                            {
                                Name = x.Attribute("name").Value,
                                Alias = x.Attribute("alias").Value,
                                Description = x.Attribute("description").Value,
                                Active = x.Attribute("active").Value,
                                SortId = x.Attribute("sortId").Value,
                                Value1 = x.Attribute("value1").Value,
                                Value2 = x.Attribute("value2").Value,
                                Value3 = x.Attribute("value3").Value
                            };
    }
}

Can you help cure this issue at all?

Thanks, Carl


0
Pavlina
Telerik team
answered on 04 Aug 2010, 07:27 PM
Hello Carl,

Please check this online example for more information about how to achieve your goal:
http://www.telerik.com/DEMOS/ASPNET/Prometheus/Ajax/Examples/Manager/PartialAjaxification/DefaultCS.aspx

You can use similar approach with repeater, etc.

Regards,
Pavlina
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
Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Carl
Top achievements
Rank 1
Iana Tsolova
Telerik team
Pavlina
Telerik team
Share this question
or