2002-24-02 Gaurav Vaish <gvaish@iitk.ac.in>
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlControl.cs
1 //\r
2 // System.Web.UI.HtmlControls.HtmlControl.cs\r
3 //\r
4 // Author\r
5 //   Bob Smith <bob@thestuff.net>\r
6 //\r
7 //\r
8 // (C) Bob Smith\r
9 //\r
10 \r
11 using System;\r
12 using System.Globalization;\r
13 using System.Web;\r
14 using System.Web.UI;\r
15 \r
16 namespace System.Web.UI.HtmlControls{\r
17         \r
18         public abstract class HtmlControl : Control, IAttributeAccessor\r
19         {\r
20                 private string _tagName = "span";\r
21                 //TODO: Is this correct, or is the StateBag really the ViewState?\r
22                 private AttributeCollection _attributes = new AttributeCollection(new StateBag(true));\r
23                 private bool _disabled = false;\r
24                 \r
25                 public HtmlControl(){}\r
26                 \r
27                 public HtmlControl(string tag)\r
28                 {\r
29                         if(tag != null && tag != String.Empty) _tagName = tag;\r
30                 }\r
31                 \r
32                 internal static string AttributeToString(int n){\r
33                         if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);\r
34                         return null;\r
35                 }\r
36                 \r
37                 internal static string AttributeToString(string s){\r
38                         if (s != null && s.Length != 0) return s;\r
39                         return null;\r
40                 }\r
41                 \r
42                 internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){\r
43                         string attr = Attributes[attribName];\r
44                         if (attr != null){\r
45                                 if (attr.Length != 0){\r
46                                         try{\r
47                                                 attr = ResolveUrl(attr);\r
48                                         }\r
49                                         catch (Exception e) {\r
50                                                 throw new HttpException(attribName + " property had malformed url");\r
51                                         }\r
52                                         writer.WriteAttribute(attribName, attr);\r
53                                         Attributes.Remove(attribName);\r
54                                 }\r
55                         }\r
56                 }\r
57                 \r
58                 string System.Web.UI.IAttributeAccessor.GetAttribute(string name){\r
59                         return Attributes[name];\r
60                 }\r
61                 \r
62                 void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){\r
63                         Attributes[name] = value;\r
64                 }\r
65                 \r
66                 protected virtual void RenderAttributes(HtmlTextWriter writer){\r
67                         if (ID != null){\r
68                                 writer.WriteAttribute("id",ClientID);\r
69                         }\r
70                         Attributes.Render(writer);\r
71                 }\r
72                 \r
73                 internal static void WriteOnClickAttribute(HtmlTextWriter writer, bool submitsAutomatically, bool submitsProgramatically, bool causesValidation) {\r
74                         string local1;\r
75                         string local2;\r
76                         string local3;\r
77                         \r
78                         AttributeCollection attr = Attributes;\r
79                         local1 = null;\r
80                         if (submitsAutomatically) {\r
81                                 if ((causesValidation))\r
82                                         local1 = System.Web.UI.Utils.GetClientValidateEvent(Page);\r
83                         }\r
84                         else if (submitsProgramatically) {\r
85                                 if (causesValidation)\r
86                                         local1 = System.Web.UI.Utils.GetClientValidatedPostback(this);\r
87                                 else\r
88                                         local1 = Page.GetPostBackClientEvent(this, String.Empty);\r
89                         }\r
90                         if (local1 != null) {\r
91                                 local2 = attr["language"];\r
92                                 if (local2 != null)\r
93                                         attr.Remove("language");\r
94                                 writer.WriteAttribute("language", "javascript");\r
95                                 local3 = attr["onclick"];\r
96                                 if (local3 != null) {\r
97                                         attr.Remove("onclick");\r
98                                         writer.WriteAttribute("onclick", local3 + " " + local1);\r
99                                         return;\r
100                                 }\r
101                                 writer.WriteAttribute("onclick", local1);\r
102                         }\r
103                 }\r
104                 \r
105                 public AttributeCollection Attributes\r
106                 {\r
107                         get\r
108                         {\r
109                                 return _attributes;\r
110                         }\r
111                 }\r
112                 public bool Disabled\r
113                 {\r
114                         get\r
115                         {\r
116                                 return _disabled;\r
117                         }\r
118                         set\r
119                         {\r
120                                 _disabled = value;\r
121                         }\r
122                 }\r
123                 public CssStyleCollection Style\r
124                 {\r
125                         get\r
126                         {\r
127                                 return _attributes.CssStyle;\r
128                         }\r
129                 }\r
130                 public virtual string TagName\r
131                 {\r
132                         get\r
133                         {\r
134                                 return _tagName;\r
135                         }\r
136                 }\r
137         }\r
138 }\r