Create method to handle multiple types of controls

Posted by Praesagus on Stack Overflow See other posts from Stack Overflow or by Praesagus
Published on 2010-03-30T23:26:02Z Indexed on 2010/03/30 23:33 UTC
Read the original article Hit count: 358

Filed under:
|
|
|

I am trying to create a method that accepts multiple types of controls - in this case Labels and Panels. The conversion does not work because IConvertible doesn't convert these Types. Any help would be so appreciated. Thanks in advance

public void LocationsLink<C>(C control)
    {
        if (control != null)
        {
            WebControl ctl = (WebControl)Convert.ChangeType(control, typeof(WebControl));
            Literal txt = new Literal();
            HyperLink lnk = new HyperLink();
            txt.Text = "If you prefer a map to the nearest facility please ";
            lnk.Text = "click here";
            lnk.NavigateUrl = "/content/Locations.aspx";
            ctl.Controls.Add(txt);
            ctl.Controls.Add(lnk);
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET