Introduction to custom server controls

How to add the controls to a page

The first thing you need to do in order to be able to use the controls you've developed on a page is to add a Register directive:

<%@ Register TagPrefix=”MySite” Namespace=”Guestbook” Assembly=”Guestbook” %>

And now if you want to add the control GuestbookForm to the page you'd only need to add the following:

<MySite:GuestbookForm runat=”server” />

And if you wanted to change the text of i.e. the submit button you'd type this:

<MySite:GuestbookForm ButtonLabel=”Sign my guestbook” runat=”server” />

The GuestbookRepeater control is a little more complex to add, because you need to define templates for it. Here is one example of adding it to a page:

<MySite:GuestbookRepeater runat="server">
    <HeaderTemplate>
        <table cellpadding="2" bgcolor="#666666" cellspacing="1" border="0" width="400">
    </HeaderTemplate>
    <ItemTemplate>
        <tr bgcolor="#eeeeee">
            <td>
                <b><%# DataBinder.Eval(Container.DataItem, "name") %></b><br/>
                <%# DataBinder.Eval(Container.DataItem, "email") %><br/><br/>
                <%# ((string) DataBinder.Eval(Container.DataItem, "message")).Replace("\n", "<br/>") %><br/><br/>
                <i>Rating: <%# DataBinder.Eval(Container.DataItem, "rating") %></i>
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr bgcolor="#dddddd">
            <td>
                <b><%# DataBinder.Eval(Container.DataItem, "name") %></b><br/>
                <%# DataBinder.Eval(Container.DataItem, "email") %><br/><br/>
                <%# ((string) DataBinder.Eval(Container.DataItem, "message")).Replace("\n", "<br/>") %><br/><br/>
                <i>Rating: <%# DataBinder.Eval(Container.DataItem, "rating") %></i>
            </td>
        </tr>
    </AlternatingItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</MySite:GuestbookRepeater>


That's it, hope you've enjoyed reading this!

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra