Adding an HTML element to a Webform

If I add an ASPX web control to my Webform the IDE correctly adds the appropriate aspx element code to my .aspx page, adds the appropriate ASPX control to my code-behind class, and automatically instantiates the ASPX control in my code-behind class at run-time.

If I add an HTML element to a Webform the IDE correctly adds the HTML it to my .aspx page. However it does not add the appropriate HTML control to my code-behind class and instantiate the corresponding HTML control at run-time.

Is this the correct behavior of the IDE Should not the IDE be adding instances of all components which are dropped into a Webform to the code-behind class and correctly instantiating those components at run-time

Is there a way to force the IDE to automatically do this


Answer this question

Adding an HTML element to a Webform

  • Oligarchy

    Standard HTML elements by default do not have any server processing. This way, you don't have the overhead of the compiler when all you want is to display a textbox, table or any other simple element. Elements that do require some serverside logic are generally selected from the list of web server controls.

    However, if you want to reference HTML elements from servercode, you can add the attribute

    runat="server"

    to the element. This effectively turns the element into a web server control.


  • KamFai

    Hi,

    HTML control wont be avilable in code behind class, two method is there to access the HTML control in code behind class

    1. add attribute runat="server"

    In this scenario object will be created in code behind class and you can use that object

    2. donot modify anything in .aspx page

    you can access the value of HTML control in code behind class using

    Request.Form["txtUserName"] This will return the value of the text box ( txtUserName ).

    - Thanks

    Mahesh



  • Adding an HTML element to a Webform