修剪或格式化 asp 网络超链接
Trimming or formatting an asp net hyperlink
正如标题所说:我想trim或格式化一个很长的超链接。如果文本太长,我希望代码知道这一点并将剩余的字符串替换为“...”,例如:"averylongemailaccountexample@example.com"。 "account" 开始后,我希望将其替换为“...”
我试过了trim,但还是不行。
C#:
var getContact = _ecSystem.GetContact(ContactId.Value);
hlEmail.Text = getContact.Email.Trim(); //getContact.Email is a string.
ASPX:
<asp:HyperLink runat="server" ID="hlEmail" NavigateUrl="#" />
您尝试过使用常规 SubString method 吗?
var emailaddress = getContact.Email.Trim();
hlEmail.Text = emailaddress.Length > 20 ? emailaddress.SubString(0, 17) + "..." : emailaddress;
但正如@RahulSingh 所说,最优雅的方法是使用 css and the text-overflow property,然后限制 link 所在的 html 容器的大小。
<a style="text-overflow: ellipsis; width: 50px; float: left; overflow: hidden;" href="mailto:averylongemailaccountexample@example.com">averylongemailaccountexample@example.com</a>
正如标题所说:我想trim或格式化一个很长的超链接。如果文本太长,我希望代码知道这一点并将剩余的字符串替换为“...”,例如:"averylongemailaccountexample@example.com"。 "account" 开始后,我希望将其替换为“...”
我试过了trim,但还是不行。
C#:
var getContact = _ecSystem.GetContact(ContactId.Value);
hlEmail.Text = getContact.Email.Trim(); //getContact.Email is a string.
ASPX:
<asp:HyperLink runat="server" ID="hlEmail" NavigateUrl="#" />
您尝试过使用常规 SubString method 吗?
var emailaddress = getContact.Email.Trim();
hlEmail.Text = emailaddress.Length > 20 ? emailaddress.SubString(0, 17) + "..." : emailaddress;
但正如@RahulSingh 所说,最优雅的方法是使用 css and the text-overflow property,然后限制 link 所在的 html 容器的大小。
<a style="text-overflow: ellipsis; width: 50px; float: left; overflow: hidden;" href="mailto:averylongemailaccountexample@example.com">averylongemailaccountexample@example.com</a>