File: HyperLinkField.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (425 lines) | stat: -rw-r--r-- 15,525 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//------------------------------------------------------------------------------
// <copyright file="HyperLinkField.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls {

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Globalization;
    using System.Web.Util;


    /// <devdoc>
    /// <para>Creates a field within the <see cref='System.Web.UI.WebControls.DataBoundControl'/> containing hyperlinks that
    ///    navigate to specified URLs.</para>
    /// </devdoc>
    public class HyperLinkField : DataControlField {

        private PropertyDescriptor textFieldDesc;
        private PropertyDescriptor[] urlFieldDescs;


        /// <devdoc>
        /// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.HyperLinkField'/> class.</para>
        /// </devdoc>
        public HyperLinkField() {
        }


        /// <devdoc>
        /// <para>Gets or sets the fields in the DataSource that provides the URL of the page to navigate to.</para>
        /// </devdoc>
        [
        WebCategory("Data"),
        DefaultValue(null),
        Editor("System.Web.UI.Design.WebControls.DataFieldEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        TypeConverterAttribute(typeof(StringArrayConverter)),
        WebSysDescription(SR.HyperLinkField_DataNavigateUrlFields)
        ]
        public virtual string[] DataNavigateUrlFields {
            get {
                object o = ViewState["DataNavigateUrlFields"];
                if (o != null) {
                    return(string[])((string[])o).Clone();
                }

                return new string[0];
            }
            set {
                string[] oldValue = ViewState["DataNavigateUrlFields"] as string[];
                if (!StringArraysEqual(oldValue, value)) {
                    if (value != null) {
                        ViewState["DataNavigateUrlFields"] = (string[])value.Clone();
                    }
                    else {
                        ViewState["DataNavigateUrlFields"] = null;
                    }
                    OnFieldChanged();
                }
            }
        }

        /// <devdoc>
        /// <para>Gets or sets the formatting applied to the <see cref='System.Web.UI.WebControls.HyperLinkField.NavigateUrl'/>
        /// property.</para>
        /// </devdoc>
        [
        WebCategory("Data"),
        DefaultValue(""),
        WebSysDescription(SR.HyperLinkField_DataNavigateUrlFormatString)
        ]
        public virtual string DataNavigateUrlFormatString {
            get {
                object o = ViewState["DataNavigateUrlFormatString"];
                if (o != null)
                    return(string)o;
                return String.Empty;
            }
            set {
                if (!String.Equals(value, ViewState["DataNavigateUrlFormatString"])) {
                    ViewState["DataNavigateUrlFormatString"] = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        /// <para>Gets or sets the field in the DataSource that will be used as the source of
        /// data for the <see cref='System.Web.UI.WebControls.HyperLinkField.Text'/> property.</para>
        /// </devdoc>
        [
        WebCategory("Data"),
        DefaultValue(""),
        TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, " + AssemblyRef.SystemDesign),
        WebSysDescription(SR.HyperLinkField_DataTextField)
        ]
        public virtual string DataTextField {
            get {
                object o = ViewState["DataTextField"];
                if (o != null)
                    return(string)o;
                return String.Empty;
            }
            set {
                if (!String.Equals(value, ViewState["DataTextField"])) {
                    ViewState["DataTextField"] = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        /// <para>Gets or sets the formatting applied to the <see cref='System.Web.UI.WebControls.HyperLinkField.Text'/>
        /// property.</para>
        /// </devdoc>
        [
        WebCategory("Data"),
        DefaultValue(""),
        WebSysDescription(SR.HyperLinkField_DataTextFormatString)
        ]
        public virtual string DataTextFormatString {
            get {
                object o = ViewState["DataTextFormatString"];
                if (o != null)
                    return(string)o;
                return String.Empty;
            }
            set {
                if (!String.Equals(value, ViewState["DataTextFormatString"])) {
                    ViewState["DataTextFormatString"] = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the URL to navigate to when the hyperlink is clicked.</para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(""),
        Editor("System.Web.UI.Design.UrlEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        UrlProperty(),
        WebSysDescription(SR.HyperLinkField_NavigateUrl)
        ]
        public virtual string NavigateUrl {
            get {
                object o = ViewState["NavigateUrl"];
                if (o != null)
                    return(string)o;
                return String.Empty;
            }
            set {
                if (!String.Equals(value, ViewState["NavigateUrl"])) {
                    ViewState["NavigateUrl"] = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the window or target frame that is
        ///       used to display the contents resulting from the hyperlink.</para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(""),
        TypeConverter(typeof(TargetConverter)),
        WebSysDescription(SR.HyperLink_Target)
        ]
        public virtual string Target {
            get {
                object o = ViewState["Target"];
                if (o != null)
                    return(string)o;
                return String.Empty;
            }
            set {
                if (!String.Equals(value, ViewState["Target"])) {
                    ViewState["Target"] = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the text to display for the hyperlink.</para>
        /// </devdoc>
        [
        Localizable(true),
        WebCategory("Appearance"),
        DefaultValue(""),
        WebSysDescription(SR.HyperLinkField_Text)
        ]
        public virtual string Text {
            get {
                object o = ViewState["Text"];
                if (o != null)
                    return(string)o;
                return String.Empty;
            }
            set {
                if (!String.Equals(value, ViewState["Text"])) {
                    ViewState["Text"] = value;
                    OnFieldChanged();
                }
            }
        }

        protected override void CopyProperties(DataControlField newField) {
            ((HyperLinkField)newField).DataNavigateUrlFields = DataNavigateUrlFields;   //the getter and setter both call Clone
            ((HyperLinkField)newField).DataNavigateUrlFormatString = DataNavigateUrlFormatString;
            ((HyperLinkField)newField).DataTextField = DataTextField;
            ((HyperLinkField)newField).DataTextFormatString = DataTextFormatString;
            ((HyperLinkField)newField).NavigateUrl = NavigateUrl;
            ((HyperLinkField)newField).Target = Target;
            ((HyperLinkField)newField).Text = Text;
            base.CopyProperties(newField);
        }

        protected override DataControlField CreateField() {
            return new HyperLinkField();
        }


        /// <devdoc>
        /// </devdoc>
        protected virtual string FormatDataNavigateUrlValue(object[] dataUrlValues) {
            string formattedUrlValue = String.Empty;

            if ((dataUrlValues != null)) {
                string formatting = DataNavigateUrlFormatString;
                if (formatting.Length == 0) {
                    if (dataUrlValues.Length > 0 && !DataBinder.IsNull(dataUrlValues[0])) {
                        formattedUrlValue = dataUrlValues[0].ToString();
                    }
                }
                else {
                    formattedUrlValue = String.Format(CultureInfo.CurrentCulture, formatting, dataUrlValues);
                }
            }

            return formattedUrlValue;
        }


        /// <devdoc>
        /// </devdoc>
        protected virtual string FormatDataTextValue(object dataTextValue) {
            string formattedTextValue = String.Empty;

            if (!DataBinder.IsNull(dataTextValue)) {
                string formatting = DataTextFormatString;
                if (formatting.Length == 0) {
                    formattedTextValue = dataTextValue.ToString();
                }
                else {
                    formattedTextValue = String.Format(CultureInfo.CurrentCulture, formatting, dataTextValue);
                }
            }

            return formattedTextValue;
        }


        /// <devdoc>
        /// </devdoc>
        public override bool Initialize(bool enableSorting, Control control) {
            base.Initialize(enableSorting, control);
            textFieldDesc = null;
            urlFieldDescs = null;
            return false;
        }


        /// <devdoc>
        ///    <para>
        ///       Initializes the cell representing this field with the
        ///       contained hyperlink.</para>
        /// </devdoc>
        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) {
            base.InitializeCell(cell, cellType, rowState, rowIndex);

            if (cellType == DataControlCellType.DataCell) {
                HyperLink hyperLink = new HyperLink();

                hyperLink.Text = Text;
                hyperLink.NavigateUrl = NavigateUrl;
                hyperLink.Target = Target;

                if (((rowState & DataControlRowState.Insert) == 0) && Visible) {
                    if ((DataNavigateUrlFields.Length != 0) ||
                        (DataTextField.Length != 0)) {
                        hyperLink.DataBinding += new EventHandler(this.OnDataBindField);
                    }
    
                    cell.Controls.Add(hyperLink);
                }
            }
        }


        /// <devdoc>
        /// </devdoc>
        private void OnDataBindField(object sender, EventArgs e) {
            Debug.Assert((DataTextField.Length != 0) || (DataNavigateUrlFields.Length != 0),
                         "Shouldn't be DataBinding without a DataTextField and DataNavigateUrlField");

            HyperLink boundControl = (HyperLink)sender;
            Control controlContainer = boundControl.NamingContainer;
            object dataItem = null;


            if (controlContainer == null) {
                throw new HttpException(SR.GetString(SR.DataControlField_NoContainer));
            }

            // Get the DataItem from the container
            dataItem = DataBinder.GetDataItem(controlContainer);

            if (dataItem == null && !DesignMode) {
                throw new HttpException(SR.GetString(SR.DataItem_Not_Found));
            }

            if ((textFieldDesc == null) && (urlFieldDescs == null)) {

                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(dataItem);
                string fieldName;

                fieldName = DataTextField;
                if (fieldName.Length != 0) {
                    textFieldDesc = props.Find(fieldName, true);
                    if ((textFieldDesc == null) && !DesignMode) {
                        throw new HttpException(SR.GetString(SR.Field_Not_Found, fieldName));
                    }
                }

                string[] dataNavigateUrlFields = DataNavigateUrlFields;
                int dataNavigateUrlFieldsLength = dataNavigateUrlFields.Length;
                urlFieldDescs = new PropertyDescriptor[dataNavigateUrlFieldsLength];

                for (int i = 0; i < dataNavigateUrlFieldsLength; i++) {
                    fieldName = dataNavigateUrlFields[i];
                    if (fieldName.Length != 0) {
                        urlFieldDescs[i] = props.Find(fieldName, true);
                        if ((urlFieldDescs[i] == null) && !DesignMode) {
                            throw new HttpException(SR.GetString(SR.Field_Not_Found, fieldName));
                        }
                    }
                }
            }

            string dataTextValue = String.Empty;
            if (textFieldDesc != null && dataItem != null) {
                object data = textFieldDesc.GetValue(dataItem);
                dataTextValue = FormatDataTextValue(data);
            }
            if (DesignMode && (DataTextField.Length != 0) && dataTextValue.Length == 0) {
                dataTextValue = SR.GetString(SR.Sample_Databound_Text);
            }

            if (dataTextValue.Length > 0) {
                boundControl.Text = dataTextValue;
            }

            int urlFieldDescsLength = urlFieldDescs.Length;
            string dataNavValue = String.Empty;
            if (urlFieldDescs != null && urlFieldDescsLength > 0 && dataItem != null) {
                object[] data = new object[urlFieldDescsLength];

                for (int i = 0; i < urlFieldDescsLength; i++) {
                    if (urlFieldDescs[i] != null) {
                        data[i] = urlFieldDescs[i].GetValue(dataItem);
                    }
                }
                string urlValue = FormatDataNavigateUrlValue(data);
                if (!CrossSiteScriptingValidation.IsDangerousUrl(urlValue)) {
                    dataNavValue = urlValue;
                }
            }
            if (DesignMode && (DataNavigateUrlFields.Length != 0) && dataNavValue.Length == 0) {
                dataNavValue = "url";
            }

            if (dataNavValue.Length > 0) {
                boundControl.NavigateUrl = dataNavValue;
            }
        }

        private bool StringArraysEqual(string[] arr1, string[] arr2) {
            if (arr1 == null && arr2 == null) {
                return true;
            }
            if (arr1 == null || arr2 == null) {
                return false;
            }
            if (arr1.Length != arr2.Length) {
                return false;
            }
            for (int i = 0; i < arr1.Length; i++) {
                if (!String.Equals(arr1[i], arr2[i])) {
                    return false;
                }
            }
            return true;
        }
        
        /// <devdoc>
        /// <para>Override with an empty body if the field's controls all support callback.
        ///  Otherwise, override and throw a useful error message about why the field can't support callbacks.</para>
        /// </devdoc>
        public override void ValidateSupportsCallback() {
        }
    }
}