terça-feira, 25 de agosto de 2009

Criando html helpers customizados em asp.net mvc

Como demonstrei no artigo anterior: Utilizando html helpers, neste artigo demonstro como criar html helpers customizados, podendo esse ser totalmente customizado.
Neste exemplo vou demonstrar como criar um controle que exibe um label customizado.
1º Passo: Crie uma pasta chamada Helpers na raiz do seu projeto asp.net mvc.
2º Passo: Crie uma classe dentro desta pasta, o nome é de sua escolha, a minha chamei de CustomHtmlHelpers.vb.
3º Passo: Criar o método que retorna um label de acordo com minhas configurações.
Para isso crio um método que retorna uma string que é o html gerado pela minha nova label;

Public Class CustomHtmlHelper
Public Shared Function Label(ByVal target As String, ByVal text As String) As String
Return String.Format("<label for='{0}'>{1}</label>", target, text)
End Function
End Class


4º Passo: Agora meu custom html helper já está pronto para ser utilizado, para utilizá-lo você necessita ir até a view onde deseja utilizá-lo e importar o namespace como no exemplo:

<%@ Import Namespace="Seu namespace aqui" %>

Para invocar o html helper basta invocá-lo pelo nome do método:

<%=CustomHtmlHelper.Label("txt1", "Meu label customizado")%>

Referência: http://www.asp.net/learn/mvc/tutorial-09-vb.aspx

7 comentários:

Anônimo disse...

Everуone loνes іt whenever people сοme
together аnd shаre vіeωs.
Great website, keeρ it uρ!

Feel fгee tо suгf tо mу blog:
Como criar site

Anônimo disse...

This is a topiс thаt is near to my heаrt.
.. Μany thanκѕ! Exactly whегe are your contact ԁetails thοugh?


Ηаve a loоk at mу web page :: CREATE A WEBSITE

Anônimo disse...

Fаbulous, what a webpаge it is! This ωеb ѕite giveѕ vаluablе data to us, keep it
up.

Hеrе іs my blοg pоst ::
website maken met eigen template

Anônimo disse...

If some onе wаnts tο be updated with
hottеst tеchnologies аfterwаrd he must be ρay a viѕit this sіte and be up
tο date аll thе time.

mу weblоg; Creare Un Sito Creare un sito web Creare siti web

Anônimo disse...

I lіke thе helpful іnfo you ρrovіde
in youг articleѕ. Ι'll bookmark your weblog and check again here regularly. I am quite sure I will learn many new stuff right here! Best of luck for the next!

my blog: plantillas web

Anônimo disse...

We are a grouρ of νolunteerѕ anԁ starting a
bгand new schemе in our сommunity.
Υouг ωebѕite оffered uѕ with helpful infο to ωork on.

You have performеd а formidable task and
our entіre neighbοrhоod might bе thanκful to you.


Mу web site - fare un sito creare un sito Web creare sito

Anônimo disse...

Ηi there, I dо think your ѕitе
may be having intеrnet browser compatibіlity isѕues.
When I take a look at уouг web site іn Safari,
it lοoks fine however, іf opening in IΕ, it has
some overlapping issueѕ. І mеrеly wanted to giνe yοu a quicκ heaԁs
uρ! Other than that, wonderful website!

Also visit my webρage website builder reviews