This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ButtonColumn.cs
1 //
2 // System.Web.UI.WebControls.ButtonColumn.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
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 \r
33 using System;\r
34 using System.ComponentModel;\r
35 using System.Web;\r
36 using System.Web.UI;\r
37 \r
38 namespace System.Web.UI.WebControls\r
39 {\r
40         public class ButtonColumn : DataGridColumn\r
41         {\r
42                 private PropertyDescriptor textFieldDescriptor;\r
43                 \r
44                 public ButtonColumn(): base()\r
45                 {\r
46                 }\r
47 \r
48                 public override void Initialize()\r
49                 {\r
50                         base.Initialize();\r
51                         textFieldDescriptor = null;\r
52                 }\r
53                 \r
54                 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)\r
55                 {\r
56                         base.InitializeCell(cell, columnIndex, itemType);\r
57                         if (Enum.IsDefined(typeof(ListItemType), itemType) &&\r
58                             itemType != ListItemType.Footer &&\r
59                             itemType != ListItemType.Header)\r
60                         {\r
61                                 WebControl toDisplay = null;\r
62                                 if(ButtonType == ButtonColumnType.PushButton)\r
63                                 {\r
64                                         Button b = new Button();\r
65                                         b.Text = Text;\r
66                                         b.CommandName = CommandName;\r
67                                         b.CausesValidation = false;\r
68                                         toDisplay = b;\r
69                                 } else\r
70                                 {\r
71                                         LinkButton lb = new DataGridLinkButton();\r
72                                         lb.Text = Text;\r
73                                         lb.CommandName = CommandName;\r
74                                         lb.CausesValidation = false;\r
75                                         toDisplay = lb;\r
76                                 }\r
77                                 if(DataTextField.Length > 0)\r
78                                 {\r
79                                         toDisplay.DataBinding += new EventHandler(OnDataBindButtonColumn);\r
80                                 }\r
81                                 cell.Controls.Add(toDisplay);\r
82                         }\r
83                 }\r
84                 \r
85                 private void OnDataBindButtonColumn(object sender, EventArgs e)\r
86                 {\r
87                         Control ctrl = (Control)sender;\r
88                         object item = ((DataGridItem)ctrl.NamingContainer).DataItem;\r
89                         if(textFieldDescriptor == null)\r
90                         {\r
91                                 textFieldDescriptor = TypeDescriptor.GetProperties(item).Find(DataTextField, true);\r
92                                 if(textFieldDescriptor == null && !DesignMode)\r
93                                         throw new HttpException(HttpRuntime.FormatResourceString("Field_Not_Found", DataTextField));\r
94                         }\r
95                         string text;\r
96                         if(textFieldDescriptor != null)\r
97                         {\r
98                                 text = FormatDataTextValue(textFieldDescriptor.GetValue(item));\r
99                         } else\r
100                         {\r
101                                 text = "Sample_DataBound_Text";\r
102                         }\r
103                         if(ctrl is LinkButton)\r
104                         {\r
105                                 ((LinkButton)ctrl).Text = text;\r
106                         }\r
107                         else\r
108                         {\r
109                                 ((Button)ctrl).Text = text;\r
110                         }\r
111                 }\r
112                 \r
113                 protected virtual string FormatDataTextValue(object dataTextValue)\r
114                 {\r
115                         string retVal = null;\r
116                         if(dataTextValue != null)\r
117                         {\r
118                                 if(DataTextFormatString.Length > 0)\r
119                                 {\r
120                                         retVal = String.Format(DataTextFormatString, dataTextValue);\r
121                                 }\r
122                                 else\r
123                                 {\r
124                                         retVal = dataTextValue.ToString();\r
125                                 }\r
126                         }\r
127                         return retVal;\r
128                 }
129
130                 // LAMESPEC The framework uses Description values for metadata here. However they should be WebSysDescriptions
131                 // because all metadata in this namespace has WebSysDescriptions\r
132
133                 [DefaultValue (typeof (ButtonColumnType), "LinkButton"), WebCategory ("Misc")]
134                 [Description ("The type of button used in this column.")]\r
135                 public virtual ButtonColumnType ButtonType\r
136                 {\r
137                         get\r
138                         {\r
139                                 object o = ViewState["ButtonType"];\r
140                                 if(o!=null)\r
141                                         return (ButtonColumnType)o;\r
142                                 return ButtonColumnType.LinkButton;\r
143                         }\r
144                         set\r
145                         {\r
146                                 if(!System.Enum.IsDefined(typeof(ButtonColumnType), value))\r
147                                         throw new ArgumentException();\r
148                                 ViewState["ButtonType"] = value;\r
149                         }\r
150                 }\r
151
152                 [DefaultValue (""), WebCategory ("Misc")]
153                 [Description ("The command assigned to this column.")]\r
154                 public virtual string CommandName\r
155                 {\r
156                         get\r
157                         {\r
158                                 string cn = (string)ViewState["CommandName"];\r
159                                 if(cn!=null)\r
160                                         return cn;\r
161                                 return String.Empty;\r
162                         }\r
163                         set\r
164                         {\r
165                                 ViewState["CommandName"] = value;\r
166                         }\r
167                 }\r
168
169                 [DefaultValue (""), WebCategory ("Misc")]
170                 [Description ("The datafield that is bound to the text property.")]\r
171                 public virtual string DataTextField\r
172                 {\r
173                         get\r
174                         {\r
175                                 string dtf = (string)ViewState["DataTextField"];\r
176                                 if(dtf!=null)\r
177                                         return dtf;\r
178                                 return String.Empty;\r
179                         }\r
180                         set\r
181                         {\r
182                                 ViewState["DataTextField"] = value;\r
183                         }\r
184                 }\r
185
186                 [DefaultValue (""), WebCategory ("Misc")]
187                 [Description ("A format that is applied to the bound text property.")]\r
188                 public virtual string DataTextFormatString\r
189                 {\r
190                         get\r
191                         {\r
192                                 string dtfs = (string)ViewState["DataTextFormatString"];\r
193                                 if(dtfs!=null)\r
194                                         return dtfs;\r
195                                 return String.Empty;\r
196                         }\r
197                         set\r
198                         {\r
199                                 ViewState["DataTextFormatString"] = value;\r
200                         }\r
201                 }\r
202
203                 [DefaultValue (""), WebCategory ("Misc")]
204                 [Description ("The text used for this button.")]\r
205                 public virtual string Text\r
206                 {\r
207                         get\r
208                         {\r
209                                 string text = (string)ViewState["Text"];\r
210                                 if(text!=null)\r
211                                         return text;\r
212                                 return String.Empty;\r
213                         }\r
214                         set\r
215                         {\r
216                                 ViewState["Text"] = value;\r
217                         }\r
218                 }\r
219         }\r
220 }\r