2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ListBox.cs
index 5a740f086a0a7f65589676e5b2516599a0ef2493..2586a1ec204884683e9cd2d9dc9a8055c4c6d0a9 100644 (file)
@@ -32,6 +32,7 @@ using System.Drawing;
 using System.Globalization;
 using System.Collections.Specialized;
 using System.Security.Permissions;
+using System.Web.Util;
 
 namespace System.Web.UI.WebControls {
 
@@ -137,7 +138,6 @@ namespace System.Web.UI.WebControls {
                        if (Page != null)
                                Page.VerifyRenderingInServerForm (this);
 
-                       base.AddAttributesToRender (writer);
 #if NET_2_0
                        if (ID != null)
                                writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
@@ -145,38 +145,60 @@ namespace System.Web.UI.WebControls {
                        writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
 #endif
 
-                       if (AutoPostBack)
+                       if (AutoPostBack) {
+#if NET_2_0
+                               string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
+                               onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
+                               writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
+#else
                                writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
-                                               Page.ClientScript.GetPostBackClientHyperlink (this, ""));
-
+                                                    BuildScriptAttribute ("onchange",
+                                                                          Page.ClientScript.GetPostBackClientHyperlink (this, "")));
+#endif
+                       }
+                       
                        if (SelectionMode == ListSelectionMode.Multiple)
                                writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
-                                               "multiple");
+                                               "multiple", false);
                        writer.AddAttribute (HtmlTextWriterAttribute.Size,
-                                        Rows.ToString (CultureInfo.InvariantCulture));
+                                        Rows.ToString (Helpers.InvariantCulture));
+                       
+                       base.AddAttributesToRender (writer);
                }
 
 #if NET_2_0
-               protected internal
-#else          
-               protected
+               PostBackOptions GetPostBackOptions () {
+                       PostBackOptions options = new PostBackOptions (this);
+                       options.ActionUrl = null;
+                       options.ValidationGroup = null;
+                       options.Argument = String.Empty;
+                       options.RequiresJavaScriptProtocol = false;
+                       options.ClientSubmit = true;
+                       options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
+                       if (options.PerformValidation)
+                               options.ValidationGroup = ValidationGroup;
+
+                       return options;
+               }
 #endif         
-               override void RenderContents (HtmlTextWriter writer)
+
+#if ONLY_1_1
+               protected override void RenderContents (HtmlTextWriter writer)
                {
                        foreach (ListItem item in Items) {
-                               writer.WriteBeginTag ("option");
                                if (item.Selected) {
-                                       writer.WriteAttribute ("selected", "selected", false);
+                                       writer.AddAttribute (HtmlTextWriterAttribute.Selected, "selected", false);
                                }
-                               writer.WriteAttribute ("value", item.Value, true);
-
-                               writer.Write (">");
+                               writer.AddAttribute (HtmlTextWriterAttribute.Value, item.Value, true);
+                               writer.RenderBeginTag (HtmlTextWriterTag.Option);
+                               
                                string encoded = HttpUtility.HtmlEncode (item.Text);
                                writer.Write (encoded);
-                               writer.WriteEndTag ("option");
+                               writer.RenderEndTag ();
                                writer.WriteLine ();
                        }
                }
+#endif
 
 #if NET_2_0
                protected internal
@@ -186,7 +208,7 @@ namespace System.Web.UI.WebControls {
                override void OnPreRender (EventArgs e)
                {
                        base.OnPreRender (e);
-                       if (Page != null)
+                       if (Page != null && Enabled)
                                Page.RegisterRequiresPostBack (this);
                }
 
@@ -195,12 +217,18 @@ namespace System.Web.UI.WebControls {
 #endif
                bool LoadPostData (string postDataKey, NameValueCollection postCollection)
                {
+#if NET_2_0
+                       EnsureDataBound ();
+#endif
                        string [] values = postCollection.GetValues (postDataKey);
                        if (values == null || values.Length == 0) {
                                int prev_index = SelectedIndex;
                                SelectedIndex = -1;
                                return (prev_index != -1);
                        }
+#if NET_2_0
+                       ValidateEvent (UniqueID, values [0]);
+#endif
 
                        if (SelectionMode == ListSelectionMode.Single)
                                return SelectSingle (values);
@@ -248,6 +276,10 @@ namespace System.Web.UI.WebControls {
 #endif
                void RaisePostDataChangedEvent ()
                {
+#if NET_2_0
+                       if (CausesValidation)
+                               Page.Validate (ValidationGroup);
+#endif
                        OnSelectedIndexChanged (EventArgs.Empty);
                }
                        
@@ -261,6 +293,18 @@ namespace System.Web.UI.WebControls {
                {
                        RaisePostDataChangedEvent ();
                }
+#if NET_2_0
+        protected internal override void VerifyMultiSelect()
+        {
+            //by default the ListControl will throw an exception in this method,
+            //therefor we should override the method if the class is supporting
+            //MultiSelect option.
+            if (this.SelectionMode != ListSelectionMode.Multiple)
+                throw new HttpException("Multiple select option is not supported");
+        }
+#endif
        }
 }
 
+
+