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

ItemDataBound customizations erased on postback in the radgrid

2 Answers 236 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arek
Top achievements
Rank 1
Arek asked on 06 May 2011, 02:58 PM
Hello,

I have a problem with all my customizations in the RadGrid1_ItemDataBound to the Total and Subtotal rows as well as to LinkButton.
The custom data in the columns in the rows (like custom calculations) are erased/removed when I for instance collapse a group or click on the LinkButton.

So any time there is a postback in the grid the customizations are erased....

Should I also initialize the customizations on some other event or am I missing something?

Please advise.

Thanks,
-Arek

the code below:
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
     
    <div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1"
            GridLines="None" EnableAJAX="true" EnableAJAXLoadingTemplate="true" LoadingTemplateTransparency="25"
            ShowGroupPanel="True" AllowPaging="True" AllowSorting="True"
            Skin="Vista" OnItemDataBound="RadGrid1_ItemDataBound" ShowFooter="true"
            OnItemCreated="RadGrid1_ItemCreated" >
            <ClientSettings AllowDragToGroup="True">
            </ClientSettings>
            <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1" ShowGroupFooter="true"
                AllowMultiColumnSorting="true">
                 
                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridBoundColumn DataField="Market" FilterControlAltText="Filter Market column"
                        HeaderText="Market" SortExpression="Market" UniqueName="Market">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Station" FilterControlAltText="Filter Station column"
                        HeaderText="Station" SortExpression="Station" UniqueName="Station">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Spots" DataType="System.Int32" FilterControlAltText="Filter Spots column"
                        HeaderText="Spots" ReadOnly="True" SortExpression="Spots" UniqueName="Spots"
                        Aggregate="Sum" FooterText=" ">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Spend" DataType="System.Decimal" FilterControlAltText="Filter Spend column"
                        HeaderText="Spend" SortExpression="Spend" UniqueName="Spend" Aggregate="Sum"
                        FooterText=" " DataFormatString="{0:F2}">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CALL" DataType="System.Int32" FilterControlAltText="Filter CALL column"
                        HeaderText="CALL" ReadOnly="True" SortExpression="CALL" UniqueName="CALL" Aggregate="Sum"
                        FooterText=" ">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ORDR" DataType="System.Int32" FilterControlAltText="Filter ORDR column"
                        HeaderText="ORDR" ReadOnly="True" SortExpression="ORDR" UniqueName="ORDR" Aggregate="Sum"
                        FooterText=" ">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CPC" DataType="System.Decimal" FilterControlAltText="Filter CPC column"
                        HeaderText="CPC" ReadOnly="True" SortExpression="CPC" UniqueName="CPC" DataFormatString="{0:F2}">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CPM" DataType="System.Decimal" FilterControlAltText="Filter CPM column"
                        HeaderText="CPM" ReadOnly="True" SortExpression="CPM" UniqueName="CPM" DataFormatString="{0:F2}">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CPO" DataType="System.Decimal" FilterControlAltText="Filter CPO column"
                        HeaderText="CPO" ReadOnly="True" SortExpression="CPO" UniqueName="CPO" DataFormatString="{0:F2}">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CTO" DataType="System.Double" FilterControlAltText="Filter CTO column"
                        HeaderText="CTO" ReadOnly="True" SortExpression="CTO" UniqueName="CTO" DataFormatString="{0:F2}">
                    </telerik:GridBoundColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
                <GroupByExpressions>
                    <telerik:GridGroupByExpression>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="Market" />
                        </GroupByFields>
                        <SelectFields>
                            <telerik:GridGroupByField FieldName="Market" HeaderText="Market" />
                        </SelectFields>
                    </telerik:GridGroupByExpression>
                    <telerik:GridGroupByExpression>
                        <SelectFields>
                            <telerik:GridGroupByField FieldName="Station" FieldAlias="Station" FormatString=""
                                HeaderText="Station"></telerik:GridGroupByField>
                        </SelectFields>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="Station" FieldAlias="Station" FormatString=""
                                HeaderText=""></telerik:GridGroupByField>
                        </GroupByFields>
                    </telerik:GridGroupByExpression>
                </GroupByExpressions>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
            </HeaderContextMenu>
        </telerik:RadGrid>
    </ContentTemplate>
    </asp:UpdatePanel>
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                if (e.Item is GridFooterItem)
                {
                    GridFooterItem footer = (GridFooterItem)e.Item;
                    footer["Market"].Controls.Add(new LiteralControl("<span>Grand Total :</span>"));
                    footer["Market"].Style.Add("Text-align", "right");
 
                    float cpc = Convert.ToSingle(footer["Spend"].Text)/Convert.ToSingle(footer["CALL"].Text);
                    string txtCpc = string.Format("<span>{0:F2}</span>", cpc);
                    footer["CPC"].Controls.Add(new LiteralControl(txtCpc));
 
                    float cpm = Convert.ToSingle(footer["CALL"].Text) / (Convert.ToSingle(footer["Spend"].Text) / 1000.00F);
                    string txtCpm = string.Format("<span>{0:F2}</span>", cpm);
                    footer["CPM"].Controls.Add(new LiteralControl(txtCpm));
 
                    float cpo = Convert.ToSingle(footer["Spend"].Text) / Convert.ToSingle(footer["ORDR"].Text);
                    string txtCpo = string.Format("<span>{0:F2}</span>", cpo);
                    footer["CPO"].Controls.Add(new LiteralControl(txtCpo));
 
                    float cto = Convert.ToSingle(footer["ORDR"].Text) / Convert.ToSingle(footer["CALL"].Text);
                    string txtCto = string.Format("<span>{0:F2}</span>", cto);
                    footer["CTO"].Controls.Add(new LiteralControl(txtCto));
                }
                else if (e.Item is GridGroupFooterItem)
                {
                    GridGroupFooterItem groupFooter = (GridGroupFooterItem)e.Item;
                    groupFooter["Market"].Controls.Add(new LiteralControl("<span>SubTotal :</span>"));
                    groupFooter["Market"].Style.Add("Text-align", "right");
 
 
                    float cpc = Convert.ToSingle(groupFooter["Spend"].Text) / Convert.ToSingle(groupFooter["CALL"].Text);
                    string txtCpc = string.Format("<span>{0:F2}</span>", cpc);
                    groupFooter["CPC"].Controls.Add(new LiteralControl(txtCpc));
 
                    float cpm = Convert.ToSingle(groupFooter["CALL"].Text) / (Convert.ToSingle(groupFooter["Spend"].Text) / 1000.00F);
                    string txtCpm = string.Format("<span>{0:F2}</span>", cpm);
                    groupFooter["CPM"].Controls.Add(new LiteralControl(txtCpm));
 
                    float cpo = Convert.ToSingle(groupFooter["Spend"].Text) / Convert.ToSingle(groupFooter["ORDR"].Text);
                    string txtCpo = string.Format("<span>{0:F2}</span>", cpo);
                    groupFooter["CPO"].Controls.Add(new LiteralControl(txtCpo));
 
                    float cto = Convert.ToSingle(groupFooter["ORDR"].Text) / Convert.ToSingle(groupFooter["CALL"].Text);
                    string txtCto = string.Format("<span>{0:F2}</span>", cto);
                    groupFooter["CTO"].Controls.Add(new LiteralControl(txtCto));
                }
                //else if (e.Item is GridDataItem2)
                //{
                //    GridDataItem item = (GridDataItem)e.Item;
                //    HyperLink hyplink = new HyperLink();
                //    hyplink.ID = "HyperLink1";
                //    hyplink.Text = item["Station"].Text;
                //    hyplink.NavigateUrl = "#";
                //    item["Station"].Controls.Add(hyplink);
                //}
                else if (e.Item is GridDataItem)
                {
                    GridDataItem item = (GridDataItem)e.Item;
                    LinkButton lnkButton = new LinkButton();
                    lnkButton.ID = "LinkButton1";
                    lnkButton.Text = item["Station"].Text;
                    lnkButton.Click += lnkButton_Click;
                    item["Station"].Controls.Add(lnkButton);
                }
 
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(base.GetType(), "Alert", ex.Message.ToString());
            }
        }

2 Answers, 1 is accepted

Sort by
0
Arek
Top achievements
Rank 1
answered on 09 May 2011, 03:47 PM
I was able to fix most of the problems by moving the code from ItemDataBound event to RadGrid1_PreRender.
The one thing I'm investigating now is how to use LinkButton in the RadGrid so that I would have server side event happen.

Thanks,
-Arek
0
Tsvetina
Telerik team
answered on 11 May 2011, 12:48 PM
Hi Arek,

You could try adding the link button in the ItemCreated event of RadGrid. However, it would be better if you use a GridButtonColumn or a GridTemplate column to display it. If you want it to be initially invisible, you can just switch the visibility of the column that displays it in the RadGrid PreRender event.

Best wishes,
Tsvetina
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.

Tags
Grid
Asked by
Arek
Top achievements
Rank 1
Answers by
Arek
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or