D:/Projects/Anthem/Anthem/RadioButtonList.cs

00001 using System;
00002 using System.ComponentModel;
00003 using System.Drawing;
00004 using System.IO;
00005 using System.Text.RegularExpressions;
00006 using System.Web;
00007 using System.Web.UI;
00008 using ASP = System.Web.UI.WebControls;
00009 
00010 namespace Anthem
00011 {
00015     [ToolboxBitmap(typeof(ASP.RadioButtonList))]
00016     public class RadioButtonList : ASP.RadioButtonList, IUpdatableControl, ICallBackControl
00017     {
00018         #region Unique Anthem control code
00019 
00024         [DefaultValue(false)]
00025         public bool AutoCallBack
00026         {
00027             get
00028             {
00029                 if (null == ViewState["AutoCallBack"])
00030                     return false;
00031                 else
00032                     return (bool)ViewState["AutoCallBack"];
00033             }
00034             set { ViewState["AutoCallBack"] = value; }
00035         }
00036 
00037 #if V2
00043         [Category("Default")]
00044         [DefaultValue("")]
00045         [MergableProperty(false)]
00046         [PersistenceMode(PersistenceMode.InnerProperty)]
00047         public override ASP.ListItemCollection Items
00048         {
00049             get
00050             {
00051                 return base.Items;
00052             }
00053         }
00054 #endif
00055 
00056 #if V2
00061         protected override void OnSelectedIndexChanged(EventArgs e)
00062         {
00063             base.OnSelectedIndexChanged(e);
00064             this.UpdateAfterCallBack = true;
00065         }
00066 
00067         private int _renderedItemIndex = 0;
00072         protected override void RenderItem(ASP.ListItemType itemType, int repeatIndex, ASP.RepeatInfo repeatInfo, HtmlTextWriter writer)
00073         {
00074             if (AutoCallBack)
00075             {
00076                 ASP.ListItem item = this.Items[_renderedItemIndex];
00077                 if (item.Selected)
00078                 {
00079                     item.Attributes.Remove("onclick");
00080                 }
00081                 else
00082                 {
00083                     item.Attributes["onclick"] = Anthem.Manager.GetCallbackEventReference(
00084                         this,
00085                         _renderedItemIndex.ToString(),
00086                         this.CausesValidation,
00087                         this.ValidationGroup
00088                     );
00089                 }
00090                 _renderedItemIndex += 1;
00091             }
00092 
00093             base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
00094         }
00095 #else
00096         // The ASP.NET 1.1 version of RadioButtonList does not have an override for creating each
00097         // item in the list, so we need some to use some method that takes affect on the entire
00098         // control. One option is to let the base control render to a string and then use Regex
00099         // to replace __doPostBack with Anthem_FireCallBackEvent. That is how the Calendar
00100         // control is handled. However, that technique is "fragile" because it will not work
00101         // if the base control changes how it is rendered (such as a change in HtmlWriter).
00102         // During my testing, I noticed that ASP.NET 1.1 always rendered the __doPostBack for
00103         // each item, even if it was already selected. I decided to take advantage of this
00104         // and also the fact that javascript events bubble up to the parent element. So this
00105         // override of OnPreRender adds a single Anthem callback to the parent element.
00106         protected override void OnPreRender(EventArgs e)
00107         {
00108             base.OnPreRender(e);
00109             if (AutoCallBack)
00110             {
00111                 Anthem.Manager.AddScriptAttribute(
00112                     this,
00113                     "onclick",
00114                     string.Format(
00115                     "AnthemListControl_OnClick(event,{0},'{1}','{2}',{3},{4},{5},{6});",
00116                     "false",
00117                     string.Empty,
00118                     this.TextDuringCallBack,
00119                     this.EnabledDuringCallBack ? "true" : "false",
00120                     (this.PreCallBackFunction == null || this.PreCallBackFunction.Length == 0) ? "null" : this.PreCallBackFunction,
00121                     (this.PostCallBackFunction == null || this.PostCallBackFunction.Length == 0) ? "null" : this.PostCallBackFunction,
00122                     (this.CallBackCancelledFunction == null || this.CallBackCancelledFunction.Length == 0) ? "null" : this.CallBackCancelledFunction
00123                     )
00124                 );
00125             }
00126         }
00127 #endif
00128 
00133         protected override void Render(HtmlTextWriter writer)
00134         {
00135 #if !V2
00136             bool DesignMode = this.Context == null;
00137 #endif
00138             if (!DesignMode)
00139             {
00140                 Anthem.Manager.WriteBeginControlMarker(writer, this.RepeatLayout == ASP.RepeatLayout.Flow ? "span" : "div", this);
00141             }
00142             if (Visible)
00143             {
00144                 base.Render(writer);
00145             }
00146             if (!DesignMode)
00147             {
00148                 Anthem.Manager.WriteEndControlMarker(writer, this.RepeatLayout == ASP.RepeatLayout.Flow ? "span" : "div", this);
00149             }
00150         }
00151 
00152         #endregion
00153 
00154         #region ICallBackControl implementation
00155 
00160         [Category("Anthem")]
00161         [DefaultValue("")]
00162         [Description("The javascript function to call on the client if the callback is cancelled.")]
00163         public virtual string CallBackCancelledFunction
00164         {
00165             get
00166             {
00167                 string text = (string)ViewState["CallBackCancelledFunction"];
00168                 if (text == null)
00169                     return string.Empty;
00170                 else
00171                     return text;
00172             }
00173             set
00174             {
00175                 ViewState["CallBackCancelledFunction"] = value;
00176             }
00177         }
00178 
00186         [Category("Anthem")]
00187         [DefaultValue(true)]
00188         [Description("True if this control uses callbacks instead of postbacks to post data to the server.")]
00189         public virtual bool EnableCallBack
00190         {
00191             get
00192             {
00193                 object obj = this.ViewState["EnableCallBack"];
00194                 if (obj == null)
00195                     return true;
00196                 else
00197                     return (bool)obj;
00198             }
00199             set
00200             {
00201                 ViewState["EnableCallBack"] = value;
00202             }
00203         }
00204 
00213         [Category("Anthem")]
00214         [DefaultValue(true)]
00215         [Description("True if this control is enabled on the client during callbacks.")]
00216         public virtual bool EnabledDuringCallBack
00217         {
00218             get
00219             {
00220                 object obj = this.ViewState["EnabledDuringCallBack"];
00221                 if (obj == null)
00222                     return true;
00223                 else
00224                     return (bool)obj;
00225             }
00226             set
00227             {
00228                 ViewState["EnabledDuringCallBack"] = value;
00229             }
00230         }
00231 
00232 
00250         [Category("Anthem")]
00251         [DefaultValue("")]
00252         [Description("The javascript function to call on the client after the callback response is received.")]
00253         public virtual string PostCallBackFunction
00254         {
00255             get
00256             {
00257                 string text = (string)this.ViewState["PostCallBackFunction"];
00258                 if (text == null)
00259                 {
00260                     return string.Empty;
00261                 }
00262                 return text;
00263             }
00264             set
00265             {
00266                 ViewState["PostCallBackFunction"] = value;
00267             }
00268         }
00269 
00275         [Category("Anthem")]
00276         [DefaultValue("")]
00277         [Description("The javascript function to call on the client before the callback is made.")]
00278         public virtual string PreCallBackFunction
00279         {
00280             get
00281             {
00282                 string text = (string)this.ViewState["PreCallBackFunction"];
00283                 if (text == null)
00284                 {
00285                     return string.Empty;
00286                 }
00287                 return text;
00288             }
00289             set
00290             {
00291                 ViewState["PreCallBackFunction"] = value;
00292             }
00293         }
00294 
00303         [Category("Anthem")]
00304         [DefaultValue("")]
00305         [Description("The text to display during the callback.")]
00306         public virtual string TextDuringCallBack
00307         {
00308             get
00309             {
00310                 string text = (string)this.ViewState["TextDuringCallBack"];
00311                 if (text == null)
00312                 {
00313                     return string.Empty;
00314                 }
00315                 return text;
00316             }
00317             set
00318             {
00319                 ViewState["TextDuringCallBack"] = value;
00320             }
00321         }
00322 
00323         #endregion
00324 
00325         #region IUpdatableControl implementation
00326 
00340         [Category("Anthem")]
00341         [DefaultValue(false)]
00342         [Description("True if this control should be updated after each callback.")]
00343         public virtual bool AutoUpdateAfterCallBack
00344         {
00345             get
00346             {
00347                 object obj = this.ViewState["AutoUpdateAfterCallBack"];
00348                 if (obj == null)
00349                     return false;
00350                 else
00351                     return (bool)obj;
00352             }
00353             set
00354             {
00355                 if (value) UpdateAfterCallBack = true;
00356                 ViewState["AutoUpdateAfterCallBack"] = value;
00357             }
00358         }
00359 
00360         private bool _updateAfterCallBack = false;
00361 
00376         [Browsable(false)]
00377         [DefaultValue(false)]
00378         public virtual bool UpdateAfterCallBack
00379         {
00380             get { return _updateAfterCallBack; }
00381             set { _updateAfterCallBack = value; }
00382         }
00383 
00384         #endregion
00385 
00386         #region Common Anthem control code
00387 
00393         protected override void OnLoad(EventArgs e)
00394         {
00395             base.OnLoad(e);
00396             Anthem.Manager.Register(this);
00397         }
00398 
00399 #if V2
00403         public override void RenderControl(HtmlTextWriter writer)
00404         {
00405             base.Visible = true;
00406             base.RenderControl(writer);
00407         }
00408 #endif
00409 
00417         public override bool Visible
00418         {
00419             get
00420             {
00421 #if !V2
00422                 bool DesignMode = this.Context == null;
00423 #endif
00424                 return Anthem.Manager.GetControlVisible(this, ViewState, DesignMode);
00425             }
00426             set { Anthem.Manager.SetControlVisible(ViewState, value); }
00427         }
00428 
00429         #endregion
00430     }
00431 }

Generated on Wed Mar 14 23:50:39 2007 for Anthem by  doxygen 1.5.1-p1