| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- //
- // System.Web.UI.WebControls.HyperLinkColumn.cs
- //
- // Authors:
- // Gaurav Vaish ([email protected])
- // Gonzalo Paniagua Javier ([email protected])
- // Andreas Nahr ([email protected])
- //
- // (c) 2002 Ximian, Inc. (http://www.ximian.com)
- // (C) Gaurav Vaish (2002)
- // (C) 2003 Andreas Nahr
- //
- using System;
- using System.ComponentModel;
- using System.Web;
- using System.Web.UI;
- namespace System.Web.UI.WebControls
- {
- public class HyperLinkColumn: DataGridColumn
- {
- PropertyDescriptor textFieldDescriptor;
- PropertyDescriptor urlFieldDescriptor;
- public HyperLinkColumn ()
- {
- }
- [DefaultValue (""), WebCategory ("Misc")]
- [WebSysDescription ("The field that gets data-bound to the NavigateUrl.")]
- public virtual string DataNavigateUrlField {
- get {
- object o = ViewState ["DataNavigateUrlField"];
- if (o != null)
- return (string) o;
- return String.Empty;
- }
- set {
- ViewState ["DataNavigateUrlField"] = value;
- OnColumnChanged ();
- }
- }
- // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
- [DefaultValue (""), WebCategory ("Misc")]
- [Description ("The formatting rule for the text content that gets data-bound to the NavigateUrl.")]
- public virtual string DataNavigateUrlFormatString {
- get {
- object o = ViewState ["DataNavigateUrlFormatString"];
- if (o != null)
- return (string) o;
- return String.Empty;
- }
- set {
- ViewState ["DataNavigateUrlFormatString"] = value;
- OnColumnChanged ();
- }
- }
- [DefaultValue (""), WebCategory ("Misc")]
- [WebSysDescription ("The field that gets data-bound to the Text property.")]
- public virtual string DataTextField {
- get {
- object o = ViewState ["DataTextField"];
- if (o != null)
- return (string) o;
- return String.Empty;
- }
- set {
- ViewState ["DataTextField"] = value;
- OnColumnChanged ();
- }
- }
- // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
- [DefaultValue (""), WebCategory ("Misc")]
- [Description ("The formatting rule for the text content that gets data-bound to the Text property.")]
- public virtual string DataTextFormatString {
- get {
- object o = ViewState ["DataTextFormatString"];
- if (o != null)
- return (string) o;
- return String.Empty;
- }
- set {
- ViewState ["DataTextFormatString"] = value;
- OnColumnChanged ();
- }
- }
- [DefaultValue (""), WebCategory ("Misc")]
- [WebSysDescription ("The URL that this hyperlink links to.")]
- public virtual string NavigateUrl {
- get {
- object o = ViewState ["NavigateUrl"];
- if (o != null)
- return (string) o;
- return String.Empty;
- }
- set {
- ViewState ["NavigateUrl"] = value;
- OnColumnChanged ();
- }
- }
- [DefaultValue (""), WebCategory ("Misc")]
- [WebSysDescription ("The target frame in which the NavigateUrl property should be opened.")]
- public virtual string Target {
- get {
- object o = ViewState ["Target"];
- if (o != null)
- return (string) o;
- return String.Empty;
- }
- set {
- ViewState ["Target"] = value;
- OnColumnChanged ();
- }
- }
- [DefaultValue (""), WebCategory ("Misc")]
- [WebSysDescription ("The Text for the hyperlink.")]
- public virtual string Text {
- get {
- object o = ViewState ["Text"];
- if (o != null)
- return (string) o;
- return String.Empty;
- }
- set {
- ViewState ["Text"] = value;
- OnColumnChanged ();
- }
- }
- public override void Initialize ()
- {
- textFieldDescriptor = null;
- urlFieldDescriptor = null;
- base.Initialize ();
- }
- public override void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)
- {
- base.InitializeCell (cell, columnIndex, itemType);
- if (itemType != ListItemType.Header && itemType != ListItemType.Footer) {
- HyperLink toDisplay = new HyperLink ();
- toDisplay.Text = Text;
- toDisplay.NavigateUrl = NavigateUrl;
- toDisplay.Target = Target;
- if (DataTextField.Length > 0 || DataNavigateUrlField.Length > 0)
- toDisplay.DataBinding += new EventHandler (OnDataBindHyperLinkColumn);
- cell.Controls.Add (toDisplay);
- }
- }
- private void OnDataBindHyperLinkColumn (object sender, EventArgs e)
- {
- HyperLink link = (HyperLink) sender;
- object item = ((DataGridItem) link.NamingContainer).DataItem;
- if (textFieldDescriptor == null && urlFieldDescriptor == null) {
- PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (item);
- textFieldDescriptor = properties.Find (DataTextField, true);
- if (textFieldDescriptor == null && !DesignMode)
- throw new HttpException (HttpRuntime.FormatResourceString (
- "Field_Not_Found", DataTextField));
- urlFieldDescriptor = properties.Find (DataNavigateUrlField, true);
- if (urlFieldDescriptor == null && !DesignMode)
- throw new HttpException (HttpRuntime.FormatResourceString (
- "Field_Not_Found", DataNavigateUrlField));
- }
- if (textFieldDescriptor != null) {
- link.Text = FormatDataTextValue (textFieldDescriptor.GetValue (item));
- } else {
- link.Text = "Sample_DataBound_Text";
- }
- if (urlFieldDescriptor != null) {
- link.NavigateUrl = FormatDataNavigateUrlValue (urlFieldDescriptor.GetValue (item));
- } else {
- link.NavigateUrl = "url";
- }
- }
- protected virtual string FormatDataNavigateUrlValue (object dataUrlValue)
- {
- if (dataUrlValue == null)
- return String.Empty;
- string retVal;
- if (DataNavigateUrlFormatString.Length > 0) {
- retVal = String.Format (DataNavigateUrlFormatString, dataUrlValue);
- } else {
- retVal = dataUrlValue.ToString ();
- }
- return retVal;
- }
- protected virtual string FormatDataTextValue (object dataTextValue)
- {
- if (dataTextValue == null)
- return String.Empty;
- string retVal;
- if (DataTextFormatString.Length > 0) {
- retVal = String.Format (DataTextFormatString, dataTextValue);
- } else {
- retVal = dataTextValue.ToString ();
- }
- return retVal;
- }
- }
- }
|