views:

180

answers:

2

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..

<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.

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?

A: 

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.

ninj
A: 

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.

Rex M
Good point. Thanks for the info.
hanesjw