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

I am loading a control into a page that has a RadGrid. NeedDataSource is being called too soon!

1 Answer 36 Views
Grid
This is a migrated thread and some comments may be shown as answers.
matt
Top achievements
Rank 1
matt asked on 07 Oct 2010, 03:48 PM
I have code that loads the control that contains the radgird like this:

if (ControlToLoad == "ConCapitalDetailsPS")
           {
               Financial.ConCapitalDetails ucConCapitalDetails = Page.LoadControl("~/Grants/Phases/Financial/ConCapitalDetails.ascx") as Financial.ConCapitalDetails;
               ucConCapitalDetails.ID = "ConCapitalDetailsPS";
               PhaseContent.Controls.Clear();
               PhaseContent.Controls.Add(ucConCapitalDetails);
               ucConCapitalDetails.ActivePhase = ActivePhase;
               ucConCapitalDetails.BindData();
           }


But the problem is I need to load my data into the grid with

 

 

protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

 



So this is being called before I have the chance to load a specific data into the page

//int iGrantID = ActivePhase.GrantId;
          //int iPhaseID = ActivePhase.Id;

So what I need is somehow be able to set properties on that user control BEFORE the NeedDatasrouce so that the NeedDataSource has the parameters it needs.. How would I do that?  The cheating way is perhaps just make a session variable but that seems ugly.. here is the rest of the relevant code (The control being called )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Thc.Gms.Services;
using Thc.Gms.Core;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Transactions;
using System.Data.Common;
using Telerik.Web.UI;
  
namespace Thc.Gms.Web.UI.Grants.Phases.Financial
{
    public partial class HistoricExpenditures : System.Web.UI.UserControl
    {
        public Thc.Gms.Core.Phase ActivePhase
        {
            get { return ViewState["Phase"] as Thc.Gms.Core.Phase; }
            set
            {
                this.ViewState.Add("Phase", value);
            }
        }
  
        protected void Page_Load(object sender, EventArgs e)
        {
  
        }
  
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            Thc.Gms.Core.Phase ActivePhaseSession = (Phase)Session["ActivePhase"];
            int iGrantID = 267;
            int iPhaseID = 1517;
  
            //int iGrantID = ActivePhase.GrantId;
            //int iPhaseID = ActivePhase.Id;
  
            //GrantService gService = new GrantService();
            //Grant grant = gService.FindBy(ActivePhase.GrantId);
            //City.Text = grant.Address.City;
            //State.Text = grant.Address.State.Abbreviation;
            //GrantName.Text = grant.Name;
            //PhaseNameTitle.Text = ActivePhase.Name;
  
            string sql = "[dbo].[financial_getExpendituresByFtaskId]";
            Database _database = DatabaseFactory.CreateDatabase("GMSData");
            DataSet tempDataset = new DataSet();
            DataTable DetailsData = new DataTable("Details");
            DetailsData.Columns.Add("ftaskid");
            DetailsData.Columns.Add("level2", typeof(string));
            tempDataset.Tables.Add(DetailsData);
  
            using (TransactionScope scope = new TransactionScope())
            {
                using (DbCommand command = _database.GetStoredProcCommand(sql))
                {
                    _database.AddInParameter(command, "@grantid", DbType.Int32, iGrantID);
                    _database.AddInParameter(command, "@phaseid", DbType.Int32, iPhaseID);
                    _database.LoadDataSet(command, tempDataset, "Details");
                }
                scope.Complete();
            }
            RadGrid1.DataSource = tempDataset;
           
        }
  
        public void BindData()
        {
            //int iGrantID = ActivePhase.GrantId;
            //int iPhaseID = ActivePhase.Id;
  
            //GrantService gService = new GrantService();
            //Grant grant = gService.FindBy(ActivePhase.GrantId);
            //City.Text = grant.Address.City;
            //State.Text = grant.Address.State.Abbreviation;
            //GrantName.Text = grant.Name;
            //PhaseNameTitle.Text = ActivePhase.Name;
  
            //string sql = "[dbo].[financial_getExpendituresByFtaskId]";
            //Database _database = DatabaseFactory.CreateDatabase("GMSData");
            //DataSet tempDataset = new DataSet();
            //DataTable DetailsData = new DataTable("Details");
            //DetailsData.Columns.Add("ftaskid");
            //DetailsData.Columns.Add("level2", typeof(string));
            //tempDataset.Tables.Add(DetailsData);
  
            //using (TransactionScope scope = new TransactionScope())
            //{
            //    using (DbCommand command = _database.GetStoredProcCommand(sql))
            //    {
            //        _database.AddInParameter(command, "@grantid", DbType.Int32, iGrantID);
            //        _database.AddInParameter(command, "@phaseid", DbType.Int32, iPhaseID);
            //        _database.LoadDataSet(command, tempDataset, "Details");
            //    }
            //    scope.Complete();
            //}
            //RadGrid1.DataSource = tempDataset;
            //RadGrid1.DataBind();
        }
  
  
        protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {
            int iGrantID = ActivePhase.GrantId;
            int iPhaseID = ActivePhase.Id;
            int iUserID = SessionHelper.CurrentUser.Id;
  
            GridEditableItem editedItem = e.Item as GridEditableItem;
            int iFTaskID = Int32.Parse(editedItem.GetDataKeyValue("ftaskid").ToString());
            int iQtr = Int32.Parse((editedItem["Qtr"].Controls[0] as TextBox).Text);
            int iYear = Int32.Parse((editedItem["Year"].Controls[0] as TextBox).Text);
            decimal dHP6 = decimal.Parse((editedItem["H6_Exp"].Controls[0] as TextBox).Text);
            decimal dPH6 = decimal.Parse((editedItem["PH_Exp"].Controls[0] as TextBox).Text);
            decimal dOth6 = decimal.Parse((editedItem["Oth_Exp"].Controls[0] as TextBox).Text);
            decimal dFF6 = decimal.Parse((editedItem["FF_Exp"].Controls[0] as TextBox).Text);
  
            Dictionary<string, decimal> ExpColl = new Dictionary<string, decimal>();
            ExpColl.Add("H6", dHP6);
            ExpColl.Add("PH", dPH6);
            ExpColl.Add("Oth", dOth6);
            ExpColl.Add("FF", dFF6);
  
            foreach (KeyValuePair<string, decimal> item in ExpColl)
            {
                string sql = "[dbo].[financial_setExpendituresByYearQtr]";
                Database _database = DatabaseFactory.CreateDatabase("GMSData");
  
                using (TransactionScope scope = new TransactionScope())
                {
                    using (DbCommand command = _database.GetStoredProcCommand(sql))
                    {
                        _database.AddInParameter(command, "@grantid", DbType.Int32, iGrantID);
                        _database.AddInParameter(command, "@phaseid", DbType.Int32, iPhaseID);
                        _database.AddInParameter(command, "@ftaskId", DbType.Int32, iFTaskID);
                        _database.AddInParameter(command, "@amount", DbType.Decimal, item.Value);
                        _database.AddInParameter(command, "@fundtype ", DbType.String, item.Key);
                        _database.AddInParameter(command, "@year", DbType.Int32, iYear);
                        _database.AddInParameter(command, "@qtr", DbType.Int32, iQtr);
                        _database.AddInParameter(command, "@userid", DbType.Int32, iUserID);
                        _database.ExecuteNonQuery(command);
                    }
                    scope.Complete();
                }
            }
        }
  
        protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            int iGrantID = ActivePhase.GrantId;
            int iPhaseID = ActivePhase.Id;
            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
            int iCategoryID = Int32.Parse(dataItem.GetDataKeyValue("FTaskID").ToString());
  
            string sql = "[dbo].[financial_getExpendituresOVerTime]";
            Database _database = DatabaseFactory.CreateDatabase("GMSData");
            DataSet tempDataset = new DataSet();
            DataTable DetailsData = new DataTable("Details");
            DetailsData.Columns.Add("ftaskid");
            DetailsData.Columns.Add("qtr", typeof(int));
            DetailsData.Columns.Add("year", typeof(int));
            DetailsData.Columns.Add("H6_Exp", typeof(decimal));
            DetailsData.Columns.Add("H6_Use", typeof(decimal));
            DetailsData.Columns.Add("PH_Exp", typeof(decimal));
            DetailsData.Columns.Add("PH_Use", typeof(decimal));
            DetailsData.Columns.Add("Oth_Exp", typeof(decimal));
            DetailsData.Columns.Add("Oth_Use", typeof(decimal));
            DetailsData.Columns.Add("FF_Exp", typeof(decimal));
            DetailsData.Columns.Add("FF_Use", typeof(decimal));
  
  
            tempDataset.Tables.Add(DetailsData);
  
            using (TransactionScope scope = new TransactionScope())
            {
                using (DbCommand command = _database.GetStoredProcCommand(sql))
                {
                    _database.AddInParameter(command, "@grantid", DbType.Int32, iGrantID);
                    _database.AddInParameter(command, "@phaseid", DbType.Int32, iPhaseID);
                    _database.AddInParameter(command, "@ftaskId", DbType.Int32, iCategoryID);
                    _database.LoadDataSet(command, tempDataset, "Details");
                }
                scope.Complete();
            }
  
            RadGrid1.DataSource = tempDataset;     
          //  RadGrid1.DataBind();
        }
  
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "GridDetails")
            {
                GridEditFormItem editItem = (GridEditFormItem)e.Item;
                if (false)
                {
                    TextBox txt = (TextBox)editItem.FindControl("unH6_Exp");
                    txt.ReadOnly = true;
                }
            }
        }
  
        
  
    }
}


1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 12 Oct 2010, 11:33 AM
Hello Matt,

Please, note that when you load controls dynamically, it is recommended to do that in the Page_Load event handler. For further information, you can read this help article which deals exactly with dynamically loading usercontrols which contain RadGrid.


Sincerely yours,
Tsvetina
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
matt
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or