views:

33

answers:

1

Hi,

I made a function to truncate a string in the code behind file. But how do i use it in the aspx file?

This is the textbox:

<asp:TemplateField HeaderText="page" HeaderStyle-Wrap="true">
    <ItemTemplate>
        <a href='<%# makepageURL( Eval("page") )%> '>
            <%# Eval("page")%>
        </a>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtpage" TextMode="SingleLine" Rows="1" Width="100%" runat="server" Text='<% #Bind("page") %>' />
    </EditItemTemplate>
</asp:TemplateField>

And this is my function:

Public Function TrimString(ByVal Value As String, ByVal Length As Integer) As String
    If Value.Length > 20 Then
        Return Value.Substring(Value.Length - (20 - 3)) + "..."
    End If

    Return Value   
End Function
+1  A: 

It's not an issue of how to use it, but actually when to use it?

If you had a regular span, you could do this:

<span><%: TrimString("somestring") %></span>

But this is a TextBox your dealing with (user input).

When should it truncate?

On Form Submit? (that would make sense).

As they type (well then you'd need to use JavaScript).

By the looks of your code snipper, your using a FormView.

So i wouldn't be calling it from the ASPX (which the equivalent of executing code during Page Render), i would be calling it during the Edit/Submit event, server-side event handler.

In other words, truncate the value the user put in, after they have submitted the form and before you persist to the database.

RPM1984
The posted snippet will _not_ work. You can't use `<%%>` (code blocks) inside a server side control.
Oded
No actually i should be truncated when they 'view' it. I edited my first post. forgot to paste the complete template
Yustme
@Oded - my bad, updated.
RPM1984
@Yustme - i haven't used FormView in a while, but assume you have a DataBind event in the code behind? can't you truncate each row as you databind?
RPM1984
hmm.. thats a good idea, havent thought about that, thanks!
Yustme
Okay, let us know how you get on - and don't forget to tick this as the correct answer if it helped.
RPM1984