E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Shuffler Server Control
Source code by Ryan Trudelle-Schwarz ryants@aspelite.com>
idea by Charles Carroll

Server Controls can totally control how the information enclosed within them is rendered to the browser. They also can be deployed by merely placing a .dll file in a /bin directory. No registration process needed.

The DLL for this is at /experiments/bin/randomcontrols.dll. Just download and insert in your C:\inetpub\wwwroot or relevant vroot and sample will start working.

The samples that use this control are at /freebook/learn/shuffler.aspx.

This is source code for Iprioritizable object:

filename=/experiments/shuffler/source/iprioritizable.cs

namespace Mamanze.RandomControls
{
  public interface IPrioritizable
  {
    int Priority
    {
      get;
      set;
    }
  }
}

This is source code for ListItem object:

filename=/experiments/shuffler/source/listitem.cs

namespace Mamanze.RandomControls
{
  using System;
  using System.Web;
  using System.Web.UI;
  
  public class ListItem : Control, IPrioritizable
  {
    int intPriority = 1;

    public int Priority
    {
      get
      {
        return intPriority;
      }
      set
      {
        intPriority = value;
      }
    }

    protected override void Render(HtmlTextWriter wrtOutput)
    {
      wrtOutput.RenderBeginTag("div");
      for(int i = 0; i < Controls.Count; i++)
      {
          Controls[i].RenderControl(wrtOutput);
      }
      wrtOutput.RenderEndTag();
    }
  }

}

This is source code for ListSection object:

filename=/experiments/shuffler/source/listsection.cs

namespace Mamanze.RandomControls
{
  using System;
  using System.Collections;
  using System.Web.UI;
  using Mamanze.RandomControls;
 
  [
    ParseChildren(true)
  ]
  public class ListSection : RandomWeighted, IPrioritizable
  {
    private string strText = "";
    private string strTitle = "";
    private int intPriority = 1;

    // To satisfy the IPrioritized interface, we must implement the 
    // Priority property.

    public int Priority
    {
      get
      {
        return intPriority;
      }
      set
      {
        intPriority = value;
      }
    }

    // Property to hold the limit on the number of sub elements to display.
    public int Limit
    {
      get
      {
        return intLimit;
      }
      set
      {
        if(value > 0)
          intLimit = value;
      }
    }
    
    // Property to hold the title of this element.
    public string Title
    {
      get
      {
        return strTitle;
      }
      set
      {
        strTitle = value;
      }
    }

    // Allows this element to contain ListItems
    public ListItem LI
    {
      get
      {
        ListItem x = new ListItem();
        Controls.Add(x);
        return x;
      }
    }


    // ALlows this element to contain ListSections.
    public ListSection LS
    {
      get
      {
        ListSection x = new ListSection();
        Controls.Add(x);
        return x;
      }
    }


    // Renders the element.
    override protected void Render(HtmlTextWriter wrtOutput)
    {
      // Begin the rendering of this parent element.
      wrtOutput.AddStyleAttribute("border","solid 1px black;");
      wrtOutput.AddStyleAttribute("padding","2px;");
      wrtOutput.RenderBeginTag("div");
      wrtOutput.AddAttribute("class","title");
      wrtOutput.RenderBeginTag("span");
      wrtOutput.RenderBeginTag("b");
      wrtOutput.Write(Title);
      wrtOutput.RenderEndTag();
      wrtOutput.RenderEndTag();
      wrtOutput.Write("<br />");

      // Render the child controls.
      base.Render(wrtOutput);

      // Finish rendering the parent element.
      wrtOutput.RenderEndTag();
      
    }

  }

}

This is source code for RandomList object:

filename=/experiments/shuffler/source/RandomList.cs

namespace Mamanze.RandomControls
{
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  using Mamanze.RandomControls;
  
  [ ParseChildren(true) ]
  public class RandomList : RandomWeighted
  {

    public ListSection LS
    {
      get
      {
        ListSection x = new ListSection ();
        Controls.Add(x);
        return x;
      }
    }

    public ListItem LI
    {
      get
      {
        ListItem x = new ListItem();
        Controls.Add(x);
        return x;
      }
    }

    public int Limit
    {
      set
      {
        intLimit = value;
      }
    }

    override protected void OnInit(EventArgs e)
    {
      base.OnInit(e);
    }

  }


}

This is source code for RandomWeighted object:

filename=/experiments/shuffler/source/RandomWeighted.cs

namespace Mamanze.RandomControls
{
  using System;
  using System.Collections;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using Mamanze.RandomControls;

  public abstract class RandomWeighted : WebControl
  {

    protected int intLimit = 0;

    override protected void Render(HtmlTextWriter wrtOutput)
    {
      if(Controls.Count > 0)
      {
        // Generic prioritized variable for temp storage.
        IPrioritizable priTemp;

        // Point in the controls list to stop rendering.
        int intStop = 0;

        // If the user specified a limit then use it, otherwise default to 0.
        if(intLimit != 0)
        {
          if(intLimit >= Controls.Count)
          {
            intStop = 0;
          }
          else
          {
            intStop = Controls.Count - intLimit;
          }
        }

        // Used to sort the weight values.
        SortedList lstControls = new SortedList(Controls.Count);

        // Random number generator.
        Random rndGenerator = new Random();
        
        // Variable to hold the random numbers.
        int intRandom = 0;
        
        // Limit the random numbers allowed. This will help limit the vollitility of the randomizer.
        int intMaxRandom = Controls.Count * 2;

        // Variable to hold the value of the element we've selected to display.
        int intValue = 0;

        // Fill the weight/value list.
        for(int i = 0; i < Controls.Count; i++)
        {
          // Get a reference to the control.
          priTemp = (IPrioritizable)Controls[i];
          
          // Loop while the weight/value has already been used.
          do
          {
            intRandom = rndGenerator.Next(intMaxRandom);
          }while(lstControls.ContainsKey(priTemp.Priority * intRandom));

          // Add the weight/value to the list.
          lstControls.Add((priTemp.Priority * intRandom),i);
        }
       
        // Loop through and render the controls.
        for(int i = (lstControls.Count - 1); i >= intStop; i--)
        {
          Controls[(int)lstControls.GetByIndex(i)].RenderControl(wrtOutput);
        }
      }
    }
  }
}
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.