使用 VB.NET 在 ASP.NET 中截断字符串

发布于 2024-09-28 18:11:26 字数 822 浏览 1 评论 0原文

我创建了一个函数来截断代码隐藏文件中的字符串。但是我如何在 aspx 文件中使用它呢?

这是文本框:

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

这是我的功能:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-10-05 18:11:27

这不是如何使用的问题,而是何时使用它的问题?

如果你有一个常规的跨度,你可以这样做:

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

但这是你处理的一个文本框(用户输入)。

什么时候应该截断?

在表单提交上? (这是有道理的)。

当他们输入时(那么你需要使用 JavaScript)。

从代码片段的外观来看,您使用的是 FormView。

所以我不会从 ASPX 调用它(相当于在页面渲染期间执行代码),我会在编辑/提交事件、服务器端事件处理程序期间调用它。

换句话说,在用户提交表单之后、保存到数据库之前截断用户输入的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文