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

Dynamic Resources in codebehind

9 Answers 661 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Dasha
Top achievements
Rank 1
Dasha asked on 25 Aug 2010, 07:48 AM
Hi, I just realized that I posted a question last night into the wrong location:

http://www.telerik.com/community/forums/wpf/scheduler/dynamic-resources-in-codebehind.aspx

For some reason I'm having a hard time applying ResourceTypes and ResourceStyles to the RadScheduler dynamically. I'm sure I'm missing something small, and yet I can't catch it.  Please take a look at my code and maybe you can point me in the right direction. 

protected void Page_Load(object sender, EventArgs e) 
   if (!IsPostBack) 
   
       objDS = GetDataSet(); 
       SetupResources(); 
       RadScheduler.DataSource = objDS.Tables[0]; 
       RadScheduler.DataBind(); 
   
    
private void SetupResources() 
  ResourceType resType = new ResourceType();
  resType.ForeignKeyField = "AssignedTo"
  resType.DataSource = objDS.Tables["table2"]; 
  resType.KeyField = "id"
  resType.Name = "User"
  resType.TextField = "UserName"
    
  RadScheduler.ResourceTypes.Add(resType); 
    
  ResourceStyleMapping rsm = new ResourceStyleMapping(); 
  rsm.ApplyCssClass = "rsCategoryPink"
  rsm.Text = "Dasha"
  rsm.Type = "User"
  RadScheduler.ResourceStyles.Add(rsm); 
    
  rsm = new ResourceStyleMapping(); 
  rsm.ApplyCssClass = "rsCategoryGreen"
  rsm.Text = "Joe"
  rsm.Type = "User"
  RadScheduler.ResourceStyles.Add(rsm); 
    
  rsm = new ResourceStyleMapping(); 
  rsm.ApplyCssClass = "rsCategoryOrange"
  rsm.Text = "Adam"
  rsm.Type = "User"
  RadScheduler.ResourceStyles.Add(rsm); 
}

This is the xml data that's converted into a dataset:


<?
xml version="1.0" encoding="utf-8"?>   
<dataSet>   
  <table1>   
    <id>1</id>   
    <Title>Task1</Title>   
    <Date>08/07/2010</Date>   
    <End>08/17/2010</End>   
    <AssignedTo>Dasha</AssignedTo>   
    <Status>Completed</Status>   
    <Room>Library</Room>   
  </table1>   
  <table1>   
    <id>2</id>   
    <Title>Task 2</Title>   
    <Date></Date>   
    <End>08/07/2010</End>   
    <AssignedTo>Dasha</AssignedTo>   
    <Status>Completed</Status>   
    <Room>Office</Room>   
  </table1>   
  <table1>   
    <id>3</id>   
    <Title>Task 3</Title>   
    <Date>08/18/2010</Date>   
    <End>08/18/2010</End>   
    <AssignedTo>Adam</AssignedTo>   
    <Status>Completed</Status>   
    <Room>Library</Room>   
  </table1>   
  <table1>   
    <id>4</id>   
    <Title>Task 4</Title>   
    <Date></Date>   
    <End>08/08/2010</End>   
    <AssignedTo>Joe</AssignedTo>   
    <Status>Completed</Status>   
    <Room>Library</Room>   
  </table1>    
  <table2>   
    <id>1</id>   
    <UserName>Dasha</UserName>   
    <UserAge>28</UserAge>   
    <UserOccupation>Plumber</UserOccupation>   
  </table2>   
  <table2>   
    <id>2</id>   
    <UserName>Joe</UserName>   
    <UserAge>30</UserAge>   
    <UserOccupation>IT Professional</UserOccupation>   
  </table2>   
  <table2>   
    <id>3</id>   
    <UserName>Adam</UserName>   
    <UserAge>33</UserAge>   
    <UserOccupation>Manager</UserOccupation>   
  </table2>   
  <table2>   
    <id>4</id>   
    <UserName>Michael</UserName>   
    <UserAge>30</UserAge>   
    <UserOccupation>Developer</UserOccupation>   
  </table2>   
</dataSet

When the sheduler is done databinding, I can step into the code and see that the Resources collection of the RadScheduler has been populated, but if I step into the AppointmentDataBound event, none of the appointments show any resources assigned, even though the AssignedTo column is definitely populated in the datatable.  I'm also open to populating the Resouces collection manually, as long as it's done programmatically.  I've looked at probably all your demo samples, but somehow still missed the solution.  Thank you so much!

Dasha.

9 Answers, 1 is accepted

Sort by
0
Vuyiswa
Top achievements
Rank 2
answered on 25 Aug 2010, 08:15 AM
Why do you have

SetupResources();

I see you are returning a Dataset , if you are returning a Dataset its enough, remove that function  and if you want to change the colors of the appointments based on condition do it like this


protected void RadScheduler1_AppointmentDataBound(object sender, Telerik.Web.UI.SchedulerEventArgs e)
{
   if (e.Appointment.Resources.GetResourceByType("User") != null)
   {
       switch (e.Appointment.Resources.GetResourceByType("User").Text)
       {
           case "Alex":
               e.Appointment.BackColor = Color.Blue;
               break;
           case "Bob":
               e.Appointment.Back = Color.Orange;
               break;
           case "Charlie":
               e.Appointment.CssClass = "rsCategoryGreen";
               break;
           default:
               break;
       }       
   }      
}
as done in the Documentation

http://www.telerik.com/help/aspnet-ajax/appearance-setting-appointment-style.html

Hope this Helps
0
Dasha
Top achievements
Rank 1
answered on 25 Aug 2010, 08:33 AM
I have
SetupResources();
so that i can setup the resource types, otherwise how would the scheduler know what are the resource groups?  Also, the resource types and the resource styles are defined dynamically by user settings, my example was a simplified version of what's happening.  This is why I was trying to keep all the resource definition in one place.  Also, what would be the overhead of looking at each appointment individually if you have thousands of items?  This is what the actualy code should look like in ideal:

private void SetupResources()
{
    try
    {
        DataSet objCS = null;
        if (wp.ResourcesSetup != "")
            objCS = new Helper().GetDataSet(wp.ResourcesSetup);
        else
            return;
        if (objCS != null && objCS.Tables.Count >= 1)
        {
            using (DataTable objDT = objCS.Tables[0])
            {
                string defType = "";
                string tableName = "";
                ResourceType rt = new ResourceType();
                ResourceStyleMapping rsm = new ResourceStyleMapping();
                foreach (DataRow row in objDT.Rows)
                {
                    if (objDT.Columns.Contains("ResourceDefinitionType"))
                    {
                        try
                        {
                            defType = row["ResourceDefinitionType"].ToString();
                            tableName = "";
                            switch (defType)
                            {
                                case "ResourceType":
                                    rt = new ResourceType();
                                    if (objDT.Columns.Contains("AllowMultipleValues")) { try { rt.AllowMultipleValues = bool.Parse(row["AllowMultipleValues"].ToString()); } catch { } }
                                    if (objDT.Columns.Contains("ForeignKeyField")) { try { rt.ForeignKeyField = row["ForeignKeyField"].ToString(); } catch { } }
                                    if (objDT.Columns.Contains("KeyField")) { try { rt.KeyField = row["KeyField"].ToString(); } catch { } }
                                    if (objDT.Columns.Contains("Name")) { try { rt.Name = row["Name"].ToString(); } catch { } }
                                    if (objDT.Columns.Contains("TextField")) { try { rt.TextField = row["TextField"].ToString(); } catch { } }
                                    if (objDT.Columns.Contains("DataSource")) { try { tableName = row["DataSource"].ToString(); } catch { } }
                                    if (objDS.Tables.Contains(tableName) && !RadSheduler.ResourceTypes.Contains(rt))
                                    {
                                        RadSheduler.ResourceTypes.Add(rt);
                                        rt.DataSource = objDS.Tables[tableName];
                                    }
                                    break;
                                case "ResourceStyleMapping":
                                    rsm = new ResourceStyleMapping();
                                    if (objDT.Columns.Contains("ApplyCssClass")) { try { rsm.ApplyCssClass = row["ApplyCssClass"].ToString(); } catch { } }
                                    if (objDT.Columns.Contains("Text")) { try { rsm.Text = row["Text"].ToString(); } catch { } }
                                    if (objDT.Columns.Contains("Type")) { try { rsm.Type = row["Type"].ToString(); } catch { } }
                                    RadSheduler.ResourceStyles.Add(rsm);
                                    break;
                                default: break;
                            }
                        }
                        catch { }
                    }
                }
            }
            objCS.Dispose();
        }
    }
    catch (Exception error) { }
}

and the xml would look like this:

<resources>
    <table1>
      <ResourceDefinitionType>ResourceType</ResourceDefinitionType>
      <AllowMultipleValues>true</AllowMultipleValues>
      <ForeignKeyField>AssignedTo</ForeignKeyField>
      <KeyField>id</KeyField>
      <Name>User</Name>
      <TextField>UserName</TextField>
      <DataSource>table2</DataSource>
    </table1>
    <table1>
      <ResourceDefinitionType>ResourceType</ResourceDefinitionType>
      <AllowMultipleValues>true</AllowMultipleValues>
      <ForeignKeyField>Room</ForeignKeyField>
      <KeyField>id</KeyField>
      <Name>Location</Name>
      <TextField>RoomName</TextField>
      <DataSource>table3</DataSource>
    </table1>
    <table1>
      <ResourceDefinitionType>ResourceStyleMapping</ResourceDefinitionType>
      <ApplyCssClass>rsCategoryRed</ApplyCssClass>
      <Text>Dasha</Text>
      <Type>User</Type>
      <Key>1</Key>
    </table1>
    <table1>
      <ResourceDefinitionType>ResourceStyleMapping</ResourceDefinitionType>
      <ApplyCssClass>rsCategoryBlue</ApplyCssClass>
      <Text>Joe</Text>
      <Type>User</Type>
      <Key>2</Key>
    </table1>
    <table1>
      <ResourceDefinitionType>ResourceStyleMapping</ResourceDefinitionType>
      <ApplyCssClass>rsCategoryGreen</ApplyCssClass>
      <Text>Adam</Text>
      <Key>3</Key>
      <Type>User</Type>
    </table1>
  </resources>
0
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
answered on 25 Aug 2010, 06:00 PM
I have a similar issue, in that I'm trying to add a dynamic resource and the resource drop down on the advanced form has nothing in it.  As long as you keep things simple this control is great.  When you start doing non-vanilla type things this control is not easy to customize.
0
Dasha
Top achievements
Rank 1
answered on 25 Aug 2010, 08:41 PM
I hear ya.  I just don't understand why the appointments don't get any resources assigned to them at databinding.  I must be calling the methods in the wrong place, it's probably my timing that's wrong.  But the scheduler does populate the resources from the DataSource properties that are assigned to the ResourceTypes, and the ResourceTypes and ResourceStyles collections are all populated, just the data won't connect! grr...
0
Peter
Telerik team
answered on 27 Aug 2010, 04:05 PM

Hello,

I have attached a simple demo for your perusal. I hope you find it helpful.


Kind regards,
Peter
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
Thomas
Top achievements
Rank 1
answered on 29 Jan 2011, 09:06 PM
I had the same problem. I tried to attach the resources as they were being populated into a list before binding to the scheduler. Which worked but by the time they got to the ondatabound event they were gone. The only solution that worked for me was to save my dataset in a session variable and attach the resources during the ondatbound event. Once this was accomplished everything works great.

Private Sub LoadResources(ByVal apt As Appointment)
        Dim EventTypeValue As Integer
        Dim Employee As Integer = Nothing
        Dim _dv As DataView = New DataView(CType(Me.Session("FUData"), DataTable))
        _dv.RowFilter = "ID = " & CStr(apt.ID)
        If _dv.Count = 1 Then
            Dim _row As DataRow = _dv.ToTable.Rows(0)
            EventTypeValue = CInt(_row.Item("EventType"))
            Employee = CInt(_row.Item("Employee"))
        End If
        Dim EventType As Resource = apt.Owner.Resources.GetResource("EventType", EventTypeValue)
        apt.Resources.Add(EventType)
  
        EventType = apt.Owner.Resources.GetResource("Employee", Employee)
        apt.Resources.Add(EventType)
    End Sub
0
David
Top achievements
Rank 1
answered on 11 Jun 2013, 03:48 PM
Thank you Peter!

I was able to add and populate my resource without fail.

Now, if I could only find out how to add my own controls (ie a Checkbox) via the code behind that render properly.

I have added checkboxes in the FormCreate procedure but they end up squished.

Any other posts about this that you could direct me to?

Thanks!

Dave
0
Plamen
Telerik team
answered on 14 Jun 2013, 01:42 PM
Hi Dave,

 
In scenarios when you want to add custom controls inside the Advanced Form we recommend using CUstom AdvancedTemplate as in this on-line demo for example.

Regards,
Plamen
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
bablov
Top achievements
Rank 1
answered on 02 Apr 2015, 06:21 AM
Maybe you should add this line of code:
  RadScheduler1.GroupBy = "";
Tags
Scheduler
Asked by
Dasha
Top achievements
Rank 1
Answers by
Vuyiswa
Top achievements
Rank 2
Dasha
Top achievements
Rank 1
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
Peter
Telerik team
Thomas
Top achievements
Rank 1
David
Top achievements
Rank 1
Plamen
Telerik team
bablov
Top achievements
Rank 1
Share this question
or