2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / RadioButton.cs
1 //
2 // System.Web.UI.WebControls.RadioButton.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //
11 \r
12 using System;\r
13 using System.Collections;\r
14 using System.Collections.Specialized;\r
15 using System.Globalization;\r
16 using System.Web;\r
17 using System.Web.UI;\r
18 using System.ComponentModel;
19 using System.ComponentModel.Design;\r
20 \r
21 namespace System.Web.UI.WebControls\r
22 {\r
23         [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]\r
24         public class RadioButton : CheckBox, IPostBackDataHandler\r
25         {\r
26                 public RadioButton () : base ()\r
27                 {\r
28                 }
29 \r
30                 [DefaultValue (""), WebCategory ("Behavior")]
31                 [WebSysDescription ("The name of the group that this control belongs to.")]\r
32                 public virtual string GroupName\r
33                 {\r
34                         get {\r
35                                 object o = ViewState ["GroupName"];\r
36                                 return (o == null) ? String.Empty : (string) o;\r
37                         }\r
38 \r
39                         set { ViewState ["GroupName"] = value; }\r
40                 }\r
41 \r
42                 protected override void OnPreRender (EventArgs e)\r
43                 {\r
44                         base.OnPreRender (e);\r
45                         if (Page != null && Enabled && !Checked)\r
46                                 Page.RegisterRequiresPostBack (this);\r
47 \r
48                         if(GroupName.Length == 0)\r
49                                 GroupName = UniqueID;\r
50                 }\r
51 \r
52                 internal override void RenderInputTag (HtmlTextWriter writer, string id)\r
53                 {\r
54                         writer.AddAttribute (HtmlTextWriterAttribute.Id, id);\r
55                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "radio");\r
56                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueGroupNamePrivate);\r
57                         writer.AddAttribute (HtmlTextWriterAttribute.Value, ValueAttributePrivate);\r
58 \r
59                         if (Checked)\r
60                                 writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
61                         \r
62                         if (!Enabled)
63                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
64 \r
65                         if (AutoPostBack){\r
66                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick,\r
67                                                      Page.GetPostBackClientEvent (this, ""));\r
68                                 writer.AddAttribute ("language", "javascript");\r
69                         }\r
70 \r
71                         if (AccessKey.Length > 0)\r
72                                 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);\r
73 \r
74                         if (TabIndex != 0)
75                                 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,\r
76                                                      TabIndex.ToString (NumberFormatInfo.InvariantInfo));\r
77 \r
78                         writer.RenderBeginTag (System.Web.UI.HtmlTextWriterTag.Input);\r
79                         writer.RenderEndTag ();\r
80                 }\r
81 \r
82                 private string UniqueGroupNamePrivate\r
83                 {\r
84                         get {\r
85                                 string retVal = GroupName;\r
86                                 int unique = UniqueID.LastIndexOf (':');\r
87                                 if (unique >= 0)\r
88                                         retVal += UniqueID.Substring (unique + 1);\r
89 \r
90                                 return retVal;\r
91                         }\r
92                 }\r
93 \r
94                 private string ValueAttributePrivate\r
95                 {\r
96                         get {\r
97                                 string retVal = Attributes ["value"];\r
98                                 if (retVal != null)\r
99                                         return retVal;\r
100 \r
101                                 if (ID != null)\r
102                                         return ID;\r
103 \r
104                                 return UniqueID;\r
105                         }\r
106                 }\r
107 \r
108                 bool IPostBackDataHandler.LoadPostData (string postDataKey,\r
109                                                         NameValueCollection postCollection)\r
110                 {\r
111                         bool _checked = Checked;\r
112                         if (postCollection [UniqueGroupNamePrivate] == ValueAttributePrivate){\r
113                                 if (_checked)\r
114                                         return false;\r
115                                 Checked = true;\r
116                                 return true;\r
117                         }\r
118 \r
119                         if (_checked)\r
120                                 Checked = false;\r
121                         return true;\r
122                 }\r
123 \r
124                 void IPostBackDataHandler.RaisePostDataChangedEvent ()\r
125                 {\r
126                         OnCheckedChanged (EventArgs.Empty);\r
127                 }\r
128         }\r
129 }\r