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

[Solved] Dynamically created Radgrid, Collapse Group doesn't work

9 Answers 392 Views
Grid
This is a migrated thread and some comments may be shown as answers.
rob
Top achievements
Rank 1
rob asked on 03 Apr 2009, 10:30 PM
Using RadControls for ASPNET AJAX Q1 2009, I have created a dynamic grid as per your examples.  It works fine, including grouping, except that clicking "Collapse Group" doesn't do anything.

TIA
Rob

9 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 06 Apr 2009, 04:55 PM
Hello rob,

Can you please verify that you are using advanced binding with NeedDataSource handling for your grid? Additionally, make sure that you instantiate the grid instance programmatically in par with concepts from this documentation topic. This will ensure that the viewstate of the control/its event lifecycle will remain consistent.
 
Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
rob
Top achievements
Rank 1
answered on 06 Apr 2009, 08:21 PM
I've implemented NeedDataSource, but there is no change in behavior, and the new method doesn't get called in debug.
Here's my new code:

RadGrid1.NeedDataSource +=

new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);

 

 

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

 

{

GetGridData();

}

Am I missing something?
Thanks,
Rob

0
Sebastian
Telerik team
answered on 07 Apr 2009, 08:28 AM
Hello rob,

I suppose you attached the NeedDataSource handler in the PageInit handler of the page and your grid is instantiated as explained in the help topic linked previously - is that correct? If so, please post the code from the problematic page in this forum thread (using the 'Format Code Block' dialog at the top right corner of the forum editor toolbar). I will examine it in detail and will get around to you with more info on the subject.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
rob
Top achievements
Rank 1
answered on 07 Apr 2009, 11:18 PM
<%@ Page Language="C#" MasterPageFile="~/MSCOMWeb.master" AutoEventWireup="true" 
    EnableEventValidation="false" Inherits="MSCOMAdvertising.MyCampaigns" %> 
 
<%@ Register Assembly="Telerik.Web.UI, Version=2009.1.311.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4" 
    Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="contentMain" runat="Server">  
 
    <script type="text/javascript">  
        function newCampaign() {  
            window.location = "CampaignSubmission.aspx";  
        }  
 
        function DisplayPrivacyNotice() {  
            alert("You are about to export Microsoft Confidential Information. Please handle in accordance with the Legal Notice linked in the footer of each page of this application. Click 'OK' to continue.");  
        }  
    </script> 
 
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
    </telerik:RadScriptManager> 
    <asp:ObjectDataSource ID="ObjectDataSourceMyCampaigns" runat="server" OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetData" TypeName="DataAccess.MSComCampaignDSTableAdapters.MyCampaignsTableAdapter" 
        UpdateMethod="Update">  
        <SelectParameters> 
            <asp:Parameter Name="username" Type="String" /> 
            <asp:ControlParameter ControlID="ddlQuarters" Name="quarterID" PropertyName="SelectedValue" 
                Type="Int32" /> 
            <asp:ControlParameter ControlID="ddlCategories" Name="categoryID" PropertyName="SelectedValue"   
                Type="Int32"  /> 
        </SelectParameters> 
        <UpdateParameters> 
            <asp:Parameter Name="StatusID" Type="Int32" /> 
            <asp:Parameter Name="ModifiedBy" Type="String" /> 
            <asp:Parameter Name="Original_StatusID" Type="Int32" /> 
            <asp:Parameter Name="Original_CampaignID" Type="Int32" /> 
        </UpdateParameters> 
    </asp:ObjectDataSource> 
    <asp:ObjectDataSource ID="ObjectDataSourceQuestionColumns" runat="server" OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetData" TypeName="DataAccess.MSComAccessTableAdapters.QuestionColumnsTableAdapter">  
        <SelectParameters> 
            <asp:Parameter Name="QuarterID" Type="Int16" /> 
            <asp:Parameter Name="PageName" Type="String" DefaultValue="My Campaigns" /> 
            <asp:Parameter Name="GridType" Type="String" DefaultValue="W" /> 
        </SelectParameters> 
    </asp:ObjectDataSource> 
    <asp:ObjectDataSource ID="ObjectDataSourceQuarters" runat="server" SelectMethod="GetData" 
        TypeName="DataAccess.MSComAccessTableAdapters.QuartersTableAdapter" OldValuesParameterFormatString="original_{0}" 
        CacheDuration="Infinite" EnableCaching="False" SqlCacheDependency="CommandNotification">  
    </asp:ObjectDataSource> 
    <asp:ObjectDataSource ID="ObjectDataSourceCampaignCategory" runat="server" SelectMethod="GetData" 
        TypeName="DataAccess.MSComAccessTableAdapters.CampaignCategoriesTableAdapter" 
        OldValuesParameterFormatString="original_{0}" CacheDuration="Infinite" EnableCaching="False" 
        SqlCacheDependency="CommandNotification"></asp:ObjectDataSource> 
    <asp:ObjectDataSource ID="ObjectDataSourceCampaignAnswers" runat="server" SelectMethod="GetData" 
        SqlCacheDependency="CommandNotification" TypeName="DataAccess.MSComCampaignDSTableAdapters.CampaignAnswersTableAdapter" 
        OldValuesParameterFormatString="original_{0}">  
        <SelectParameters> 
            <asp:Parameter Name="CampaignID" Type="Int32" /> 
        </SelectParameters> 
    </asp:ObjectDataSource> 
    <br /> 
    <div id="divIntroductionContent" runat="server" class="introductionContent">  
        <asp:Label ID="lblIntroductionContent" runat="server" /> 
    </div> 
    <div class="bodyContent">  
        <table> 
            <tr> 
                <td align="right" class="Caption">  
                    Quarter:  
                </td> 
                <td align="left">  
                    <asp:DropDownList ID="ddlQuarters" runat="server" AutoPostBack="false" DataSourceID="ObjectDataSourceQuarters" 
                        DataTextField="Name" DataValueField="QuarterID" Width="200px">  
                    </asp:DropDownList> 
                </td> 
            </tr> 
            <tr> 
                <td align="right" class="Caption">  
                    Campaign Category:  
                </td> 
                <td align="left">  
                    <asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="false" DataSourceID="ObjectDataSourceCampaignCategory" 
                        DataTextField="Name" DataValueField="CampaignCategoryID" Width="200px" > 
                    </asp:DropDownList> 
                </td> 
            </tr> 
            <tr> 
                <td align="right">  
                </td> 
                <td align="left">  
                    <asp:Button ID="btnSearch" CssClass="btnStyle" runat="server" Text="Search" /> 
                </td> 
            </tr> 
            <tr> 
                <td align="right">  
                </td> 
                <td align="left">  
                    <br /> 
                </td> 
            </tr> 
            <tr> 
                <td colspan="2">  
                    <asp:Label ID="lblSystemMessage" runat="server" /> 
                </td> 
            </tr> 
        </table> 
        <div style="padding-bottom: 5px;">  
            <input id="Button1" type="button" class="btnStyle" runat="server" onclick="newCampaign();" 
                value="New Campaign" /> 
            <asp:Button ID="btnClearFilters" runat="server" CssClass="btnStyle" Font-Bold="true" 
                Text="Clear Filters" OnCommand="ClearFilters_Command" Width="120px" /> 
            <asp:Button ID="btnExcelExport" CssClass="btnStyle" runat="server" Text="Export to Excel" 
                OnClientClick="DisplayPrivacyNotice()" />&nbsp;  
        </div> 
        <div style="padding-bottom: 5px">  
        </div> 
        <table cellpadding="0" cellspacing="0">  
            <tr> 
                <td> 
                    <asp:PlaceHolder ID="PlaceHolder1" runat="server" /> 
                </td> 
                <td> 
                </td> 
            </tr> 
        </table> 
    </div> 
</asp:Content> 
 
 
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 System.Data.Sql;  
using System.Data.SqlClient;  
using Telerik.Web.UI;  
using MSCOMAdvertising.Common;  
 
namespace MSCOMAdvertising  
{  
    public partial class MyCampaigns : BaseUpdateableRadGridPage  
    {  
        protected RadGrid RadGrid1;  
        protected DataView dvQuestionColumns;  
        private bool staticOnly = false;  
        private Control postBackControl;  
 
        //Continuation stuff  
        private DataTable dtQuarters;  
        private int fromQuarterID = 0;  
        private int toQuarterID = 0;  
        private bool nextQuarterManageAllowed = false;  
 
        protected void Page_Init(object sender, System.EventArgs e)  
        {  
 
            postBackControl = CampaignGrid.GetPostBackControl(Page);  
            if (!string.IsNullOrEmpty(Request.QueryString["StaticOnly"]))  
            {  
                staticOnly = true;  
            }  
            //Note: RadGrid not a real control (RadGrid1 is the grid)  
            if (postBackControl == null || postBackControl.ID != "btnExcelExport")  
                DefineGridStructure();  
        }  
 
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                SetCommonPageContent(this, "MYCAMPAIGNS", "My Campaigns", null, lblIntroductionContent, divIntroductionContent);  
 
                this.ddlCategories.Items.Insert(0, new ListItem("All", "0"));  
            }  
            dtQuarters = GetCurrentAndNextQuarter();  
            DateTime dtFrom = Convert.ToDateTime(dtQuarters.Rows[0]["NextQuarterCallForCampaignsOpenDate"]);  
            DateTime dtTo = Convert.ToDateTime(dtQuarters.Rows[0]["NextQuarterEndDate"]);  
            if (DateTime.Now > dtFrom && DateTime.Now < dtTo)  
            {  
                fromQuarterID = Convert.ToInt32(dtQuarters.Rows[0]["QuarterID"]);  
                toQuarterID = Convert.ToInt32(dtQuarters.Rows[0]["NextQuarterID"]);  
                nextQuarterManageAllowed = Convert.ToBoolean(dtQuarters.Rows[0]["ManageAllowed"]);  
 
 
            }  
            if ((postBackControl == null || postBackControl.ID != "btnExcelExport") && ddlQuarters.SelectedIndex > 0)  
            {  
                GetGridData();  
            }  
        }  
 
        private void DefineGridStructure()  
        {  
            RadGrid1 = new RadGrid();  
            RadGrid1.ID = "RadGrid1";  
 
            // Event handlers  
            RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);  
            RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand2);  
            RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);  
            RadGrid1.ExcelExportCellFormatting += new OnExcelExportCellFormattingEventHandler(RadGrid1_ExcelExportCellFormatting);  
            RadGrid1.Init += new EventHandler(RadGrid1_Init);  
 
            // Set common grid properties and attributes  
            SetCommonGridProperties(ref RadGrid1, "DefaultCustom");  
 
            // Master Table View  
            //RadGrid1.MasterTableView.ExpandCollapseColumn.Resizable = false;  
            //RadGrid1.MasterTableView.ExpandCollapseColumn.Visible = false;  
            //RadGrid1.MasterTableView.ExpandCollapseColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(20);  
            //RadGrid1.MasterTableView.RowIndicatorColumn.Visible = false;  
            //RadGrid1.MasterTableView.RowIndicatorColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(20);  
            //RadGrid1.MasterTableView.ItemStyle.Wrap = true;  
 
            RadGrid1.GroupPanel.Visible = true;  
            //RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;  
 
            #region Column Definitions  
 
            GridTemplateColumn gtCol = new GridTemplateColumn();  
            string templateColumnName = "Manage";  
            gtCol.HeaderText = "Manage";  
            gtCol.UniqueName = "Manage";  
            gtCol.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(60);  
            gtCol.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(60);  
            gtCol.AllowFiltering = false;  
            gtCol.Groupable = false;  
            gtCol.Reorderable = false;  
            gtCol.HeaderStyle.Wrap = true;  
            gtCol.ItemStyle.Wrap = false;  
            gtCol.ItemTemplate = (System.Web.UI.ITemplate)new CampaignMenuTemplate(templateColumnName);  
            RadGrid1.MasterTableView.Columns.Add(gtCol);  
 
            GridBoundColumn col = new GridBoundColumn();  
            col.DataField = "IsSelect";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "CampaignID";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Name";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.FilterImageToolTip = "Filter by text contained in Campaign Name and press Enter";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            /*  
            col = new GridBoundColumn();  
            col.DataField = "Quarter";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.FilterImageToolTip = "Filter by Quarter and press Enter";  
            RadGrid1.MasterTableView.Columns.Add(col);  
*/  
            col = new GridBoundColumn();  
            col.DataField = "QuarterID";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.Visible = false;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "StartDate";  
            col.HeaderText = "Start Date";  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;  
            col.FilterImageToolTip = "Filter results by Start Date greater than or equal to the entered date";  
            col.DataFormatString = "{0:M/d/yyyy}";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "EndDate";  
            col.HeaderText = "End Date";  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;  
            col.FilterImageToolTip = "Filter results by End Date less than or equal to the entered date";  
            col.DataFormatString = "{0:M/d/yyyy}";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Status";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Status";  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.FilterImageToolTip = "Filter by Status text and press Enter";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Category";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Requested";  
            col.HeaderText = "Requested Category";  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
 
            col = new GridBoundColumn();  
            col.DataField = "CreatedOn";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "CreatedBy";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "ModifiedOn";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "ModifiedBy";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Continued";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.Visible = false;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            GridButtonColumn col2 = new GridButtonColumn();  
            col2.ButtonType = Telerik.Web.UI.GridButtonColumnType.LinkButton;  
            col2.Text = "Continue";  
            col2.CommandName = "Continue";  
            col2.UniqueName = "ContinueButton";  
            col2.ConfirmText = "You are about to continue this campaign to the next fiscal quarter. Click OK to continue.";  
            RadGrid1.MasterTableView.Columns.Add(col2);  
 
            #endregion  
 
            dvQuestionColumns = (DataView)ObjectDataSourceQuestionColumns.Select();  
            if (!staticOnly)  
            {  
                CampaignGrid.CreateDynamicColumns(RadGrid1, dvQuestionColumns);  
            }  
         //   const string CAMPAIGNID = "CampaignID";  
         //   Parameter pCampaignID = ObjectDataSourceCampaignAnswers.SelectParameters[CAMPAIGNID];  
 
            PlaceHolder1.Controls.Add(RadGrid1);  
        }  
 
 
        private void GetGridData()  
        {  
            const string USERNAME_PARAM = "Username";  
            Parameter usernameParam = ObjectDataSourceMyCampaigns.SelectParameters[USERNAME_PARAM];  
            if (usernameParam != null)  
            {  
                string[] curUsername = this.User.Identity.Name.ToLower().Split('\\');  
 
                usernameParam.DefaultValue = curUsername[1];  
            }  
            DataView dvMyCampaigns = (DataView)ObjectDataSourceMyCampaigns.Select();  
 
            DataTable dtValues = CampaignGrid.CreateTable(dvMyCampaigns);  
 
            dvQuestionColumns = (DataView)ObjectDataSourceQuestionColumns.Select();  
 
            for (int rowNum = 0; rowNum < dvQuestionColumns.Table.Rows.Count; rowNum++)  
            {  
                DataRow row = dvQuestionColumns.Table.Rows[rowNum];  
                dtValues.Columns.Add(row["QuestionName"].ToString());  
            }  
 
 
            const string CAMPAIGNID = "CampaignID";  
            Parameter pCampaignID = ObjectDataSourceCampaignAnswers.SelectParameters[CAMPAIGNID];  
 
            DataView dvAnswers;  
 
            for (int i = 0; i < dtValues.Rows.Count; i++)  
            {  
                int campaignID = Convert.ToInt32(dtValues.Rows[i]["CampaignID"]);  
                if (pCampaignID != null)  
                {  
                    pCampaignID.DefaultValue = campaignID.ToString();  
                }  
 
                dvAnswers = (DataView)ObjectDataSourceCampaignAnswers.Select();  
 
                dvQuestionColumns = (DataView)ObjectDataSourceQuestionColumns.Select();  
                if (!staticOnly)  
                    CampaignGrid.FillDynamicColumnsValues(dvQuestionColumns, dvAnswers, dtValues, dtValues.Rows[i]);  
            }  
 
            RadGrid1.DataSource = dtValues;  
            RadGrid1.Rebind();  
 
 
        }  
          
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            GetGridData();  
        }  
 
 
        public DataTable GetCurrentAndNextQuarter()  
        {  
            SqlCommand command = new SqlCommand();  
            command.CommandType = CommandType.StoredProcedure;  
            command.CommandText = "QuarterGetCurrentAndNext";  
            SqlConnection connection = new SqlConnection();  
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["DataAccess.Properties.Settings.MSComConnectionString"].ConnectionString;  
            command.Connection = connection;  
            connection.Open();  
 
 
            using (SqlDataReader myReader = command.ExecuteReader())  
            {  
                DataTable myTable = new DataTable();  
                myTable.Load(myReader);  
                connection.Close();  
                return myTable;  
            }  
 
 
        }  
 
 
 
 
 
 
       protected void RadGrid1_ItemCommand2(object source, GridCommandEventArgs e)  
        {  
            if (e.CommandName == "Continue")  
            {  
 
                Exception generationException;  
                int newCampaignID;  
 
                bool dataGenerated = ContinueCampaign(e, out generationException, out newCampaignID);  
                if (!dataGenerated)  
                {  
                    // Set negative system message  
                    lblSystemMessage.Text = "Generation could not be completed.";  
                    if (generationException != null)  
                        lblSystemMessage.Text += " Reason: " + generationException.Message;  
                    lblSystemMessage.CssClass = "systemMessageFailure";  
                }  
                else  
                {  
 
                    Response.Redirect("CampaignSubmission.aspx?cid=" + newCampaignID, true);  
 
                }  
            }  
             
             
        }  
 
        private bool ContinueCampaign(GridCommandEventArgs e, out Exception generationException, out int newCampaignID)  
        {  
            bool dataGenerated = false;  
            generationException = null;  
            newCampaignID = 0;  
 
            SqlCommand command = new SqlCommand();  
            SqlConnection connection = new SqlConnection();  
 
            try  
            {  
 
                connection.ConnectionString = ConfigurationManager.ConnectionStrings["DataAccess.Properties.Settings.MSComConnectionString"].ConnectionString;  
                command.CommandType = CommandType.StoredProcedure;  
                command.CommandText = "CampaignsClone";  
                command.Connection = connection;  
 
                int campaignID = Convert.ToInt32(RadGrid1.Items[e.Item.ItemIndex]["CampaignID"].Text.ToString());  
 
                command.Parameters.Add(base.CreateSimpleParameter("@FromCampaignID", SqlDbType.Int, campaignID));  
                command.Parameters.Add(base.CreateSimpleParameter("@FromQuarterID", SqlDbType.Int, fromQuarterID));  
                command.Parameters.Add(base.CreateSimpleParameter("@ToQuarterID", SqlDbType.Int, toQuarterID));  
                command.Parameters.Add(base.CreateSimpleParameter("@NextQuarterManageAllowed", SqlDbType.Bit, nextQuarterManageAllowed));  
                command.Parameters.Add(base.CreateSimpleParameter("@CreatedOn", SqlDbType.DateTime, DateTime.Now));  
                command.Parameters.Add(base.CreateSimpleParameter("@CreatedBy", SqlDbType.NVarChar, User.Identity.Name));  
 
                SqlParameter newnewCampaignParam = new SqlParameter();  
                newCampaignParam.ParameterName = "@NewCampaignID";  
                newCampaignParam.SqlDbType = SqlDbType.Int;  
                newCampaignParam.Direction = ParameterDirection.Output;  
                command.Parameters.Add(newCampaignParam);  
 
 
                connection.Open();  
                command.ExecuteNonQuery();  
 
                newCampaignID = Convert.ToInt32(newCampaignParam.Value);  
 
 
                dataGenerated = true;  
            }  
             catch (Exception ex)  
             {  
                 // Log exception  
                 Log.LogException(Log.Level.Error, ex.Message, "MYCAMPAIGNS", User.Identity.Name);  
 
 
                 generationException = ex;  
             }  
             finally  
             {  
                 connection.Close();  
             }  
 
            if (dataGenerated == true)  
            {  
                return true;  
             }  
            else  
            {  
                return false;  
            }   
        }  
 
        void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
            //No menu if Export  
            if (postBackControl == null || postBackControl.ID != "btnExcelExport")  
            {  
 
                if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)  
                {  
                    GridDataItem item = (GridDataItem)e.Item;  
 
                    // Append CampaignID to RadMenuGridItem items  
                    RadMenu radMenu = (RadMenu)item.FindControl("RadMenuGridItem");  
                    for (int i = 0; i <= radMenu.Items[0].Items.Count - 1; i++)  
                    {  
                        radMenu.Items[0].Items[i].NavigateUrl += "?cid=" + item["CampaignID"].Text;  
                    }  
 
                    CampaignGrid.SetMenuItems(radMenu, item["Category"].Text);  
 
 
 
 
                    GridTableCell cell = item["ContinueButton"] as GridTableCell;  
                    if (cell != null)  
                    {  
                        LinkButton btn = cell.Controls[0] as LinkButton;  
 
                        if (btn != null)  
                        {  
 
                            if (Convert.ToInt32(item["quarterID"].Text) == fromQuarterID && item["Status"].Text == "Active" && item["Continued"].Text != "True")  
                            {  
                                btn.Enabled = true;  
                            }  
                            else  
                            {  
                                btn.Enabled = false;  
                            }  
                        }  
 
 
                    }  
 
                }  
            }  
        }  
 
 
 
        /// <summary> 
        /// Call base method to clear filters.  
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void ClearFilters_Command(object sender, CommandEventArgs e)  
        {  
            base.ClearFilters(this.RadGrid1);  
        }  
 
        protected override void OnInit(EventArgs e)  
        {  
            this.ddlCategories.PreRender += new EventHandler(ddlCategories_PreRender);  
            this.ddlQuarters.PreRender += new EventHandler(ddlQuarters_PreRender);  
            this.btnExcelExport.Click += new EventHandler(btnExcelExport_Click);  
 
            base.OnInit(e);  
        }  
 
        protected void ddlCategories_PreRender(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                this.ddlCategories.Items.Insert(0, new ListItem("All", "0"));  
                ddlCategories.SelectedIndex = 0;  
 
            }  
        }  
 
        protected void ddlQuarters_PreRender(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                //Set default  
                int currentQuarter = base.GetCurrentQuarter();  
                this.ddlQuarters.SelectedValue = currentQuarter.ToString();  
                GetGridData();  
            }  
        }  
 
        /// <summary> 
        /// Export grid data to Excel.  
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        void btnExcelExport_Click(object sender, EventArgs e)  
        {  
            const string GridType = "GridType";  
            Parameter gridTypeParam = ObjectDataSourceQuestionColumns.SelectParameters[GridType];  
            if (gridTypeParam != null)  
            {  
                gridTypeParam.DefaultValue = "E";  
            }  
            DefineGridStructure();  
            GetGridData();  
            RadGrid1.DataBind();  
 
            RadGrid1.MasterTableView.Columns.FindByUniqueName("Manage").Visible = false;  
            RadGrid1.MasterTableView.Columns.FindByUniqueName("ContinueButton").Visible = false;  
            RadGrid1.MasterTableView.GroupByExpressions.Clear();  
            base.ExportGridToExcel(this.RadGrid1, "MyCampaignsReport");  
        }  
 
        protected void RadGrid1_ExcelExportCellFormatting(object source, Telerik.Web.UI.ExcelExportCellFormattingEventArgs e)  
        {  
            if (e.FormattedColumn.UniqueName.EndsWith("Date"))  
            {  
                e.Cell.Style["mso-number-format"] = @"m\/d\/yyyy";  
            }  
        }  
 
 
 
    }  
 
 
0
Sebastian
Telerik team
answered on 10 Apr 2009, 03:00 PM

Hello rob,

I reviewed your code and found that you invoke the Rebind() method of the grid from the GetGridData() method (called in the NeedDataSource handler). This is not a proper approach and cause infinite loop as explained under the Description section of the online demo.

Remove the Rebind() call from your code to see whether this helps.

Regards,

Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
rob
Top achievements
Rank 1
answered on 10 Apr 2009, 07:27 PM
Remove the Rebind() call results in the grid not being populated at all.
0
Sebastian
Telerik team
answered on 13 Apr 2009, 08:18 AM
Hello rob,

I examined once again your code and noticed the following additional discrepancies:

  1. You invoke the GetGridData() method from the PageLoad handler as well (inside a conditional block). This is not necessary because the NeedDataSource event will be raised after the PageLoad handler automatically, hence you need to remove this call.
  2. You call the same method from the PreRender handler of dllQuarters dropdown list on the page (on !Page.IsPostBack condition). If you would like to refresh the grid based on the value in the combobox on initial load, merely invoke grid's Rebind() method from the PreRender handler. 
  3. These lines should not be present in the Excel export button's OnClick handler:

    DefineGridStructure();     
    GetGridData();     
    RadGrid1.DataBind();    
     

    because the grid is rebound internally on export action and invoking the DefineGridStructure() method from postback event handler will conflict with the proper viewstate persistance of the control as explained in the help topic linked in my first post.
Regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
rob
Top achievements
Rank 1
answered on 13 Apr 2009, 04:58 PM

Hi Sebastian,

I've implemented the changes you recommended, but still the collapse group doesn't do anything.

I've inserted by updated code for you to examine.

Thanks again for your help,

Rob

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 System.Data.Sql;  
using System.Data.SqlClient;  
using Telerik.Web.UI;  
using MSCOMAdvertising.Common;  
 
namespace MSCOMAdvertising  
{  
    public partial class MyCampaigns : BaseUpdateableRadGridPage  
    {  
        protected RadGrid RadGrid1;  
        protected DataView dvQuestionColumns;  
        private bool staticOnly = false;  
        private Control postBackControl;  
 
        //Continuation stuff  
        private DataTable dtQuarters;  
        private int fromQuarterID = 0;  
        private int toQuarterID = 0;  
        private bool nextQuarterCallForCampaign = false;  
 
        protected void Page_Init(object sender, System.EventArgs e)  
        {  
 
            postBackControl = CampaignGrid.GetPostBackControl(Page);  
            if (!string.IsNullOrEmpty(Request.QueryString["StaticOnly"]))  
            {  
                staticOnly = true;  
            }  
            //Note: RadGrid not a real control (RadGrid1 is the grid)  
            if (postBackControl == null || postBackControl.ID != "btnExcelExport")  
                DefineGridStructure();  
        }  
 
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                SetCommonPageContent(this, "MYCAMPAIGNS", "My Campaigns", null, lblIntroductionContent, divIntroductionContent);  
 
                this.ddlCategories.Items.Insert(0, new ListItem("All", "0"));  
            }  
            dtQuarters = GetCurrentAndNextQuarter();  
            DateTime dtFrom = Convert.ToDateTime(dtQuarters.Rows[0]["NextQuarterCallForCampaignsOpenDate"]);  
            DateTime dtTo = Convert.ToDateTime(dtQuarters.Rows[0]["NextQuarterEndDate"]);  
            if (DateTime.Now > dtFrom && DateTime.Now < dtTo)  
            {  
                fromQuarterID = Convert.ToInt32(dtQuarters.Rows[0]["QuarterID"]);  
                toQuarterID = Convert.ToInt32(dtQuarters.Rows[0]["NextQuarterID"]);  
                nextQuarterCallForCampaign = Convert.ToBoolean(dtQuarters.Rows[0]["CallForCampaign"]);  
 
 
            }  
            if ((postBackControl == null || postBackControl.ID != "btnExcelExport"))  
            {  
               // GetGridData();  
            }  
        }  
 
        private void DefineGridStructure()  
        {  
            RadGrid1 = new RadGrid();  
            RadGrid1.ID = "RadGrid1";  
 
            // Event handlers  
            RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);  
            RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand2);  
            RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);  
            RadGrid1.ExcelExportCellFormatting += new OnExcelExportCellFormattingEventHandler(RadGrid1_ExcelExportCellFormatting);  
            RadGrid1.Init += new EventHandler(RadGrid1_Init);  
            RadGrid1.PreRender += new EventHandler(RadGrid1_PreRender);  
 
            // Set common grid properties and attributes  
            SetCommonGridProperties(ref RadGrid1, "DefaultCustom");  
 
            // Master Table View  
            //RadGrid1.MasterTableView.ExpandCollapseColumn.Resizable = false;  
            //RadGrid1.MasterTableView.ExpandCollapseColumn.Visible = false;  
            //RadGrid1.MasterTableView.ExpandCollapseColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(20);  
            //RadGrid1.MasterTableView.RowIndicatorColumn.Visible = false;  
            //RadGrid1.MasterTableView.RowIndicatorColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(20);  
            //RadGrid1.MasterTableView.ItemStyle.Wrap = true;  
 
            RadGrid1.GroupPanel.Visible = true;  
            //RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;  
 
            #region Column Definitions  
 
            GridTemplateColumn gtCol = new GridTemplateColumn();  
            string templateColumnName = "Manage";  
            gtCol.HeaderText = "Manage";  
            gtCol.UniqueName = "Manage";  
            gtCol.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(60);  
            gtCol.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(60);  
            gtCol.AllowFiltering = false;  
            gtCol.Groupable = false;  
            gtCol.Reorderable = false;  
            gtCol.HeaderStyle.Wrap = true;  
            gtCol.ItemStyle.Wrap = false;  
            gtCol.ItemTemplate = (System.Web.UI.ITemplate)new CampaignMenuTemplate(templateColumnName);  
            RadGrid1.MasterTableView.Columns.Add(gtCol);  
 
            GridBoundColumn col = new GridBoundColumn();  
            col.DataField = "IsSelect";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "CampaignID";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Name";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.FilterImageToolTip = "Filter by text contained in Campaign Name and press Enter";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            /*  
            col = new GridBoundColumn();  
            col.DataField = "Quarter";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.FilterImageToolTip = "Filter by Quarter and press Enter";  
            RadGrid1.MasterTableView.Columns.Add(col);  
*/  
            col = new GridBoundColumn();  
            col.DataField = "QuarterID";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.Visible = false;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "StartDate";  
            col.HeaderText = "Start Date";  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;  
            col.FilterImageToolTip = "Filter results by Start Date greater than or equal to the entered date";  
            col.DataFormatString = "{0:M/d/yyyy}";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "EndDate";  
            col.HeaderText = "End Date";  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;  
            col.FilterImageToolTip = "Filter results by End Date less than or equal to the entered date";  
            col.DataFormatString = "{0:M/d/yyyy}";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Status";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Status";  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.FilterImageToolTip = "Filter by Status text and press Enter";  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Category";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Requested";  
            col.HeaderText = "Requested Category";  
            colcol.UniqueName = col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
 
            col = new GridBoundColumn();  
            col.DataField = "CreatedOn";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "CreatedBy";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "ModifiedOn";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "ModifiedBy";  
            colcol.HeaderText = col.DataField;  
            col.UniqueName = "Export" + col.DataField;  
            col.ReadOnly = true;  
            colcol.SortExpression = col.DataField;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            col.Visible = false;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            col = new GridBoundColumn();  
            col.DataField = "Continued";  
            colcol.HeaderText = col.DataField;  
            colcol.UniqueName = col.DataField;  
            col.Visible = false;  
            colcol.SortExpression = col.UniqueName;  
            col.AutoPostBackOnFilter = true;  
            col.CurrentFilterFunction = GridKnownFunction.Contains;  
            RadGrid1.MasterTableView.Columns.Add(col);  
 
            GridButtonColumn col2 = new GridButtonColumn();  
            col2.ButtonType = Telerik.Web.UI.GridButtonColumnType.LinkButton;  
            col2.Text = "Continue";  
            col2.CommandName = "Continue";  
            col2.UniqueName = "ContinueButton";  
            col2.ConfirmText = "You are about to continue this campaign to the next fiscal quarter. Click OK to continue.";  
            RadGrid1.MasterTableView.Columns.Add(col2);  
 
            #endregion  
 
            dvQuestionColumns = (DataView)ObjectDataSourceQuestionColumns.Select();  
            if (!staticOnly)  
            {  
                CampaignGrid.CreateDynamicColumns(RadGrid1, dvQuestionColumns);  
            }  
            //const string CAMPAIGNID = "CampaignID";  
            //Parameter pCampaignID = ObjectDataSourceCampaignAnswers.SelectParameters[CAMPAIGNID];  
 
            PlaceHolder1.Controls.Add(RadGrid1);  
        }  
 
 
        private void GetGridData()  
        {  
 
            const string QUARTERID = "QuarterID";  
            Parameter pQuarterID = ObjectDataSourceQuestionColumns.SelectParameters[QUARTERID];  
            pQuarterID.DefaultValue = ddlQuarters.SelectedValue;  
 
            const string USERNAME_PARAM = "Username";  
            Parameter usernameParam = ObjectDataSourceMyCampaigns.SelectParameters[USERNAME_PARAM];  
            if (usernameParam != null)  
            {  
                string[] curUsername = this.User.Identity.Name.ToLower().Split('\\');  
 
                usernameParam.DefaultValue = curUsername[1];  
            }  
            DataView dvMyCampaigns = (DataView)ObjectDataSourceMyCampaigns.Select();  
 
            DataTable dtValues = CampaignGrid.CreateTable(dvMyCampaigns);  
 
            dvQuestionColumns = (DataView)ObjectDataSourceQuestionColumns.Select();  
 
            for (int rowNum = 0; rowNum < dvQuestionColumns.Table.Rows.Count; rowNum++)  
            {  
                DataRow row = dvQuestionColumns.Table.Rows[rowNum];  
                dtValues.Columns.Add(row["QuestionName"].ToString());  
            }  
 
 
            const string CAMPAIGNID = "CampaignID";  
            Parameter pCampaignID = ObjectDataSourceCampaignAnswers.SelectParameters[CAMPAIGNID];  
 
            DataView dvAnswers;  
 
            for (int i = 0; i < dtValues.Rows.Count; i++)  
            {  
                int campaignID = Convert.ToInt32(dtValues.Rows[i]["CampaignID"]);  
                if (pCampaignID != null)  
                {  
                    pCampaignID.DefaultValue = campaignID.ToString();  
                }  
 
                dvAnswers = (DataView)ObjectDataSourceCampaignAnswers.Select();  
 
                dvQuestionColumns = (DataView)ObjectDataSourceQuestionColumns.Select();  
                if (!staticOnly)  
                    CampaignGrid.FillDynamicColumnsValues(dvQuestionColumns, dvAnswers, dtValues, dtValues.Rows[i]);  
            }  
 
            RadGrid1.DataSource = dtValues;  
          //  RadGrid1.Rebind();  
 
 
        }  
          
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            GetGridData();  
        }  
 
 
        public DataTable GetCurrentAndNextQuarter()  
        {  
            SqlCommand command = new SqlCommand();  
            command.CommandType = CommandType.StoredProcedure;  
            command.CommandText = "QuarterGetCurrentAndNext";  
            SqlConnection connection = new SqlConnection();  
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["DataAccess.Properties.Settings.MSComConnectionString"].ConnectionString;  
            command.Connection = connection;  
            connection.Open();  
 
 
            using (SqlDataReader myReader = command.ExecuteReader())  
            {  
                DataTable myTable = new DataTable();  
                myTable.Load(myReader);  
                connection.Close();  
                return myTable;  
            }  
 
 
        }  
 
 
 
 
 
 
       protected void RadGrid1_ItemCommand2(object source, GridCommandEventArgs e)  
        {  
            if (e.CommandName == "Continue")  
            {  
 
                Exception generationException;  
                int newCampaignID;  
 
                bool dataGenerated = ContinueCampaign(e, out generationException, out newCampaignID);  
                if (!dataGenerated)  
                {  
                    // Set negative system message  
                    lblSystemMessage.Text = "Generation could not be completed.";  
                    if (generationException != null)  
                        lblSystemMessage.Text += " Reason: " + generationException.Message;  
                    lblSystemMessage.CssClass = "systemMessageFailure";  
                }  
                else  
                {  
 
                    Response.Redirect("CampaignSubmission.aspx?cid=" + newCampaignID, true);  
 
                }  
            }  
             
             
        }  
 
        private bool ContinueCampaign(GridCommandEventArgs e, out Exception generationException, out int newCampaignID)  
        {  
            bool dataGenerated = false;  
            generationException = null;  
            newCampaignID = 0;  
 
            SqlCommand command = new SqlCommand();  
            SqlConnection connection = new SqlConnection();  
 
            try  
            {  
 
                connection.ConnectionString = ConfigurationManager.ConnectionStrings["DataAccess.Properties.Settings.MSComConnectionString"].ConnectionString;  
                command.CommandType = CommandType.StoredProcedure;  
                command.CommandText = "CampaignsClone";  
                command.Connection = connection;  
 
                int campaignID = Convert.ToInt32(RadGrid1.Items[e.Item.ItemIndex]["CampaignID"].Text.ToString());  
 
                command.Parameters.Add(base.CreateSimpleParameter("@FromCampaignID", SqlDbType.Int, campaignID));  
                command.Parameters.Add(base.CreateSimpleParameter("@FromQuarterID", SqlDbType.Int, fromQuarterID));  
                command.Parameters.Add(base.CreateSimpleParameter("@ToQuarterID", SqlDbType.Int, toQuarterID));  
                command.Parameters.Add(base.CreateSimpleParameter("@NextQuarterCallForCampaign", SqlDbType.Bit, nextQuarterCallForCampaign));  
                command.Parameters.Add(base.CreateSimpleParameter("@CreatedOn", SqlDbType.DateTime, DateTime.Now));  
                command.Parameters.Add(base.CreateSimpleParameter("@CreatedBy", SqlDbType.NVarChar, User.Identity.Name));  
 
                SqlParameter newnewCampaignParam = new SqlParameter();  
                newCampaignParam.ParameterName = "@NewCampaignID";  
                newCampaignParam.SqlDbType = SqlDbType.Int;  
                newCampaignParam.Direction = ParameterDirection.Output;  
                command.Parameters.Add(newCampaignParam);  
 
 
                connection.Open();  
                command.ExecuteNonQuery();  
 
                newCampaignID = Convert.ToInt32(newCampaignParam.Value);  
 
 
                dataGenerated = true;  
            }  
             catch (Exception ex)  
             {  
                 // Log exception  
                 Log.LogException(Log.Level.Error, ex, "MYCAMPAIGNS", User.Identity.Name);  
 
 
                 generationException = ex;  
             }  
             finally  
             {  
                 connection.Close();  
             }  
 
            if (dataGenerated == true)  
            {  
                return true;  
             }  
            else  
            {  
                return false;  
            }   
        }  
 
        void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
            //No menu if Export  
            if (postBackControl == null || postBackControl.ID != "btnExcelExport")  
            {  
 
                if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)  
                {  
                    GridDataItem item = (GridDataItem)e.Item;  
 
                    // Append CampaignID to RadMenuGridItem items  
                    RadMenu radMenu = (RadMenu)item.FindControl("RadMenuGridItem");  
                    for (int i = 0; i <= radMenu.Items[0].Items.Count - 1; i++)  
                    {  
                        radMenu.Items[0].Items[i].NavigateUrl += "?cid=" + item["CampaignID"].Text;  
                    }  
 
                    CampaignGrid.SetMenuItems(radMenu, item["Category"].Text);  
 
 
 
 
                    GridTableCell cell = item["ContinueButton"] as GridTableCell;  
                    if (cell != null)  
                    {  
                        LinkButton btn = cell.Controls[0] as LinkButton;  
 
                        if (btn != null)  
                        {  
 
                            if (Convert.ToInt32(item["quarterID"].Text) == fromQuarterID && item["Status"].Text == "Active" && item["Continued"].Text != "True")  
                            {  
                                btn.Enabled = true;  
                            }  
                            else  
                            {  
                                btn.Enabled = false;  
                            }  
                        }  
 
 
                    }  
 
                }  
            }  
        }  
 
 
 
        /// <summary> 
        /// Call base method to clear filters.  
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void ClearFilters_Command(object sender, CommandEventArgs e)  
        {  
            base.ClearFilters(this.RadGrid1);  
        }  
 
        protected override void OnInit(EventArgs e)  
        {  
            this.ddlCategories.PreRender += new EventHandler(ddlCategories_PreRender);  
            this.ddlQuarters.PreRender += new EventHandler(ddlQuarters_PreRender);  
            this.btnExcelExport.Click += new EventHandler(btnExcelExport_Click);  
 
            base.OnInit(e);  
        }  
        protected void RadGrid1_PreRender(object sender, EventArgs e)  
        {  
            RadGrid1.Rebind();  
        }  
 
        protected void ddlCategories_PreRender(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                this.ddlCategories.Items.Insert(0, new ListItem("All", "0"));  
                ddlCategories.SelectedIndex = 0;  
 
            }  
        }  
 
        protected void ddlQuarters_PreRender(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                //Set default  
                int currentQuarter = base.GetCurrentQuarter();  
                this.ddlQuarters.SelectedValue = currentQuarter.ToString();  
                GetGridData();  
            }  
        }  
 
        /// <summary> 
        /// Export grid data to Excel.  
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        void btnExcelExport_Click(object sender, EventArgs e)  
        {  
            const string GridType = "GridType";  
            Parameter gridTypeParam = ObjectDataSourceQuestionColumns.SelectParameters[GridType];  
            if (gridTypeParam != null)  
            {  
                gridTypeParam.DefaultValue = "E";  
            }  
           // DefineGridStructure();  
           // GetGridData();  
           // RadGrid1.DataBind();  
 
            RadGrid1.MasterTableView.Columns.FindByUniqueName("Manage").Visible = false;  
            RadGrid1.MasterTableView.Columns.FindByUniqueName("ContinueButton").Visible = false;  
            RadGrid1.MasterTableView.GroupByExpressions.Clear();  
            base.ExportGridToExcel(this.RadGrid1, "MyCampaignsReport");  
        }  
 
        protected void RadGrid1_ExcelExportCellFormatting(object source, Telerik.Web.UI.ExcelExportCellFormattingEventArgs e)  
        {  
            if (e.FormattedColumn.UniqueName.EndsWith("Date"))  
            {  
                e.Cell.Style["mso-number-format"] = @"m\/d\/yyyy";  
            }  
        }  
 
 
 
    }  
 
 
0
Sebastian
Telerik team
answered on 16 Apr 2009, 11:17 AM
Hello rob,

It seems that now you invoke the Rebind() method each time from the PreRender handler of the grid. This is not appropriate and can be the cause for the failure in the expand/collapse functioality. I suggest you modify the event handlers below as follows:

       protected void RadGrid1_PreRender(object sender, EventArgs e)     
        {     
             //RadGrid1.Rebind();    
        }     
    
        protected void ddlCategories_PreRender(object sender, EventArgs e)     
        {     
            if (!IsPostBack)     
            {     
                this.ddlCategories.Items.Insert(0, new ListItem("All""0"));     
                ddlCategories.SelectedIndex = 0;     
    
            }     
        }     
    
        protected void ddlQuarters_PreRender(object sender, EventArgs e)     
        {     
            if (!IsPostBack)     
            {     
                //Set default     
                int currentQuarter = base.GetCurrentQuarter();     
                this.ddlQuarters.SelectedValue = currentQuarter.ToString();     
                RadGrid1.Rebind();      
            }     
        }     
 

Regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
rob
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
rob
Top achievements
Rank 1
Share this question
or