locked
Custom Control, Properties, URL Selection, Path Problems RRS feed

  • Question

  • User-1631963038 posted

    I'm working with a Custom Control that has the following property:

            /// <summary>
            /// Image url
            /// </summary>
            [Bindable(true), Browsable(true), Category("Appearance"), DefaultValue(""), Localizable(true),
            RefreshProperties(RefreshProperties.Repaint),
            [UrlProperty]
            EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
            public string ImgURL
            {
                get
                {
                    if (ViewState["ImgURL"] != null)
                        return ViewState["ImgURL"].ToString();
                    return "";
                }
                set
                {
                    ViewState["ImgURL"] = value;
                }
            }

    I am currently using this control inside an web page aspx file that is in a subdirectory off of my web site root directory.  I am having a problem with using the property editor to select the image.    In the source, I position the mouse over the control code in the ASPX file.  I then click F4 to bring up Visual Studio's Properties window.  When I try to edit ImgURL property, I click the button with the eclipses to the right of the edit box for the property.  This brings up the "Select Project Item" dialog box.  When I select the image and click OK it does not include "~/" in front of the path when it is supposed to.  Also, I put "~/" in front of the path and then re-edit the value, it does really strange things. 

     It seems to me the ImageUrlEditor should be smarter about path locations.  It this the correct behavior?  My hope is there some switch to fix this.  I do not want to write my own custom class derived from ImageUrlEditor if I can help it.  It should figure out the path relative to the control's parent container.  Instead of "~/images/MyImage.jpg" maybe it should be "../images/MyImage.jpg".  What's the point of the dialog box if you still have to edit the value it selects.

    Monday, July 7, 2008 10:04 PM

All replies

  • User-1407570774 posted

    You must use this.ResolveClientUrl(), where you want to assign the ImgUrl. Visit code below:

    .
    .
    .
    writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ResolveClientUrl(this.ImgUrl));
    writer.RenderBeginTag(HtmlTextWriterTag.Img);
    writer.RenderEndTag();
    .
    .
    .
    Hope this helps
    Tuesday, July 8, 2008 2:59 AM
  • User-1631963038 posted

    THanks for the post but my problem is on the design side.  It looks like <asp:HyperLink> from the Toolbox has the same the problem when selecting NavigateURL.  If the aspx page having the <asp:HyperLink> is in a subdirectory the URL picking dialog box just doesn't work right.  The parent container is in a subdirectory.  When you pick the URL it should put "../" in front of the URL to correctly account for the parent container's subdirectory.  Otherwise the URL is not found. 

    Tuesday, July 8, 2008 8:33 AM
  • User-1631963038 posted

    Here's a link showing how to invoke the UrlEditor at design time that I am talking about.  Of course, it's not the custom editor shown in the pick but the one used by <asp:HyperLink>

    http://books.google.com/books?id=DUwx_H3G0AEC&pg=PA283&lpg=PA283&dq=%2B+UrlEditor+%2B+property+%2B+C%23&source=web&ots=75Uc5dZT30&sig=Lt9auznVGhuDLgU-VBBx3QHNvj0&hl=en&sa=X&oi=book_result&resnum=3&ct=result#PPA283,M1

    Tuesday, July 8, 2008 9:40 AM
  • User-1631963038 posted

    I don't think there's anyway around this other than manually adding the parent container's path, that is "../" for one subdirectory down.  Pretty lame if this is it.  I will keep looking.

    Tuesday, July 8, 2008 5:36 PM
  • User-16411453 posted

     

    You have to add the references to your server control project.(Add Reference) At the top of solution explorer, you can click the show all button to expose the references.

    System.Design

    System.Drawing

    System.Web.Extensions

      

    <Bindable(True)> _
    <Category("Data")> _
    <DefaultValue("")> _
    <Localizable(True)> _
    <RefreshProperties(RefreshProperties.Repaint)> _
    <EditorAttribute(GetType(System.Web.UI.Design.UrlEditor), GetType(UITypeEditor))> _
    Property DATA_Countries_XML() As String
    Get
    Dim
    _DATA_Countries_XML As String = CStr(ViewState("DATA_Countries_XML"))
    If _DATA_Countries_XML Is Nothing Then
    Return String
    .Empty
    Else
    Return
    _DATA_Countries_XML
    End If
    End Get

    Set
    (ByVal Value As String)
    ViewState("DATA_Countries_XML") = Value
    End Set

    End Property
     
     
    /// <summary>
            /// Image url
            /// </summary>
            [Bindable(true), Browsable(true), Category("Appearance"), DefaultValue(""), Localizable(true),
            RefreshProperties(RefreshProperties.Repaint),
            [UrlProperty]
            EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
            public string ImgURL
            {
                get
                {
                    if (ViewState["ImgURL"] != null)
                        return ViewState["ImgURL"].ToString();
                    return "";
                }
                set
                {
                    ViewState["ImgURL"] = value;
                }
            }
      
     
    Tuesday, July 8, 2008 7:15 PM
  • User-1631963038 posted

    jkirkerx,

    I'm not sure what you are responding to in your post.  The problem is not getting the ImageUrlEditor class to work.  The problem is it does not work the way I think it should work.  My criticism is it does not add the path information relative to the control's parent container.  So if you use ImageUrlEditor to select an image to use for an image property if the parent container is in a subdirectory then the selected image will not be found when you view the page at run time.  This is because ImageUrlEditor does not add "../" in front of the selected image to take into account the parent container's location in the web site's directory structure.  I think this is a bug with the ImageUrlEditor class because what's the point of using it if it allows the user to select images that are not found at runtime!  By not adding "../" in front of the selected image then at runtime the image is being looked for is at a subdirectory relative to the parent container's directory location.  This results in an error because the image is not found.

    Monday, July 14, 2008 11:06 AM
  • User-16411453 posted

    It correctly adds the path url to the property.  ~/path/image.gif.

    When the server sees the "~/path" in the webform, it automatically calculates and generates the proper format such as "../path" in the XHTML or HTML sent to the browser.

    This is the web output from firefox. This is the proper operation of the the editor and server output.

    <img src="../../images/Label-Wishlist-670.gif" style="border-width:0px;" />
     
    Monday, July 14, 2008 1:37 PM
  • User-1631963038 posted

    This has more to do with what Visual Studio is doing at design time.  It does not matter if the UrlEditor inserts "~/path/..." or "../../path...".  It doesn't insert anything which is just wrong.  You have to manually add the "~/". 

    If the "~/" or "../" is present it will sort of behave correctly.  But if the property is blank, and you use the UrlEditor, it does not include the path information correctly.   

    Say you have the following structure:

    /test1/Default.aspx

    /images/image1.jpg

    And you have the following code in your test1/Default.aspx:

    <asp:image ID="Image1" runat="server" />

    You position the mouse within <asp:image /> tag area, you click on F4.  The properties window comes up.  You select ImageUrl property.  A button with eclipses shows up to the right of the edit box.  You click on the button, you navigate over to the "images" folder, select image1.jpg, click okay, you will see the following:

    <asp:image ID="Image1" runat="server" ImageUrl="images/image1.jpg" />

    Immediately within Visual Studio it puts green squigglely lines underneath the image URI because it is not found.  This is because the image "/test1/images/image1.jpg" does not exist.  I think what the UrlEditor should be doing is the following:

    <asp:image ID="Image1" runat="server" ImageUrl="../images/image1.jpg" />

    or

    <asp:image ID="Image1" runat="server" ImageUrl="~/images/image1.jpg" />

    Either way as long as it doesn't do what it currently does!!!!!!!   I don't know how to state the problem in simpler terms.  The same is true for NavigateUrl in the <asp:HyperLink> tag.

    For the record squiggly or squigglely is a very difficult word to spell!

    Wednesday, July 16, 2008 2:49 PM
  • User-16411453 posted

    I don't have that problem.  Maybe I did along time ago, and I found a work around for it and forgot about it.  Years ago, I ended creating about 8 2 gig extemded partitions with drive letters.  I placed each project that I currently was working on seperate drive partitions and labeled the drives with project names. This fixed all of my problems regarding file and folder locations, and rendered correctly when running debug, or in a browser for testing.  Never had the problem in DreamWeaver, it always worked no matter where the project was.

    When I render in a local browser, I set IIS home folder to the current drive I'm working on, and flush the cache in the browser.

     

    Wednesday, July 16, 2008 3:31 PM
  • User-1631963038 posted

    I am using Visual Studio 2008.

    Tuesday, July 22, 2008 10:18 PM