Best way to access properties of my code behind class from the markup in ASP.NET

前端 未结 5 1292
攒了一身酷
攒了一身酷 2020-12-14 02:50

This might be a really dumb question but I\'m learning .NET, so I\'m quite clueless...

Let\'s say I have two files default.aspx and the associated default.a

5条回答
  •  [愿得一人]
    2020-12-14 03:14

    The <%= ... %> is ASPX short-hand for Response.Write( .... ).

    If you change myObject to be strongly types (rather than just types as Object) you can certain use the line:

    <%= myObject.Awesome %>
    

    to emit a value.

    Be aware, however, that the <%= syntax has limitations - specifically:

    • it cannot be used within HTML attribute as a value
    • it cannot emit server-side elements
    • it cannot be used within ASP UpdatePanels - you will get a exception when you perform a partial postback

    You are probably better off creating a Label control on you page, and programmatically setting the Text property to the value. This also gives you more control over how value-to-string conversions are performed. And will work correctly with update panel controls.

提交回复
热议问题