Where is the best place to htmlEncode a text box on an aspx page? Inside of a Subroutine, between html head tags, between html body tags...?

I want that the content of the text box is HtmlEncode before the data reaches to the server so that no damage is caused. I only knows that it is recommended 'HtmlEncode' all the input strings for security reasons, I understand all the text box fields that in its server validation allows the characters '<' and '>'.

For example: (Where I have to write this?)
Code:
Dim var1 As String
var1 = mail.Text()
Server.HtmlEncode(var1)
Thank you,
Cesar