Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Loading image at run time in a custom control used in a page. How to code this?

Loading image at run time in a custom control used in a page. How to code this?

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
10 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Frankidoze
    wrote on last edited by
    #1

    Well here is what I have: I have a custom control MyCC deriving WebControl composed of an Image control and a Label control. This custom control is defined in an assembly and used by an aspx. Here is what I want to do: I want to set the Image url at run time in the Render() method of MyCC. How am I supposed to code that? Setting the ImageUrl property of the Image with "~/Images/myImage.gif" doesn't work. My assembly is defined like this: Project Name: MyProject Class component: MyProject.cs Namespace: MyNameSpace Class: MyCC All images are stored in a folder: Images under MyProject. Thanks for your help. PS: Tell me if I'm in the wrong forum. :)

    "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

    P G F 4 Replies Last reply
    0
    • F Frankidoze

      Well here is what I have: I have a custom control MyCC deriving WebControl composed of an Image control and a Label control. This custom control is defined in an assembly and used by an aspx. Here is what I want to do: I want to set the Image url at run time in the Render() method of MyCC. How am I supposed to code that? Setting the ImageUrl property of the Image with "~/Images/myImage.gif" doesn't work. My assembly is defined like this: Project Name: MyProject Class component: MyProject.cs Namespace: MyNameSpace Class: MyCC All images are stored in a folder: Images under MyProject. Thanks for your help. PS: Tell me if I'm in the wrong forum. :)

      "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      The ImageUrl property of the Image control sets the url for the image to be displayed. So you need to add a property to your custom control for getting/setting the image url that internally maps to this property.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      F 1 Reply Last reply
      0
      • P pmarfleet

        The ImageUrl property of the Image control sets the url for the image to be displayed. So you need to add a property to your custom control for getting/setting the image url that internally maps to this property.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        F Offline
        F Offline
        Frankidoze
        wrote on last edited by
        #3

        pmarfleet wrote:

        So you need to add a property to your custom control for getting/setting the image url that internally maps to this property

        Well, the Image is not set by the user but at render time because the image to set depends on different conditions. What I need to know is how to build the URL dynamically in the Render() method so it would work wherever the control is used. I'm looking for something like:

            protected override void Render(HtmlTextWriter writer)
            {
                this.\_ico.ImageUrl = "imageUrl.gif";
                this.\_ico.RenderControl(writer);
            }
        

        I just need to know how to build a correct URL as the image is embed in the custom control dll. Thank you for your answers.

        "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

        P 1 Reply Last reply
        0
        • F Frankidoze

          pmarfleet wrote:

          So you need to add a property to your custom control for getting/setting the image url that internally maps to this property

          Well, the Image is not set by the user but at render time because the image to set depends on different conditions. What I need to know is how to build the URL dynamically in the Render() method so it would work wherever the control is used. I'm looking for something like:

              protected override void Render(HtmlTextWriter writer)
              {
                  this.\_ico.ImageUrl = "imageUrl.gif";
                  this.\_ico.RenderControl(writer);
              }
          

          I just need to know how to build a correct URL as the image is embed in the custom control dll. Thank you for your answers.

          "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          I don't understand what you mean. In order to display an image, you need to specify the url of the image. The images would need to be stored as image files on your web server.

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          F A 2 Replies Last reply
          0
          • F Frankidoze

            Well here is what I have: I have a custom control MyCC deriving WebControl composed of an Image control and a Label control. This custom control is defined in an assembly and used by an aspx. Here is what I want to do: I want to set the Image url at run time in the Render() method of MyCC. How am I supposed to code that? Setting the ImageUrl property of the Image with "~/Images/myImage.gif" doesn't work. My assembly is defined like this: Project Name: MyProject Class component: MyProject.cs Namespace: MyNameSpace Class: MyCC All images are stored in a folder: Images under MyProject. Thanks for your help. PS: Tell me if I'm in the wrong forum. :)

            "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Frankidoze wrote:

            Setting the ImageUrl property of the Image with "~/Images/myImage.gif" doesn't work.

            That should work. What do you mean when you say that it "doesn't work"? What does it do, and how does that differ from what you expect? Do you get any error message?

            Experience is the sum of all the mistakes you have done.

            F 1 Reply Last reply
            0
            • G Guffa

              Frankidoze wrote:

              Setting the ImageUrl property of the Image with "~/Images/myImage.gif" doesn't work.

              That should work. What do you mean when you say that it "doesn't work"? What does it do, and how does that differ from what you expect? Do you get any error message?

              Experience is the sum of all the mistakes you have done.

              F Offline
              F Offline
              Frankidoze
              wrote on last edited by
              #6

              It doesn't work because the image is not loaded when I run the page. There is no error. It shows the traditional "image missing" image :). The situation is this: My custom control MyCC is a class (it's not a user control) compiled into a dll. All images I need to use for MyCC are defined in a folder named Images. So I guess they ended up in the dll somehow. My page uses MyCC. When MyCC is rendered, the image set for the Image control is not found because their is no such file on my web server. In order to access those images from the page I guess they need to be extracted from the dll. I want to defined those images with my class so wherever you use the control no extra step need to be done. Just drag the CC from the tool bar onto your page. I guess at some point I need to manage those images as resources. I hope my explanations will help you to help me. :)

              "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

              1 Reply Last reply
              0
              • P pmarfleet

                I don't understand what you mean. In order to display an image, you need to specify the url of the image. The images would need to be stored as image files on your web server.

                Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                F Offline
                F Offline
                Frankidoze
                wrote on last edited by
                #7

                pmarfleet wrote:

                The images would need to be stored as image files on your web server.

                I understand that. But how to do so when the images are defined with the custom control (within the custom control project). Do I need to use a resource file?

                "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

                1 Reply Last reply
                0
                • F Frankidoze

                  Well here is what I have: I have a custom control MyCC deriving WebControl composed of an Image control and a Label control. This custom control is defined in an assembly and used by an aspx. Here is what I want to do: I want to set the Image url at run time in the Render() method of MyCC. How am I supposed to code that? Setting the ImageUrl property of the Image with "~/Images/myImage.gif" doesn't work. My assembly is defined like this: Project Name: MyProject Class component: MyProject.cs Namespace: MyNameSpace Class: MyCC All images are stored in a folder: Images under MyProject. Thanks for your help. PS: Tell me if I'm in the wrong forum. :)

                  "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

                  F Offline
                  F Offline
                  Frankidoze
                  wrote on last edited by
                  #8

                  :)

                  "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

                  1 Reply Last reply
                  0
                  • F Frankidoze

                    Well here is what I have: I have a custom control MyCC deriving WebControl composed of an Image control and a Label control. This custom control is defined in an assembly and used by an aspx. Here is what I want to do: I want to set the Image url at run time in the Render() method of MyCC. How am I supposed to code that? Setting the ImageUrl property of the Image with "~/Images/myImage.gif" doesn't work. My assembly is defined like this: Project Name: MyProject Class component: MyProject.cs Namespace: MyNameSpace Class: MyCC All images are stored in a folder: Images under MyProject. Thanks for your help. PS: Tell me if I'm in the wrong forum. :)

                    "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

                    F Offline
                    F Offline
                    Frankidoze
                    wrote on last edited by
                    #9

                    Here is the solution to How to dynamically load images within a custom control with images embedded with it! Context: You have a project of type "Class Library" where you create your custom control MyCC. 1/ Add a new item: Resources File: Resources.resx 2/ Drag your images onto this resource file you had open. This will generate a sub folder named Resources containing all images dragged. 3/ Under "Solution Explorer", select each image file to change their "Build Action" property to "Embedded Resource". 4/ Set the image url property as follow in the overrided Render() or RenderControl() method depending which one you are using to generate the HTML of your custom control:

                    this._myImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyNameSpace.Resources.MyImage.gif");

                    The second param is of structure: [Namespace].[Resources Folder].[Resource File Name] 5/!!!! Carefull, the Namespace above and below has to match the default one defined in the project properties (Tab "Application"). 6/ Last, either in your custom control .cs file or in your AssemblyInfo.cs, add this line for each image: [assembly: System.Web.UI.WebResource("MyNameSpace.Resources.MyImage.gif", "image/gif")] Conclusion: Like this any page using your custom control will be able to display any image declared in your custom control. Note: If you look at your page source when you run it, the src attribute of your image uses a WebResource.axd. Enjoy. If I have missed anything, I apologize.

                    "Nothing is lost, Nothing is created, Everything is transformed" Lavoisier

                    1 Reply Last reply
                    0
                    • P pmarfleet

                      I don't understand what you mean. In order to display an image, you need to specify the url of the image. The images would need to be stored as image files on your web server.

                      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                      A Offline
                      A Offline
                      ahmet ureten
                      wrote on last edited by
                      #10

                      Firstly, Must be clear Url like; Image.Url = ""; After that initialize image location, like; Image.Url = "LOCATION";

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups