[Mono-list] ASP.NET CheckBoxList Control

John Anderson sontek at gmail.com
Tue Apr 17 17:50:27 EDT 2007


Hey, I'm trying to implement my own CheckBoxList control based off of
mono's.  But for some reason my LoadPostData method doesn't get fired, can
anyone see anything wrong?

using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web.UI.WebControls;
using System.Web.UI;
using System;
using System.Text;
using System.IO;
namespace Orchid.Web.Controls
{
    /// <summary> A checkboxlist control that uses ajax callbacks for
sorting</summary>
    public class SortableCheckBoxList : ListControl, INamingContainer,
IPostBackDataHandler
    {
        private CheckBox checkBox;
        public SortableCheckBoxList()
        {
            checkBox = new CheckBox();
            Controls.Add(checkBox);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            for (int count = 0; count < Items.Count; count++)
            {
                if (Items[count].Selected)
                {
                    checkBox.ID = String.Format("chk_item_{0}",
count.ToString());
                    Page.RegisterRequiresPostBack(checkBox);
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string sortId = String.Format("{0}[]", ID);
            if (Page.Request.Form[sortId] != null && !Page.IsPostBack)
            {
                string[] sortedItems = Page.Request.Form[sortId].Split(',');
                SortedItems = new int[sortedItems.Length];
                for (int count = 0; count < sortedItems.Length; count++)
                    SortedItems[count] = int.Parse(sortedItems[count]);
            }
        }
        public int[] SortedItems
        {
            get { return (int[])Page.Session[this.ID + "SortedItems"]; }
            set { Page.Session[this.ID + "SortedItems"] = value; }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (Items == null || Items.Count == 0)
                return;
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ID);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);

            if (SortedItems == null)
            {
                for (int itemIndex = 0; itemIndex < Items.Count;
itemIndex++)
                {
                    RenderListItem(itemIndex, writer);
                }
            }
            else
            {
                foreach (int itemIndex in SortedItems)
                {
                    RenderListItem(itemIndex, writer);
                }
            }
            writer.RenderEndTag();
            StringBuilder sb = new StringBuilder();

            string ajaxCallBack = String.Format("function() {{ new
Ajax.Request('{0}?ajaxAction={2}{3}',{{asynchronous:true, parameters:
Sortable.serialize('{1}')}})}}",
                                                 Path.GetFileName(
Page.Request.PhysicalPath), ID, new Random().Next(), GetQueryString());
            sb.AppendLine(String.Format("Sortable.create('{0}',
{{onUpdate:{1}, asynchronous:true, dropOnEmpty:true,constraint:
false,overlap:'horizonal'}});",
                                        ID, ajaxCallBack));
            Page.ClientScript.RegisterStartupScript(this.Page.GetType(),
"draggable", sb.ToString(), true);
        }
        private string GetQueryString()
        {
            int count = Page.Request.QueryString.Count;
            if (count > 0)
            {
                StringBuilder queryString = new StringBuilder();
                for (int index = 0; index < count; index++)
                {
                    queryString.AppendFormat("&{0}={1}",
Page.Request.QueryString.Keys[index], Page.Request.QueryString[index]);
                }
                return queryString.ToString();
            }
            return string.Empty;
        }
        private void RenderListItem(int index, HtmlTextWriter writer)
        {
            ListItem item = Items[index];
            writer.AddAttribute(HtmlTextWriterAttribute.Id,
String.Format("item_{0}",
index));
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            checkBox.ID = String.Format("chk_item_{0}", index.ToString());
            checkBox.Text = item.Text;
            checkBox.AutoPostBack = AutoPostBack;
            checkBox.Enabled = Enabled;
            checkBox.ValidationGroup = ValidationGroup;
            checkBox.CausesValidation = CausesValidation;
            checkBox.Checked = item.Selected;
            checkBox.RenderControl(writer);
            writer.RenderEndTag();
        }

        #region IPostBackDataHandler Members

        public bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
        {
            EnsureDataBound();
            int checkboxIndex = -1;

            try
            {
                string id = postDataKey.Substring(ClientID.Length + 1);
                if (Char.IsDigit(id[0]))
                    checkboxIndex = Int32.Parse(id);
            }
            catch
            {
                return false;
            }
            if (checkboxIndex == -1)
                return false;

            string val = postCollection[postDataKey];
            bool isChecked = val == "on";
            ListItem item = Items[checkboxIndex];
            if (item.Selected != isChecked)
            {
                item.Selected = isChecked;
                return true;
            }
            return false;
        }

        public void RaisePostDataChangedEvent()
        {
            if (CausesValidation)
                Page.Validate(ValidationGroup);
            OnSelectedIndexChanged(EventArgs.Empty);

        }

        #endregion
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20070417/f4feefe5/attachment-0001.html 


More information about the Mono-list mailing list