I have a repeater control with some html controls being populated from some server side DataSource. The code for these controls inside the repeater looks something like this..
我有一个转发器控件,一些html控件从一些服务器端DataSource填充。转发器内部的这些控件的代码看起来像这样。
<img src='<%# DataBinder.Eval(Container.DataItem, "Path")%>' title='<%# DataBinder.Eval(Container.DataItem, "Name")%>' alt="">
I needed to format some of the text so I added a method to this code. The method I added is a server side method though. So I'm assuming this isn't exactly the best way to handle things in terms of performance. The code looked something like this...
我需要格式化一些文本,所以我在这段代码中添加了一个方法。我添加的方法虽然是服务器端方法。所以我认为这不是处理性能方面的最佳方法。代码看起来像这样......
<span><%# trimItemName((DataBinder.Eval(Container.DataItem, "Name"))%></span>
trimItemName(Object obj) is a server side method that will obviously trim the name.
trimItemName(Object obj)是一个服务器端方法,显然会修剪名称。
Is there a way I can do this using javascript so doing a simple string trimming (or any other kind of formatting) doesn't have to be done on the server side?
有没有办法我可以使用javascript这样做,所以做一个简单的字符串修剪(或任何其他类型的格式)不必在服务器端完成?
2 个解决方案
#1
You can always do:
你可以随时做:
<script type="text/javascript">
function trimItemName(input) {
//return trimmed string
}
</script>
<span>
<script type="text/javascript">
document.write(
trimItemName('<%# DataBinder.Eval(Container.DataItem, "Name") %>')
);
</script>
</span>
But I'm not really sure how moving it to the client-side would buy you anything. You risk not getting it trimmed if the user has JS disabled or doesn't support it (~10% of internet users), plus the development overhead of keeping your client-side and server-side logic in sync.
但是我不确定如何将它转移到客户端会给你买任何东西。如果用户已禁用JS或不支持JS(大约10%的互联网用户),以及保持客户端和服务器端逻辑同步的开发开销,则可能无法修复它。
#2
Personally I would start by placing that kind of string processing on the server. I wouldn't actually consider it a problem until I saw some kind of significant performance hit.
我个人会首先在服务器上放置那种字符串处理。在看到某种重要的性能影响之前,我实际上并不认为这是一个问题。
#1
You can always do:
你可以随时做:
<script type="text/javascript">
function trimItemName(input) {
//return trimmed string
}
</script>
<span>
<script type="text/javascript">
document.write(
trimItemName('<%# DataBinder.Eval(Container.DataItem, "Name") %>')
);
</script>
</span>
But I'm not really sure how moving it to the client-side would buy you anything. You risk not getting it trimmed if the user has JS disabled or doesn't support it (~10% of internet users), plus the development overhead of keeping your client-side and server-side logic in sync.
但是我不确定如何将它转移到客户端会给你买任何东西。如果用户已禁用JS或不支持JS(大约10%的互联网用户),以及保持客户端和服务器端逻辑同步的开发开销,则可能无法修复它。
#2
Personally I would start by placing that kind of string processing on the server. I wouldn't actually consider it a problem until I saw some kind of significant performance hit.
我个人会首先在服务器上放置那种字符串处理。在看到某种重要的性能影响之前,我实际上并不认为这是一个问题。