Sunday 24 April 2011

Asp.Net: Embedding resources (image, css and JavaScript) to a user control and convert the user control to dll


What are the advantages of embedding them?

You could put all your dependencies into one single assembly and then ship the assembly out to whoever needs it without having to worry about stuff like does the user has the latest client-side scripts? Did the user remember to put the images in the /something/something/images folder? Did the user set the permissions for the new folder accordingly? Is there any conflict between the resources that my library requires and any other library? So there is no such problem if you use an assembly.

How to embed resources (image, css and JavaScript) to a user control


1. I have created a user control testUsercontrol.ascx inside that. By default the MS VS 2008 will create an ASCX file with next declaration inside it:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="testUsercontrol.ascx.cs" Inherits="ControlCreate.testUsercontrol" %>

After that put some code in the user control.I have added a Image control and button control into that user control.Which look like:

<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td align="center">
            <asp:Image ID="imgTest" runat="server"/>
        </td>
    </tr>
    <tr>
        <td align="center">
            &nbsp;</td>
    </tr>
</table>
<asp:Button ID="btnClick" runat="server" Text="Click"  />

2. All the Image, Css and JavaScript files those we would like to embed to our desired dll file is under a folder Resources.








3. Add all those files to the WebResources class which is under Assembly.cs file under Properties of the Project.AS I showed in the figures below.

 
 4. Now in the page load event of the user control we have to write the following code:

 

 

Before I created an assembly I tested the user control into the first project. I added that user control in the page ControlPage.aspx.   
  • ·         I registered the user control Like this:     <%@ Register Src="~/testUsercontrol.ascx" TagName="testUC1" TagPrefix="uc1" %>
  • ·         And then I put that inside a div under <form> tag.Like this:      <uc1:testUC1 ID="TestUC1_1" runat="server" />

And worked for me as I could able to see the desired output.Then I started to created an assembly as I described below.

How to Convert a User Control to an assembly (dll)

Now I will convert the user control to an assembly (dll) and using that in separate web project. Let’s start:
  •   I have downloaded “WebDeploymentSetup.msi" and installed in my pc. You have to install it as plug-in for MS VS 2008.This file also may to be found in Microsoft Web site and download from there. This file gives the ability of MS VS 2008 to create more complex deployment web projects. You can get it here:http://www.microsoft.com/downloads/en/details.aspx?FamilyId=0AA30AE8-C73B-4BDD-BB1B-FE697256C459

Firstly we have to add a web deployment project.




























    Fig: 1
1. Add a Web Development Project. It will look like Fig: 2:
 










Fig: 2
2. After that we will go to the Property Pages of that project. Fig: 3
 







 






















Fig: 3
Next step in our converting process is to setting-up our deployment project with correct settings.
Into item "Compilation" we have to uncheck the option:
"Allow this precompiled site to be updated". I am doing this because in update mode only files of code behind will be compiled. ASCX will be left.Because our goal is to redistribute our DLLS/assemblies/ and for this purpose our DLLS has to be self-contained.

Into item "Output assemblies" we have to check the option: "Create a separate assembly for each page and control output" And as last option into item "Deployment" we have to check option: "Remove App_Data folder from output location"
All of these actions are shown on figures No.4, 5 and 6.













Fig No.4 














Fig No.5 













Fig No.6
3. Now in this step we are ready to build the entire solution and generate our assemblies.
In bin folder of web deployment project you may found ours assembly App_Web_testusercontrol.ascx.cdcab7d2.dll.Exactly the assembly is our redistributive DLL. Our custom controls.

4. We are almost on the end of this article. We have to create now one separate Web Application Project which has to consume and use our converted user controls. Create a usual web project and references to DLLS above.

5. Now the last step is manually to register the added assemblies in ASPX page where we like to use our converted controls. This is very easy step
and the code for this action is shown below:

<%@ Register Assembly="App_Web_testusercontrol.ascx.cdcab7d2" TagPrefix="T1" Namespace="ASP"  %>

10. The last step is to register in our ASPX page these two controls and starts use them. For this goal I use next code:

<T1:testusercontrol_ascx ID="mitkoTestControl1" runat="server" />

It should now work fine.

hasan.se.bd@gmail.com.


No comments:

Post a Comment