ASP.Net Web 表单替换 Literal 控件中文本的最佳实践
这是一个简单的问题,我正在查看您的意见。
我正在寻找最佳实践来替换文字控件后面的代码中的一段文本。
aspx 标记看起来像这样,我正在替换链接中显示 {0} 的值:
<asp:Literal runat="server" ID="litViewLightingCollections" Visible="false">
<span class="disclaimer">
<div style="padding-bottom:5px;">
<a href="/LightingCollections/{0}/default.aspx" >
<img src="/images/productdetail/elements/icon-lighting-collections.gif" border="0" align="absmiddle"> Lighting Collections
</a>
</div>
</span>
</asp:Literal>
所以我在测试中调用 string.format 所做的工作,效果很好:
litViewLightingCollections.Visible = true;
litViewLightingCollections.Text = String.Format(litViewLightingCollections.Text, lightingDictionary[category]);
这工作得很好,但是还有其他方法来做到这一点。 最好和最轻量级的方法是什么?
- 我可以在 href 标签中有另一个文字控件并设置它的值
- 我可以在文本中创建一个占位符,如 {replaceme} 并在 lit.text 上调用 String.Replace
- 在后面的代码中放置一个属性并使用 <%= Property %> (我真的不喜欢这个,因为它在页面上创建了很多属性)
- 在文字中放置超链接控件(反对轻量级)
- 还有哪些其他选项?
This is an easy question that I'm looking your input on.
I'm looking for the best practice to replace a piece of text from the code behind in the literal control.
The aspx markup looks like this and I'm replacing value in the link where it says {0}:
<asp:Literal runat="server" ID="litViewLightingCollections" Visible="false">
<span class="disclaimer">
<div style="padding-bottom:5px;">
<a href="/LightingCollections/{0}/default.aspx" >
<img src="/images/productdetail/elements/icon-lighting-collections.gif" border="0" align="absmiddle"> Lighting Collections
</a>
</div>
</span>
</asp:Literal>
So what I have done to call a string.format on the test, which works fine:
litViewLightingCollections.Visible = true;
litViewLightingCollections.Text = String.Format(litViewLightingCollections.Text, lightingDictionary[category]);
And this works fine, however there are other ways to do this.
What is the best and the most lightweight method to do this?
- I could have another literal control in the href tag and set value of it
- I can create a placeholder in the text like {replaceme} and call String.Replace on lit.text
- Put a property in the code behind and use <%= Property %> (I really don't like this one, since it creates a lot of properties on the page)
- Placing a hyperlink control in the literal (against the light weight)
- What are other options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试做的只是替换 href,则没有理由使用文字。控件的重点之一是它们允许以更加编程的方式设置值。解决此问题的一种方法是将 替换为 链接按钮并在链接按钮的加载上在后面的代码中设置href。另一种解决方案是使用旧式 asp 标签 <%= %>并让服务器代码自动替换所需的部分。
If all your attempting to do is replace href there is no reason to use a literal. One of the whole point of controls is that they allow a more programmatic way to set values. One way to solve this is to replace the with a link button and on the onload of the link button set the href in the code behind. Another solution is to use old style asp tags <%= %> and have the server code automatically replace the needed section.