将 body id 设置为 .net 中的当前文件名
我不是 .Net 人员,但试图更新现有的 .net 站点。即使我使用的是母版页,也尝试为正文标记设置唯一的 ID。
显然上面的例子不起作用,但不太确定是否有一个函数可以返回可以轻松添加的文件名到母版页,以便它根据页面名称为每个页面的正文标记提供唯一的“id”。
我希望不必在每个代码隐藏页面上设置一些变量,而只需在母版页上设置一次。
可能会以完全错误的方式解决这个问题。我已经使用 PHP 这样做过很多次了。
基本上我试图为网站的每个页面获取一个唯一的正文ID。这将允许我使用 CSS 来设置页面特定布局项目的样式。
I am not a .Net guy but trying to get update an existing .net site. Trying to set up a unique id for the body tag even though I am using a master page.
<body id="<% page_name %>">
Obviously the above example doesn't work but not quite sure if there is a function that returns the file name that can be easily added to the masterpage so it give each page's body tag a unique "id" based on the page name.
I would perfer not having to set some variable up on each code behind page but just once on the master page.
Could be going about this the completely wrong way. Using PHP I have done this a number of times.
Basically I am trying to get a unique body id for each of the pages of the site. This will allow me to use CSS to style page specific layout items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经做到了你所说的。我就是这样做的。
body 标签中的内容非常接近。这是我在母版页标记中使用的,它是一个代码隐藏方法。
在后面的母版页代码中,您需要这个 using 语句:
基本上,它的作用是获取您将在运行时访问的页面的文件名。
以下方法用于获取页面的文件名,然后将该文件名作为唯一的 id 分配给生成的页面。
现在您应该能够访问该 body ID,就像访问任何其他类型的 ID 一样。我使用它通过样式表启用导航突出显示。还有一些涉及到的步骤,但您通常可以在谷歌上找到这些答案。
祝你好运,希望这会有所帮助。
I've done exactly what you're talking about. This is how I did it.
What you have in the body tag is pretty close. This is what I've used in the masterpage's markup, it's a code behind method.
In the masterpage code behind, you need this using statement:
Basically what this does is gets the file name of the page that you will be accessing during runtime.
The following methods are used to grab the file name of the page, and then assign that file name as a unique id to the generated page.
Now you should be able to access that body ID just like you would with any other type of ID. I've used this to enable navigation highlighting via a stylesheet. There are a few more steps involved with that, but you can generally find those answers on google.
Good luck, and hope this helps.
如果您想要包含在母版页中的解决方案,请向 body 标记添加 onload 函数调用。
在母版页中定义这个 JS 函数。像这样的东西:
If you want a solution contained in the Masterpage, add an onload function call to the body tag.
Define this JS function in the masterpage. Something like this:
我正在使用这个:
但是它对URL重写不友好。
I am using this:
but it's not friendly to URL rewriting.
您可能希望每个页面都使用您的母版页,向页面添加一些 onload javascript(启动脚本块),并且该 JS 函数将设置正文的 id。
在每个页面都包含的 js 文件中定义 setBodyId(txt) 函数。或者甚至将其放入母版页中。然后做这样的事情(未经测试):
在代码隐藏的 OnPreRender 事件中:
然后你的 JS 函数将像这样(未经测试):
注意,我从页面名称中取出了点,以避免正文 id 中的句点。
You may want to have each page that is using your masterpage, add some onload javascript to the page (startup script block) and that JS function will set the body's id.
Have your setBodyId(txt) function defined in a js file that every page includes. Or even put it in the masterpage. Then do something like this (untested):
In the OnPreRender event in the codebehind:
Then your JS function will be like this (untested):
Note I took the dot out of the page name to avoid periods in the body id.
通过 Google 快速搜索,我找到了此页面。也许
Request.Url
就是您所需要的?A quick Google search led me to this page. Maybe
Request.Url
is what you need?