Dynamic Resources in codebehind

10 posts, 0 answers
  1. Dasha
    Dasha avatar
    80 posts
    Member since:
    Aug 2010

    Posted 25 Aug 2010 Link to this post

    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.
  2. Vuyiswa Maseko
    Vuyiswa Maseko avatar
    187 posts
    Member since:
    Jan 2010

    Posted 25 Aug 2010 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dasha
    Dasha avatar
    80 posts
    Member since:
    Aug 2010

    Posted 25 Aug 2010 Link to this post

    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>
  5. SSirica
    SSirica avatar
    50 posts
    Member since:
    Jan 2007

    Posted 25 Aug 2010 Link to this post

    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.
  6. Dasha
    Dasha avatar
    80 posts
    Member since:
    Aug 2010

    Posted 25 Aug 2010 Link to this post

    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...
  7. Peter
    Admin
    Peter avatar
    6637 posts

    Posted 27 Aug 2010 Link to this post


    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
  8. Thomas
    Thomas avatar
    6 posts
    Member since:
    Sep 2010

    Posted 29 Jan 2011 Link to this post

    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
  9. David
    David avatar
    3 posts
    Member since:
    Jun 2013

    Posted 11 Jun 2013 Link to this post

    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
  10. Plamen
    Admin
    Plamen avatar
    2733 posts

    Posted 14 Jun 2013 Link to this post

    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.
  11. bablov
    bablov avatar
    1 posts
    Member since:
    Dec 2014

    Posted 02 Apr 2015 in reply to Dasha Link to this post

    Maybe you should add this line of code:
      RadScheduler1.GroupBy = "";
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017