Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

I wanted to have hyperlink next to image  that when clicked, will open the same URL.

The standard WebControls.HyperLink  shows text or image, but not both.

So I decided to create custom control.

Image can be at the left or at the right of the HyperLink that can be specified by ImageAlign property.

See  a source code for the custom control below:

using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

using System.Web.UI.Design; //!!!System.Design (in System.Design.dll)

using System.Drawing.Design;

using System.Collections.Specialized;//NameValueCollection Class 

using System.Diagnostics;

 

namespace FSWeb.CustomControls

{

//TODO should I use ViewState and also different names for Text and ImageUrl

      ///

      /// HyperLinkWithImage -hyperlink next to image that when clicked, will open the same URL.

      ///

      [DefaultProperty("Text"),

            ToolboxData("<{0}:HyperLinkWithImage runat=server>")]

      public class HyperLinkWithImage : System.Web.UI.WebControls.HyperLink 

      { //,IPostBackDataHandler

            private string m_ImageUrl;

            private ImageAlign m_ImageAlign;

            //private string m_text;use ViewState instead

            public HyperLinkWithImage() : base()

            {

            }

 

            [Bindable(true),

                  Category("Appearance"),

                  DefaultValue(""),

            EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor))]

            public  new string ImageUrl  //Shadows base hyperlink ImageUrl that will be null

            {

                  get

                  {

                        return m_ImageUrl;

                  }

 

                  set

                  { 

                        m_ImageUrl = value;

                  }

            }

            //WebCategory("Layout")-internal in System.Web

            //WebSysDescription("Image_ImageAlign")-internal in System.Web

            [ Bindable(true), Category("Layout"), DefaultValue(0)]

            public virtual ImageAlign ImageAlign

            {

                  get

                  {

                        object obj1 = m_ImageAlign ;//this.ViewState["ImageAlign"];

                        if (obj1 != null)

                        {

                              return ((ImageAlign) obj1);

                        }

                        return ImageAlign.NotSet;

                  }

                  set

                  {

                        if ((value < ImageAlign.NotSet) || (value > ImageAlign.TextTop))

                        {

                              throw new ArgumentOutOfRangeException("value");

                        }

                        m_ImageAlign= value;// this.ViewState["ImageAlign"] = value;

                  }

            }

            [Bindable(true),

            Category("Appearance"),

            DefaultValue("")

            ]

            public  new string Text  //Shadows base hyperlink Text that will be null

            {

                  get

                  {

                        object obj1 = this.ViewState["TextValue"];

                        if (obj1 != null)

                        {

                              return (string) obj1;

                        }

                        return string.Empty;

                  }

                  set

                  { 

                        this.ViewState["TextValue"] = value;

                  }

            }

 

            ///

            /// Overrides the OnPreRender method.

            ///

            protected override void OnPreRender(System.EventArgs e)

            {

                  base.OnPreRender(e);

            }

 

            ///

            /// Render this control to the output parameter specified.

            ///

            /// The HTML writer to write out to

            protected override void Render(HtmlTextWriter output)

            {

                  string sUrl, sImg=null;

                  if (!DataHelper.IsNullOrEmpty(m_ImageUrl))

                  {    

                        sUrl=this.ResolveUrl(m_ImageUrl);//to resolve '~' can't be called in construction time

                        sImg="<IMG alt=\"\" src=\"" + sUrl + "\" border=\"0\" >";

                  }

                  bool bImageAlignNotSet=(ImageAlign.NotSet == m_ImageAlign);

                  bool bImageRight=(ImageAlign.Right== m_ImageAlign);

                  if ( bImageAlignNotSet) //

                  {//Standard HyperLink

                        base.Text= this.Text;

                        base.ImageUrl= this.ImageUrl;

                  }

                  else if (null!=sImg)

                  {

                        if(!bImageRight)

                        {    

                              base.Text= sImg+this.Text;

                        }

                        else

                        {

                              base.Text= this.Text+sImg;

                        }

                  }

                  base.Render(output);

            }

 

      }

}

  Update: The control refers to DataHelper class, posted here

Posted on Monday, November 7, 2005 6:30 AM | Back to top


Comments on this post: ASP.NET custom control HyperLinkWithImage

# re: ASP.NET custom control HyperLinkWithImage
Requesting Gravatar...
Link to Your blog is bookmarked!!!!

nice stuff!!

keep it up!
Left by Royya on Oct 11, 2011 2:07 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net