Contentplaceholder for replacing attributes?

萝らか妹 提交于 2019-12-13 19:28:47

问题


The code below works but confuses Visual Studio. Are there alternative/better ways to accomplish this?

<body <asp:contentplaceholder id="BodyAttribute" runat="server"/>>

回答1:


Other way

<asp:contentplaceholder id="BodyAttribute" runat="server"><body></asp:contentplaceholder>

and on contentPlaceholder you type

<body attr="whatever">

If not place holder found, then the <body> is used.




回答2:


I don't think doing this is a good idea in general.

First of all, with ContentPlaceHolders you can easily end-up with HTML with newlines in id attribute.

Secondly, the code will be a total spaghetti. You will not be able to maintain it.

Thirdly, there must be a better way of accomplishing what you need.

For example, if you need to change the body id for the CSS styling purpose, or using it in JavaScript you will be better of with creating a nested div for that.

Master:

<body>
  <!-- div wrapper for general styling purposes -->
  <div id="mainContentWrapper">
    <asp:contentplaceholder id="Maincontent" runat="server" />
  </div>
</body>

Page:

<asp:content contentplaceholderid="MainContent" runat="server">
  <!-- div wrapper for page specific styling -->
  <div id="pageSpecificIdForYourPuprose">
    Your content goes here
  </div>
</asp:content>

Which will give you very style-able HTML:

<body>
  <div id="mainContentWrapper">
    <div id="pageSpecificIdForYourPuprose">
      Your content goes here
    </div>
  </div>
</body>

I would really try to avoid those WebForms tricky things to accomplish simple thing. Simple things can be done the simple way (pretty often at least).

Hope that helps.




回答3:


You could always do

<body id="body1" runat="server">

this page shows an example - http://msdn.microsoft.com/en-us/library/zzk7dby1(vs.71).aspx

the example is based off .net 1.1 but i've used something similar for .net 2.0



来源:https://stackoverflow.com/questions/4149971/contentplaceholder-for-replacing-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!