Click here to Skip to main content
Click here to Skip to main content

Server controls in separete assembly

By , 21 May 2013
Rate this:
Please Sign up or sign in to vote.

In my project I wanted to create text box control with bulletin configurable validation. ASP.NET definition in .aspx or .ascx file would get validation function by name. But I do not wanted to create just .ascx file with text box. I could not then reuse that control in other projects and it certainly would be nice, right? Creating such server control from code is not hard, but maintainability of HTML code in C# (it cannot be done other way) is not good. It's pain really. Why Microsoft would create way to automatically create C# code from .ascx, .aspx files is mystery for me.

But what we can do get around this problem? Only solution I could find is using WebSite project type. You creates such controls or pages in that project and then after publish with specific options you can obtain .dll files for every control and page in that project. You can add web site to your solution by clicking right mouse button on solution and then Add>New Web Site.

After that in your new project add new control file: HelloWorld.ascx 

Great. Now in control Add "Hello World!" text.

Now we must build our WebSite. Right click on your web site project and chose "Publish Web Site" option. Choose options like on screen:

After click on "ok" button, when Publish ends, in destination path in "bin" folder, we will see some files:

This strangely named file "App_Web_helloworld.ascx.cdcab7d2.dll" is our compiled HelloWorld control. Great! Now we just have to add reference to whis assembly in our Web Application project. This can be done by reference menu like with every other .dll file. After adding it we just have to configure project to use control from that assembly. That is tricky part. Despite the fact that we did not add namespace to our control, website project did that for us. This namespace is called ASP. So to use this control in our web application project we need to insert this configuration into web.config file:

<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="WebControls" namespace="ASP" assembly="App_Web_helloworld.ascx.cdcab7d2" />
      </controls>
    </pages>
  </system.web>
</configuration>


Now we can use our new control in page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <WebControls:helloworld_ascx runat="server" />
    </div>
    </form>
</body>
</html>
That is all. Tomorrow I'll cover topic of making more complicated controls.

Smile

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

n.podbielski
Software Developer
Poland Poland
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 21 May 2013
Article Copyright 2013 by n.podbielski
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid