locked
Embedding Resources in ASP.NET 2.0 RRS feed

  • Question

  • User888441741 posted

    i have a embeded resource gif file in my composite control and when i add my control to the .aspx page then i dont see my image (design and run time)

    what i'm missing...

    here is what i have takens steps:

    i have added as a prefix to the filename of embedded resources

    [assembly: System.Web.UI.WebResource("myImage.gif", "img/gif")]

    protected override void OnInit(System.EventArgs e)

    {

    string includeTemplate = "<link rel='stylesheet' text='text/css' href='{0}' />";
    string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myStylesheet _Links.css");
    LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation));
    ((HtmlControls.HtmlHead) Page.Header).Controls.Add(include);


    base
    .OnInit(e);

    }

     


    here is my stylehsheet looks like:


     

    body

    {

    }

    .SearchButtonStyle

    {

    border-right: black 1px solid;

    border-top: black 1px solid;

    font-weight: bold;

    font-size: 9pt;

    background-image: url(myImage.gif); <<<<<

    border-left: black 1px solid;

    width: 19px;

    color: navy;

    border-bottom: black 1px solid;

    font-family: Arial;

    height: 22px;

    }

    and lastly, i have code in my .cs as like this:

    btnSearch = new HtmlInputButton();
    btnSearch.ID =
    "btnSearch";
    btnSearch.Attributes[
    "class"] = SearchButtonStyle;   <<<<<<<<<<<<<<<<


     when i run the page then i dont see the image?

     what else im missing?

    thanks.

    Tuesday, April 24, 2007 12:05 PM

Answers

  • User180615624 posted

    Not it is not correct, background-image: url(myImage.gif);  myImage.gif is not comming from resources this way.

    In stylesheet you cannot use such things, although there is concept of dynamic stylesheet supported by IE.
    Anyway, you should reference your embedded resouce from the code behind using Page.ClientScript.GetWebResouceUrl method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2007 4:25 PM
  • User-1121087500 posted

    mosessaur is correct, you cannot access the image that way.

    WebResources are just a way to create a url link to an embedded resource.  This url will be dynamically created, therefore it would not make any sense to try and reference it from a static location (your css file).

    To get the effect you are looking for you can do the following:

    btnSearch.Style[HtmlTextWriterStyle.BackgroundImage] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myImage.gif"); 

    Don't forget to include the Namespace and any folder names for your webresource.  eg. If your project name is Proj1 and myImage is in a folder called images, you need:

    [assembly: System.Web.UI.WebResource("Proj1.images.myImage.gif", "img/gif")]

    and the button would need the following 

    btnSearch.Style[HtmlTextWriterStyle.BackgroundImage] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Proj1.images.myImage.gif");

     

    Also, there is a control called Link for adding style sheets that you can use instead of a LiteralControl.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2007 5:13 PM

All replies

  • User180615624 posted

    What you have done is not applicable, your embedded resources cannot be referenced normaly by mentioning resouce name like how you did.
    You need to refere to the following links that will give you a good understanding of WebResourceAttribute and how to use it.

    WebResourceAttribute Class
    WebResource ASP.NET 2.0 explained
    Whats with System.Web.UI.WebResourceAttribute?
    System.Web.UI.WebResourceAttribute Issue
    http://forums.asp.net/thread/1677440.aspx

    Tuesday, April 24, 2007 3:39 PM
  • User888441741 posted

    What you have done is not applicable, your embedded resources cannot be referenced normaly by mentioning resouce name like how you did.
    You need to refere to the following links that will give you a good understanding of WebResourceAttribute and how to use it.

    WebResourceAttribute Class
    WebResource ASP.NET 2.0 explained
    Whats with System.Web.UI.WebResourceAttribute?
    System.Web.UI.WebResourceAttribute Issue
    http://forums.asp.net/thread/1677440.aspx

    mosessaur,

    most of the links i have already read before i post here... my post is unique (i guess) because i haven't find anybody use the way i'm using (style-sheet class)

    is this the correct way of using?

    .SearchButtonStyle

    {

    border-right: black 1px solid;

    border-top: black 1px solid;

    font-weight: bold;

    font-size: 9pt;

    background-image: url(myImage.gif);
    border-left: black 1px solid;

    width: 19px;

    color: navy;

    border-bottom: black 1px solid;

    font-family: Arial;

    height: 22px;

    }

    btnSearch =  new HtmlInputButton();
    btnSearch.ID =
    "btnSearch";
    btnSearch.Attributes[
    "class"] = SearchButtonStyle;   <<<<<<<<<<<<<<<<


    again.. will you please show me exactly what i need to do ?

     

    Tuesday, April 24, 2007 4:16 PM
  • User180615624 posted

    Not it is not correct, background-image: url(myImage.gif);  myImage.gif is not comming from resources this way.

    In stylesheet you cannot use such things, although there is concept of dynamic stylesheet supported by IE.
    Anyway, you should reference your embedded resouce from the code behind using Page.ClientScript.GetWebResouceUrl method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2007 4:25 PM
  • User-1121087500 posted

    mosessaur is correct, you cannot access the image that way.

    WebResources are just a way to create a url link to an embedded resource.  This url will be dynamically created, therefore it would not make any sense to try and reference it from a static location (your css file).

    To get the effect you are looking for you can do the following:

    btnSearch.Style[HtmlTextWriterStyle.BackgroundImage] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myImage.gif"); 

    Don't forget to include the Namespace and any folder names for your webresource.  eg. If your project name is Proj1 and myImage is in a folder called images, you need:

    [assembly: System.Web.UI.WebResource("Proj1.images.myImage.gif", "img/gif")]

    and the button would need the following 

    btnSearch.Style[HtmlTextWriterStyle.BackgroundImage] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Proj1.images.myImage.gif");

     

    Also, there is a control called Link for adding style sheets that you can use instead of a LiteralControl.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2007 5:13 PM
  • User888441741 posted

     Also, there is a control called Link for adding style sheets that you can use instead of a LiteralControl.

    thank you guys...

    can you please tell me what is the namespace of Link for adding style sheets ?

    i have googled but not found anything...

    Thanks

    Tuesday, April 24, 2007 9:09 PM
  • User-1121087500 posted

    The tag is HtmlLink, sorry for the typo.

     

    System.Web.UI.HtmlControls.HtmlLink

    Wednesday, April 25, 2007 10:25 AM
  • User-300753328 posted
     Hello friends

    Today I was working on embedding javascript file into assemblies. I did following steps:

    1) copied a treeviewadapter.js file in a common folder under project folder named blablah.common.web.

    2) Set the Build Action to Embedded Resource.

    3) In assemblyinfo.cs file added[assembly:

    System.Web.UI.WebResource("blablah.common.web.treeviewadapter.js", "text/javascript")] 
    Now to access this embedded file in another fproject folder:
     1) In the master page.aspx added this content:
    <%@ Register TagPrefix="Samples" Namespace="blablah.common.web" Assembly="blablah.common.web" %>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
    <asp:ScriptReference Assembly="blablah.common.web" Name="blablah.common.web.treeviewadapter.js" /> </Scripts> 
    2) then in code behind under the pre render event added following code to refer to the embedded js file

    protected override void OnPreRender(EventArgs e)

    {Page.ClientScript.RegisterClientScriptInclude("blablah.common.web.treeviewadapter.js",
                     Page.ClientScript.GetWebResourceUrl(this.GetType(),"blablah.common.web.treeviewadapter.js"));
        base.OnPreRender(e);}

     Now this code just runs fine. It solves my purpose but after using this code i started getting a "syntax error" at

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">. It refers to some Webresource.axd file.

     

    I am not able to find any solution for this .

    I will appreciate if somebody can just review my code and let me know what I am missing.

     

    Thanks a lot in advance!




     

     


     

    Monday, April 28, 2008 10:56 PM