0

I am trying to create hyper link on condition of specific value of database field, it is for news page, and some news has content long and in this case I want to create hyper link but in case the length small no need for the link, the code that I used:

<asp:Label ID="lblContent" runat="server" 
                        Text='<%# string.Format("{0}",Eval("New_Content").ToString().Length>150? <a href> Eval("New_Content").ToString().PadRight(150).Substring(0,150).TrimEnd() + " ..." </a>:Eval("New_Content")) %>' >
                    </asp:Label>

but there is error, so can I have some help?

0

3 Answers 3

3

I am trying to create hyper link

Well why not use a <asp:HyperLink /> control then?

Also don't string use Substring() to reduce the length of the text and add the ... at the end, just use css.

e.g.

<asp:HyperLink ID="hlContent" runat="server" CssClass="trimme" 
                Text='<%# Eval("New_Content").ToString() %>'></asp:HyperLink>

Then add a css class called trimme

a.trimme{
  display: block;  
  width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Take a look at this example

Sign up to request clarification or add additional context in comments.

1 Comment

good idea for using css in trimming, but how can I make the text to be more than one line, and I am not using <asp:HyperLink> because I want to use the link only if the text length more thank 150 characters
0

The code you had was almost correct, it just seems that you are missing some quotes:

<asp:Label ID="lblContent" runat="server" 
    Text='<%# String.Format("{0}", Eval("New_Content").ToString().Length > 150 ? "<a href='" + Eval("Link_Href") + Eval("New_Content").ToString().PadRight(150).Substring(0, 150).TrimEnd() + "'>...</a>" : Eval("New_Content")) %>' >
</asp:Label>

The text in an ASP Label is output raw to the response stream, so you can just write standard HTML, but it does need to be in quotes as you are building a new string object.

Comments

0

I got the solution using LastIndexOf() as:

<asp:Label ID="lblContent" runat="server" CssClass="nostyle" 
                        Text='<%# string.Format("{0}",Eval("New_Content").ToString().Length>150?string.Format("<a href=news.aspx?page=3#{0}>{1}</a>",Eval("New_Id"),string.Format("{0} {1}",Eval("New_Content").ToString().PadRight(150).Substring(0,Eval("New_Content").ToString().PadRight(150).Substring(0,150).LastIndexOf(" ")>-1 ? Eval("New_Content").ToString().PadRight(150).Substring(0,150).LastIndexOf(" "): 150).ToString(), " ...")):Eval("New_Content")) %>' >
                    </asp:Label>

Thank you all, you gave me good ideas

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.