2014-07-17

Control ID Naming in Content Pages (C#)

http://www.asp.net/web-forms/tutorials/master-pages/control-id-naming-in-content-pages-cs

If you have a container, e.g. a content page in a web form that uses master pages, then your asp server side controls ID do not map into the same html id.


ID="label1" 

not mapped to

id="label1"

instead because of the container content page, it will look like

id="ctl00_MainContent_label1"

So when retrieving the the control, we need to use
label1.ClientID to get the html id

if we need to embed the id within a jQuery selector, we should use some like the following
in the aspx page

$("<%= label1.ClientID %>").



References to the different tags

<% %>  An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class. http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx
<%= %> most useful for displaying single pieces of information. http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
<%# %> Data Binding Expression Syntax. http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx

No comments:

Post a Comment

Github CoPilot Alternatives (VSCode extensions)

https://www.tabnine.com/blog/github-copilot-alternatives/