I was looking for a solution to the problem of gaining better control over the html output than what is served up by asp.net. For example when I tried to use the radiobuttonlist control, asp.net came up with an html table. However I wanted to avoid using tables. My first thought was to try something like a data repeater. I’m sure that would have worked too but I ended up trying out a server control. I was attracted to it because it was so basic, you just output text really.
I learned that you can do server controls in two ways, one where it is part of your main project, another way where you keep the server control in a seperate project to make it easier to use in multiple web sites. So I tried the option to make it easier to deploy in multiple sites.
Steps to Create Server Control over several web sites
=====================================================
During the process if you see such an option to add the server control project to your current solution do it.
select the Solution in Solution Explorer
Right click ‘add new project’
select Visual Basic, Visual Studio 2012, ASP.NET Server Control
Now you should see two projects under your solution.
Open up the server control project class file named ServerControl.vb. Add the following.
Sub generateText() Me.Text = "hello world" End Sub
Change the RenderContents to add the generateText() call.
Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter) generateText() output.Write(Text) End Sub
Then go back to your main project and make sure you have a reference to the server control project.
Add a new webform to test it. Switch to design view. Drag on the server control from the toolbox. You should see ‘hello world’. Do a “build solution” to make sure no errors are found. Set the webform to start first with “set as start page”.
At this point if you see an error like this, it is probably because you created multiple versions of the control during development like I did.
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: BC32206: The project currently contains references to more than one version of [server control], a direct reference to version ... and an indirect reference (through 'projectname.test_of_servercontrol.ServerControl11') to version ... Change the direct reference to use version .... (or higher) of [servercontrol].
The way I fixed this error is to go back to the server control project, recompile the project as release, then go back to the main project, remove the existing reference to the server control project and then browse to the latest compile version. That dll file is located in the project folders, under bin release or bin debug (depends on where your compile options save the dll).
After I did this I re-ran the main project and the server control displayed as expected.