Oct 06 2006

Using ~ in your server controls

Posted by admin under Controls

ASP.NET somehows manage (for its server controls) to replace '~'  with the current application root - and I needed the same thing for a server control I'm currently crafting.

By using the control.ResolveUrl it could be done. Consider this property



        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PathToCalendarScriptDir
        {
            get
            {
                String s = (String)ViewState["PathToCalendarScriptDir"];
                return ((s == null) ? "~/caldir/" : s);
            }

            set
            {
                ViewState["PathToCalendarScriptDir"] = value;
            }
        }


Now, when we need the real value ( it might be ~/caldir/ as default, or it might be any type of relative/absolute path the control user has specified) we can call control.ResolveUrl to get the path expanded to the applicationroot.