Tuesday, January 23, 2007

webresource of .net 2.0

simple steps:
1. add image,css,or javascript files into a project;
2. make them build action as embed resource;
3. add assembly entry in to AssemblyInfo.vb like:

or


the second one is to generate a macro.
4.using getresourceurl to get the resource;

here is the example:

test.js:

function alertme(){
document.write("<img src='/<%=WebResource("myobj.myimage.gif")%>'>");

}

AssemblyInfo.vb:


<Assembly: WebResource("myobj.myimage.gif", "image/gif")> <Assembly: WebResource("myobj.test.js", "text/javascript", PerformSubstitution:=True)>


Test page:

Dim cs As ClientScriptManager = Page.ClientScript
Dim img As New System.Web.UI.WebControls.Image
Dim s As String = cs.GetWebResourceUrl(Me.GetType, "myobj.myimage.gif")
img.ImageUrl = s
img.Attributes.Add("onclick", "alertme();")
Page.Controls.Add(im)

those above will display the image in a page.