Search code examples
asp.netservercontrols

ASP.NET: Mixing server controls with client controls?


Can anyone help?

I normally use server controls i.e Textbox so i can get access to the server side event.

But what if i don't need access to the server side event and i am going to place some jquery or javascript on the textbox for example.

Can i use a standard HTML (client controls) ?

Is this good practice or not?

Thanks in advance


Solution

  • YOu can use simple HTML standard control BUT if you want to access that controls value in ASP.NET then you will have to add runat="server tag" see below

    <input type="text" runat="server" id="mytxtbox" name="mytxtbox">
    

    If you really want to use HTML controls you can use them. but it will make your life easy to use standard ASP.NET controls if you are trying to access them on server side.

    And if you want to access those controls using javascript as well then you can use something like

    var mytxtele = document.getElementById('<%= mytxtbox.ClientID %>')
    

    thats how you can get textbox element and play with it in javascript.

    This above code is a basic idea, depends how you want it to work