Monthly Archives: May 2011

ASP.NET Dynamic Repeater in C#

public class RepeatingRuleTemplate : ITemplate {     ListItemType templateType; List innerControls;     public RepeatingRuleTemplate(ListItemType type, List<Control> controls)    {        templateType = type;        innerControls = controls;    }    public void InstantiateIn(Control container)    {        PlaceHolder ph = new PlaceHolder();        switch (templateType)        {            case ListItemType.Header:                ph.Controls.Add(new LiteralControl("<table border="0">"));                ph.Controls.Add(new LiteralControl("<tr>"));                foreach (Control control in innerControls)                {                    Label label = new Label();                    label.Text = control.ID;                    ph.Controls.Add(new LiteralControl("<td>"));                    ph.Controls.Add(label);                    ph.Controls.Add(new LiteralControl("</td>"));                }                ph.Controls.Add(new LiteralControl("</tr>"));                break;            case ListItemType.Item:                ph.Controls.Add(new LiteralControl("<tr>"));                foreach (Control control in innerControls)                {                        //ph.Controls.Add(new LiteralControl("<td>"));                        //ph.Controls.Add(control as TextBox);                        //ph.Controls.Add(new LiteralControl("</td>"));                    if (control.GetType() != typeof(Repeater))                    {                        ph.Controls.Add(new LiteralControl("<td>"));                        TextBox textBox = new TextBox();                        textBox.ID = control.ID;                        ph.Controls.Add(textBox);                        ph.Controls.Add(new LiteralControl("</td>"));                    }                    else                    {                        ph.Controls.Add(new LiteralControl("<td>"));                        Repeater rpt = new Repeater();                        rpt.DataSource = (control as Repeater).DataSource;                        rpt.ItemTemplate = (control as Repeater).ItemTemplate;                        rpt.HeaderTemplate = (control as Repeater).HeaderTemplate;                        rpt.FooterTemplate = (control as Repeater).FooterTemplate;                        rpt.DataBind();                        ph.Controls.Add(rpt);                        //(control as Repeater).DataSource = new DataRow[4];                        //   (control as Repeater).DataBind();                        ph.Controls.Add(new LiteralControl("</td>"));                    }                }                ph.Controls.Add(new LiteralControl("</tr>"));                //ph.DataBinding += new EventHandler(Item_DataBinding);                break;            case ListItemType.Footer:                ph.Controls.Add(new LiteralControl("</table>"));                break;        }        container.Controls.Add(ph);    }    public List<Control> Controls    {        get        {            return innerControls;        }    }}

Usage:

protected void Page_Load(object sender, EventArgs e){    CreateNestedRepeater();}private void CreateNestedRepeater(){    Repeater childRpt = new Repeater();    List repeatingRuleControls = new List();    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    RepeatingRuleTemplate repeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);    childRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);    childRpt.ItemTemplate = repeatingRuleTemplate;    childRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);    childRpt.DataSource = new DataRow[4];    Repeater parentRpt = new Repeater();    repeatingRuleControls = new List();    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(childRpt);    RepeatingRuleTemplate parentrepeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);    parentRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);    parentRpt.ItemTemplate = parentrepeatingRuleTemplate;    parentRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);    parentRpt.DataSource = new DataRow[4];    parentRpt.DataBind();    phControls.Controls.Add(parentRpt);[]}

Resources: Stack Overflow, msdn

ASP.NET Dynamic Repeater in VB.NET

Public Class SubCategoriesTemplate    Implements System.Web.UI.ITemplate    Dim templateType As ListItemType    Sub New(ByVal type As ListItemType)        templateType = type    End Sub    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _      Implements System.Web.UI.ITemplate.InstantiateIn        Dim plcGeneral As New PlaceHolder()        Dim plcItemPlaceholder As New PlaceHolder()        plcItemPlaceholder.ID = "plcCategory"        Dim plcHeaderPlaceholder As New PlaceHolder()        plcHeaderPlaceholder.ID = "plcCategoryHeader"        Dim plcFooterPlaceholder As New PlaceHolder()        plcFooterPlaceholder.ID = "plcCategoryFooter"        Select Case (templateType)            Case ListItemType.Header                plcGeneral.Controls.Add(plcHeaderPlaceholder)            Case ListItemType.Item                plcGeneral.Controls.Add(plcItemPlaceholder)            Case ListItemType.AlternatingItem            Case ListItemType.Footer                plcGeneral.Controls.Add(plcFooterPlaceholder)        End Select        container.Controls.Add(plcGeneral)    End SubEnd Class

Markup

    <ul class="secondary">        <asp:PlaceHolder ID="plcSecondaryCategories" runat="server" />    </ul

Usage

Dim rptSecondaryCategories As New RepeaterrptSecondaryCategories.ID = String.Format("rpt{0}", channelCategory.Name.Replace(" ", "-"))rptSecondaryCategories.HeaderTemplate = New SubCategoriesTemplate(ListItemType.Header)rptSecondaryCategories.ItemTemplate = New SubCategoriesTemplate(ListItemType.Item)rptSecondaryCategories.FooterTemplate = New SubCategoriesTemplate(ListItemType.Footer)plcSecondaryCategories.Controls.Add(rptSecondaryCategories)rptSecondaryCategories.DataSource = From category In SubCategories Where category.ParentCategory = channelCategory.Name Select categoryAddHandler rptSecondaryCategories.ItemDataBound, AddressOf rptSecondaryCategories_OnItemDataBoundrptSecondaryCategories.DataBind()

Simple OnITemDataBound usage

Protected Sub rptSecondaryCategories_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)    If e.Item.ItemType = ListItemType.Header Then        Dim plcCategoryHeader As PlaceHolder = CType(e.Item.FindControl("plcCategoryHeader"), PlaceHolder)        plcCategoryHeader.Controls.Add(New LiteralControl(String.Format("<ul class=""{0}{1}"">", CurrentCategoryParent.ToLower().Replace(" ", "-"), IIf(CurrentCategoryParent = SelectedCategoryParent, " active", String.Empty))))    ElseIf e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then        Dim channelCategory As ChannelCategory = CType(e.Item.DataItem, ChannelCategory)        Dim plcCategory As PlaceHolder = CType(e.Item.FindControl("plcCategory"), PlaceHolder)        Dim hlCategory As New HyperLink        Dim url As String = CurrentChannelURL.GetChannelCategoryUrl(channelCategory.Name, Request.Url.AbsoluteUri)        plcCategory.Controls.Add(New LiteralControl(String.Format("<li class=""{0}{1}"">", CurrentChannelURL.ParseCategoryName(channelCategory.Name, True).ToLower().Replace(" ", "-"), IIf(url.Replace("~", String.Empty) = CurrentChannelURL.CurrentChannelCategoryURL, " active", String.Empty))))        hlCategory.NavigateUrl = CurrentChannelURL.GetChannelCategoryUrl(channelCategory.Name, Request.Url.AbsoluteUri)        hlCategory.Text = channelCategory.Name        plcCategory.Controls.Add(hlCategory)        plcCategory.Controls.Add(New LiteralControl("</li>"))    ElseIf e.Item.ItemType = ListItemType.Footer Then        Dim plcCategoryFooter As PlaceHolder = CType(e.Item.FindControl("plcCategoryFooter"), PlaceHolder)        plcCategoryFooter.Controls.Add(New LiteralControl(" </ul>"))    End IfEnd Sub

 

ASP.NET/C# Read web.config settings method

internal static string GetSetting(string key, string defaultValue){    try    {        object setting = ConfigurationManager.AppSettings[key];        if (setting != null && setting.ToString().Length == 0)        {            setting = null;        }        return (setting == null) ? defaultValue : (string)setting;    }    catch    {        return defaultValue;    }}