Files
mono/mcs/class/System.Web/System.Web.UI.HtmlControls/HtmlSelect.cs
T
Dick Porter 3ff73d8d7d 2005-07-27 Dick Porter <[email protected]>
* HtmlInputCheckBoxTest.cs: Tests for HtmlInputCheckBox

2005-07-26  Dick Porter  <[email protected]>

	* HtmlSelectTest.cs: Tests for HtmlSelect

2005-07-21  Dick Porter  <[email protected]>

	* HtmlImageTest.cs: The order of attribute rendering has changed,
	and now matches MS

2005-07-19  Dick Porter  <[email protected]>

	* HtmlImageTest.cs: Added tests for negative values in int
	properties

2005-07-14  Dick Porter  <[email protected]>

	* HtmlFormTest.cs: New set of tests

2005-07-07  Dick Porter  <[email protected]>

	* HtmlImageTest.cs: Unit tests for HtmlImage

2005-08-04  Dick Porter  <[email protected]>

	* DataGridColumnTest.cs: Tests for DataGridColumn

2005-07-29  Dick Porter  <[email protected]>

	* RadioButtonTest.cs: Added tests for RadioButton

2005-07-14  Dick Porter  <[email protected]>

	* CheckBoxTest.cs: Added enum checks

2005-07-13  Dick Porter  <[email protected]>

	* CheckBoxTest.cs: Added tests for CheckBox - currently all
	passing on mono and ms runtimes.

2005-07-14  Dick Porter  <[email protected]>

	* HttpPostedFile.cs: New basic implementation; needs someone to
	figure out what a HttpRequestStream does

2005-08-18  Dick Porter  <[email protected]>

	* HtmlTable.cs, HtmlInputButton.cs, HtmlInputFile.cs, HtmlHead.cs,
	HtmlInputImage.cs, HtmlForm.cs, HtmlInputText.cs,
	HtmlInputCheckBox.cs, HtmlAnchor.cs, HtmlInputRadioButton.cs,
	HtmlButton.cs, HtmlInputHidden.cs, HtmlTextArea.cs,
	HtmlTableRow.cs, HtmlSelect.cs, HtmlControl.cs, HtmlTitle.cs,
	HtmlContainerControl.cs: 2.0 API fixes and stubs and attribute fixes

2005-08-09  Dick Porter  <[email protected]>

	* HtmlSelect.cs: Clear databindings when adding a new one; cope
	with assorted combinations of empty text and value fields; write
	<option> tags manually to match ms HTML output.  All fix nunit
	tests.

2005-08-05  Dick Porter  <[email protected]>

	* HtmlSelect.cs: Fix databinding when one or other of Name and
	Value aren't supplied (thanks to Peter for the patch); Make
	<option> indenting match ms output.

2005-07-29  Dick Porter  <[email protected]>

	* HtmlForm.cs: Simplify the rendering code by using QueryStringRaw
	instead of building the query string by hand

2005-07-27  Dick Porter  <[email protected]>

	* HtmlInputCheckBox.cs: New implementation

2005-07-26  Dick Porter  <[email protected]>

	* HtmlSelect.cs: New implementation

2005-07-24  Dick Porter  <[email protected]>

	* HtmlForm.cs: Add the trailing query string to the form action;
	fixes bug shown in Test/standalone/htmlform/qs_postback.aspx

2005-07-22  Dick Porter  <[email protected]>

	* HtmlInputFile.cs: Add assembly attributes

2005-07-21  Dick Porter  <[email protected]>

	* HtmlImage.cs: The src attribute needs to be a relative URL;
	close the HTML element when rendering attributes, to match the MS
	impl.

	* HtmlForm.cs: Added assembly attributes; don't render ID twice;
	don't render enctype and target if they are empty strings;
	implement a Render overload to keep corcompare quiet.

2005-07-19  Dick Porter  <[email protected]>

	* HtmlImage.cs: Added attributes; use Int32.Parse instead of
	Convert.ToInt32

2005-07-14  Dick Porter  <[email protected]>

	* HtmlInputFile.cs: New implementation

2005-07-14  Dick Porter  <[email protected]>

	* HtmlForm.cs: New Implementation

2005-07-07  Dick Porter  <[email protected]>

	* HtmlImage.cs: New implementation

2005-08-18  Dick Porter  <[email protected]>

	* ControlCachePolicy.cs, PersistChildrenAttribute.cs,
	UserControl.cs, DesignerDataBoundLiteralControl.cs,
	PageStatePersister.cs, DataBoundLiteralControl.cs, Control.cs,
	BasePartialCachingControl.cs, LiteralControl.cs: 2.0 API fixes and
	stubs and attribute fixes

2005-08-11  Dick Porter  <[email protected]>

	* CssStyleCollection.cs: Tweak the css string format to pass a
	unit test

	* AttributeCollection.cs: Don't NRE if someone sets the "style"
	attribute to null.

2005-08-05  Dick Porter  <[email protected]>

	* HtmlTextWriter.cs: Make <option> tags render inline, to match
	the ms output

2005-07-07  Dick Porter  <[email protected]>

	* StateBag.cs: Added internal SetDirty (void) method to fix the
	build

2005-08-18  Dick Porter  <[email protected]>

	* PageAdapter.cs, ControlAdapter.cs: 2.0 stubs

2005-08-18  Dick Porter  <[email protected]>

	* Consts.cs: Added System.Web and Microsoft.VisualStudio.Web
	constants for 2.0

2005-08-18  Dick Porter  <[email protected]>

	* Button.cs, DetailsView.cs, Xml.cs, Table.cs, Menu.cs,
	DataControlButton.cs, Label.cs, ObjectDataSource.cs, CheckBox.cs,
	RadioButton.cs, ListControl.cs, Image.cs, SiteMapPath.cs,
	BaseDataList.cs, DataList.cs, BulletedList.cs,
	DataBoundControl.cs, Login.cs, GridView.cs,
	CompositeDataBoundControl.cs, Wizard.cs, BaseValidator.cs,
	TextBox.cs, CheckBoxList.cs, RadioButtonList.cs, FormView.cs,
	AdRotator.cs, Repeater.cs, MenuItemTemplateContainer.cs,
	DropDownList.cs, HyperLink.cs, ImageMap.cs, MultiView.cs,
	CompositeControl.cs, HierarchicalDataBoundControl.cs, Calendar.cs,
	ListBox.cs, TableCell.cs, WebControl.cs, Literal.cs,
	BaseDataBoundControl.cs, ValidationSummary.cs, ImageButton.cs,
	LinkButton.cs, TreeView.cs: 2.0 API fixes and stubs, and attribute
	fixes

2005-08-11  Dick Porter  <[email protected]>

	* CheckBox.cs: Let ViewState manage removals; apply styles and
	"style" attributes in a surrounding <span>.  Fixes unit tests.

2005-08-05  Dick Porter  <[email protected]>

	* CheckBox.cs: Shut corcompare up a bit more.  Still need to
	figure out how to not use AddAttributesToRender to get it
	completely silent.

	* ButtonColumn.cs: Implement Initialize() and InitializeCell()

	* DataGridColumn.cs: Added the duplicate viewstate manipulation
	methods

2005-08-04  Dick Porter  <[email protected]>

	* DataGridColumn.cs: New implementation

	* DataGrid.cs: 
	* DataGridColumnCollection.cs: Set the DataGridColumn's owner

2005-07-29  Dick Porter  <[email protected]>

	* RadioButton.cs: Hide nonstandard API; remove GroupName from the
	ViewState when it is null.

	* CheckBox.cs: Hide nonstandard API

2005-07-22  Dick Porter  <[email protected]>

	* BoundColumn.cs: 
	* ButtonColumn.cs: Initial stubs

2005-07-22  Dick Porter  <[email protected]>

	* RadioButton.cs:
	* CheckBox.cs: Add assembly attributes

2005-07-13  Dick Porter  <[email protected]>

	* CheckBox.cs: Reverted Ben's enum-to-int ViewState change, remove
	null Text properties from the ViewState, test for TextAlign being
	out of range.  All to make tests pass on both mono and ms
	runtimes.

2005-07-12  Dick Porter  <[email protected]>

	* RadioButton.cs: New implementation

	* CheckBox.cs: Rearrange the attributes rendering to allow
	RadioButton to derive from this

2005-07-11  Dick Porter  <[email protected]>

	* CheckBox.cs: New implementation

	* RadioButton.cs: Initial stubbed version that just compiles

2005-07-08  Dick Porter  <[email protected]>

	* TextAlign.cs: 
	* BorderStyle.cs: Added

svn path=/trunk/mcs/; revision=48915
2005-08-26 22:21:30 +00:00

730 lines
15 KiB
C#

//
// System.Web.UI.HtmlControls.HtmlSelect.cs
//
// Author:
// Dick Porter <[email protected]>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Util;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
namespace System.Web.UI.HtmlControls
{
[DefaultEvent ("ServerChange")]
[ValidationProperty ("Value")]
[ControlBuilder (typeof (HtmlSelectBuilder))]
public class HtmlSelect : HtmlContainerControl , IPostBackDataHandler
#if NET_2_0
, IParserAccessor
#endif
{
public HtmlSelect () : base ("select")
{
}
[DefaultValue ("")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public virtual string DataMember
{
get {
string member = Attributes["datamember"];
if (member == null) {
return (String.Empty);
}
return (member);
}
set {
if (value == null) {
Attributes.Remove ("datamember");
} else {
Attributes["datamember"] = value;
}
}
}
object datasource;
[DefaultValue (null)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public virtual object DataSource
{
get {
return (datasource);
}
set {
if ((value != null) &&
!(value is IEnumerable) &&
!(value is IListSource)) {
throw new ArgumentException ();
}
datasource = value;
}
}
#if NET_2_0
[DefaultValue ("")]
public virtual string DataSourceID
{
get {
return (String.Empty);
}
set {
}
}
#endif
[DefaultValue ("")]
public virtual string DataTextField
{
get {
string text = Attributes["datatextfield"];
if (text == null) {
return (String.Empty);
}
return (text);
}
set {
if (value == null) {
Attributes.Remove ("datatextfield");
} else {
Attributes["datatextfield"] = value;
}
}
}
[DefaultValue ("")]
public virtual string DataValueField
{
get {
string value = Attributes["datavaluefield"];
if (value == null) {
return (String.Empty);
}
return (value);
}
set {
if (value == null) {
Attributes.Remove ("datavaluefield");
} else {
Attributes["datavaluefield"] = value;
}
}
}
public override string InnerHtml
{
get {
throw new NotSupportedException ();
}
set {
throw new NotSupportedException ();
}
}
public override string InnerText
{
get {
throw new NotSupportedException ();
}
set {
throw new NotSupportedException ();
}
}
#if NET_2_0
[MonoTODO]
protected bool IsBoundUsingDataSourceID
{
get {
throw new NotImplementedException ();
}
}
#endif
ListItemCollection items;
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[Browsable (false)]
public ListItemCollection Items
{
get {
if (items == null) {
items = new ListItemCollection ();
}
return (items);
}
}
[DefaultValue ("")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public bool Multiple
{
get {
string multi = Attributes["multiple"];
if (multi == null) {
return (false);
}
return (true);
}
set {
if (value == false) {
Attributes.Remove ("multiple");
} else {
Attributes["multiple"] = "multiple";
}
}
}
[DefaultValue ("")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public string Name
{
get {
return (UniqueID);
}
set {
/* Do nothing */
}
}
#if NET_2_0
[MonoTODO]
protected bool RequiresDataBinding
{
get {
throw new NotImplementedException ();
}
set {
throw new NotImplementedException ();
}
}
#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[Browsable (false)]
public virtual int SelectedIndex
{
get {
/* Make sure Items has been initialised */
ListItemCollection listitems = Items;
for (int i = 0; i < listitems.Count; i++) {
if (listitems[i].Selected) {
return (i);
}
}
/* There is always a selected item in
* non-multiple mode, if the size is
* <= 1
*/
if (!Multiple && Size <= 1) {
/* Select the first item */
if (listitems.Count > 0) {
/* And make it stick
* if there is
* anything in the
* list
*/
listitems[0].Selected = true;
}
return (0);
}
return (-1);
}
set {
ClearSelection ();
if (value == -1 || items == null) {
return;
}
if (value < 0 || value >= items.Count) {
throw new ArgumentOutOfRangeException ("value");
}
items[value].Selected = true;
}
}
/* "internal infrastructure" according to the docs,
* but has some documentation in 2.0
*/
protected virtual int[] SelectedIndices
{
get {
ArrayList selected = new ArrayList ();
int count = Items.Count;
for (int i = 0; i < count; i++) {
if (Items [i].Selected) {
selected.Add (i);
}
}
return ((int[])selected.ToArray (typeof (int)));
}
}
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public int Size
{
get {
string size = Attributes["size"];
if (size == null) {
return (-1);
}
return (Int32.Parse (size, CultureInfo.InvariantCulture));
}
set {
if (value == -1) {
Attributes.Remove ("size");
} else {
Attributes["size"] = value.ToString ();
}
}
}
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public string Value
{
get {
int sel = SelectedIndex;
if (sel >= 0 && sel < Items.Count) {
return (Items[sel].Value);
}
return (String.Empty);
}
set {
int sel = Items.IndexOf (value);
if (sel >= 0) {
SelectedIndex = sel;
}
}
}
private static readonly object EventServerChange = new object ();
public event EventHandler ServerChange
{
add {
Events.AddHandler (EventServerChange, value);
}
remove {
Events.RemoveHandler (EventServerChange, value);
}
}
protected override void AddParsedSubObject (object obj)
{
if (!(obj is ListItem)) {
throw new HttpException ("HtmlSelect can only contain ListItem");
}
Items.Add ((ListItem)obj);
base.AddParsedSubObject (obj);
}
/* "internal infrastructure" according to the docs,
* but has some documentation in 2.0
*/
protected virtual void ClearSelection ()
{
if (items == null) {
return;
}
int count = items.Count;
for (int i = 0; i < count; i++) {
items[i].Selected = false;
}
}
protected override ControlCollection CreateControlCollection ()
{
return (base.CreateControlCollection ());
}
#if NET_2_0
[MonoTODO]
protected void EnsureDataBound ()
{
throw new NotImplementedException ();
}
[MonoTODO]
protected virtual IEnumerable GetData ()
{
throw new NotImplementedException ();
}
#endif
protected override void LoadViewState (object savedState)
{
object first = null;
object second = null;
int[] selected = null;
Triplet triplet = savedState as Triplet;
if (triplet != null) {
first = triplet.First;
second = triplet.Second;
selected = triplet.Third as int[];
}
base.LoadViewState (first);
if (second != null) {
IStateManager manager = Items as IStateManager;
manager.LoadViewState (second);
}
if (selected != null) {
Select (selected);
}
}
protected override void OnDataBinding (EventArgs e)
{
base.OnDataBinding (e);
/* Make sure Items has been initialised */
ListItemCollection listitems = Items;
listitems.Clear ();
IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
if (list == null) {
return;
}
foreach (object container in list) {
string text = null;
string value = null;
if (DataTextField == String.Empty &&
DataValueField == String.Empty) {
text = container.ToString ();
value = text;
} else {
if (DataTextField != String.Empty) {
text = DataBinder.Eval (container, DataTextField).ToString ();
}
if (DataValueField != String.Empty) {
value = DataBinder.Eval (container, DataValueField).ToString ();
} else {
value = text;
}
if (text == null &&
value != null) {
text = value;
}
}
if (text == null) {
text = String.Empty;
}
if (value == null) {
value = String.Empty;
}
ListItem item = new ListItem (text, value);
listitems.Add (item);
}
}
#if NET_2_0
[MonoTODO]
protected virtual void OnDataPropertyChanged ()
{
RequiresDataBinding = true;
throw new NotImplementedException ();
}
[MonoTODO]
protected virtual void OnDataSourceViewChanged (object sender,
EventArgs e)
{
RequiresDataBinding = true;
throw new NotImplementedException ();
}
[MonoTODO]
protected internal override void OnInit (EventArgs e)
{
/*
if (IsViewStateEnabled == false &&
Page.IsPostBack == true) {
RequiresDataBinding = true;
}
*/
throw new NotImplementedException ();
}
[MonoTODO]
protected internal override void OnLoad (EventArgs e)
{
throw new NotImplementedException ();
}
#endif
#if NET_2_0
protected internal
#else
protected
#endif
override void OnPreRender (EventArgs e)
{
base.OnPreRender (e);
if (Page != null) {
Page.RegisterRequiresPostBack (this);
}
}
protected virtual void OnServerChange (EventArgs e)
{
EventHandler handler = (EventHandler)Events[EventServerChange];
if (handler != null) {
handler (this, e);
}
}
protected override void RenderAttributes (HtmlTextWriter w)
{
/* If there is no "name" attribute,
* LoadPostData doesn't work...
*/
w.WriteAttribute ("name", Name);
Attributes.Remove ("name");
/* Don't render the databinding attributes */
Attributes.Remove ("datamember");
Attributes.Remove ("datatextfield");
Attributes.Remove ("datavaluefield");
base.RenderAttributes (w);
}
#if NET_2_0
protected internal
#else
protected
#endif
override void RenderChildren (HtmlTextWriter w)
{
base.RenderChildren (w);
if (items == null) {
return;
}
w.WriteLine ();
bool done_sel = false;
int count = items.Count;
for (int i = 0; i < count; i++) {
ListItem item = items[i];
w.Indent++;
/* Write the <option> elements this
* way so that the output HTML matches
* the ms version (can't make
* HtmlTextWriterTag.Option an inline
* element, cos that breaks other
* stuff.)
*/
w.WriteBeginTag ("option");
if (item.Selected && !done_sel) {
w.WriteAttribute ("selected", "selected");
if (!Multiple) {
done_sel = true;
}
}
w.WriteAttribute ("value", item.Value);
w.Write (HtmlTextWriter.TagRightChar);
w.Write (item.Text);
w.WriteEndTag ("option");
w.WriteLine ();
w.Indent--;
}
}
protected override object SaveViewState ()
{
object first = null;
object second = null;
object selected = null;
first = base.SaveViewState ();
IStateManager manager = items as IStateManager;
if (manager != null) {
second = manager.SaveViewState ();
}
selected = SelectedIndices;
if (first == null &&
second == null &&
selected == null) {
return (null);
}
return (new Triplet (first, second, selected));
}
/* "internal infrastructure" according to the docs,
* but has some documentation in 2.0
*/
protected virtual void Select (int[] selectedIndices)
{
if (items == null) {
return;
}
ClearSelection ();
int count = items.Count;
foreach (int i in selectedIndices) {
if (i >= 0 && i < count) {
items[i].Selected = true;
}
}
}
protected override void TrackViewState ()
{
base.TrackViewState ();
IStateManager manager = items as IStateManager;
if (manager != null) {
manager.TrackViewState ();
}
}
#if NET_2_0
protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
{
return (LoadPostData (postDataKey, postCollection));
}
protected virtual void RaisePostDataChangedEvent ()
{
RaisePostDataChangedEvent ();
}
#endif
bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
{
/* postCollection contains the values that are
* selected
*/
string[] values = postCollection.GetValues (postDataKey);
bool changed = false;
if (values != null) {
if (Multiple) {
/* We have a set of
* selections. We can't just
* set the new list, because
* we need to know if the set
* has changed from last time
*/
int value_len = values.Length;
int[] old_sel = SelectedIndices;
int[] new_sel = new int[value_len];
int old_sel_len = old_sel.Length;
for (int i = 0; i < value_len; i++) {
new_sel[i] = Items.IndexOf (values[i]);
if (old_sel_len != value_len ||
old_sel[i] != new_sel[i]) {
changed = true;
}
}
if (changed) {
Select (new_sel);
}
} else {
/* Just take the first one */
int sel = Items.IndexOf (values[0]);
if (sel != SelectedIndex) {
SelectedIndex = sel;
changed = true;
}
}
}
return (changed);
}
void IPostBackDataHandler.RaisePostDataChangedEvent ()
{
OnServerChange (EventArgs.Empty);
}
}
}