In my project I wanted to create text box control with bultin 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 maintability 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 mistery 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 asembly. 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