基于 html 内容的 ASP.Net 服务器端导航菜单
我需要对一堆网络表单进行一些样式设置,其中包含以相当统一的方式格式化的文章。我可以更改任何我想要的源代码。
我需要的是一种基于指定 div
的内容为 ASP.NET Web 表单动态创建导航菜单(在服务器端)的快速方法。
例如,给定以下 HTML:
<div id="article">
<h2 id="first">Chapter 1</h2>
<p>Some text...</p>
<h2 id="second">Chapter 2</h2>
<p>Some other text</p>
</div>
我想在末尾插入类似的内容(并在服务器端渲染它,而不是在脚本中):
<div id="navigation">
<ul>
<li><a href="#first">Chapter 1</a></li>
<li><a href="#second">Chapter 2</a></li>
</ul>
</div>
注意: 我知道我可以迭代父级div 的子控件位于代码隐藏中(尽管我需要让它们全部“在服务器上运行”,甚至解析父 div 的 InnerHtml 属性),但 if 感觉很奇怪。
另外,我知道如果文章是从数据源创建的,我会已经组织好内容,但我想在现有页面中进行尽可能少的更改。
I need to do some styling to a bunch of webforms, containing articles formatted in a rather uniform way. I can change any source code I want.
What I need is a quick way to dynamically create a navigation menu (on the server side) for an ASP.NET webform, based on contents of a specified div
.
For example, given the following HTML:
<div id="article">
<h2 id="first">Chapter 1</h2>
<p>Some text...</p>
<h2 id="second">Chapter 2</h2>
<p>Some other text</p>
</div>
I would like to insert something like this at the end (and render it at the server side, not in a script):
<div id="navigation">
<ul>
<li><a href="#first">Chapter 1</a></li>
<li><a href="#second">Chapter 2</a></li>
</ul>
</div>
NOTE: I know I could iterate through parent div's child controls in codebehind (although I would need to make them all "run at server", or even parse the InnerHtml property of the parent div), but if feels pretty weird.
Also, I am aware that if the article was being created from a data source, I would have the content already organized, but I would like to make as little changes needed in the existing pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用正则表达式搜索标题并从结果中呈现导航。像
"
([^<]+)
"
这样的东西会给你在第一个和第二组中的标题。You could search for the headings with a RegEx and render the navigation from the results. Something like
"<h2 id=\"([^\"]+)\">([^<]+)</h2>"
would get you the id in the first and the caption in the second group.如果您有权访问创建文章的数据源,请务必使用该数据源。
但是,如果您只有 HTML,我会使用 XSLT。
If you have access to the data source that is creating the article, definitely use that.
However, if all you have is the HTML, I would use XSLT.