How to create a label which only displays a certain amount of characters?

  • Thread starter Daniel Walzenbach
  • Start date
D

Daniel Walzenbach

Hi,



does anybody know how to create a custom control which inherits from
System.Web.UI.WebControls.Label with the ability to display only a certain
amount of characters? I thought about overriding the Text property of a
System.Web.UI.WebControls.Label but it does not work like I want it to do.



Thank you a lot in advance

Daniel
 
J

Jeffrey Tan[MSFT]

Hi Daniel,

Thank you for posting in the community!

Based on my understanding, you want the WebForm Label control to only
display certain amount of characters.

=====================================
I think you should inherit the Label class and override the Text property.
As you said, this does not work as you expected, can you show me how you do
with your Text property? Why it does not work as you expected?

I think you may do like this(Suppose you only want to display the Text's
length less than 8 ):
public class WebCustomControl1 : System.Web.UI.WebControls.Label
{
public override string Text
{
get
{
return base.Text;
}
set
{
if(value.Length<=8)
{
base.Text=value;
}
else
{
this.Page.Response.Write("The value you set is greater than 8!");
}
}
}

}

=====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Daniel Walzenbach

Jeffrey

I found the error I made. Thanks for your help. For everybody who is interested in my solution (which is far from being perfect but for now fits my needs...

Imports System.Web.UI.WebControl
Imports System.Web.U
Imports System.ComponentMode
Imports System.Drawin

<DefaultProperty("AmountOfCharactersToBeDisplayed"), ToolboxData("<{0}:CharacterLimitedLabel runat=server></{0}:CharacterLimitedLabel>")> Public Class CharacterLimitedLabe
Inherits System.Web.UI.WebControls.Labe

<Bindable(True), Category("Behavior"), Description("Die Anzahl der Zeichen, die das Label darstellen soll.")>
Public Property AmountOfCharactersToBeDisplayed() As System.Int3
Ge
If Not viewstate.Item("AmountOfCharactersToBeDisplayed") Is Nothing The
Return CInt(viewstate.Item("AmountOfCharactersToBeDisplayed")
End I
End Ge
Set(ByVal Value As System.Int32
viewstate.Item("AmountOfCharactersToBeDisplayed") = Valu
End Se
End Propert

Public Overrides Property Text() As System.Strin
Ge
Return MyBase.Tex
End Ge
Set(ByVal value As System.String
If value.Length > AmountOfCharactersToBeDisplayed The
MyBase.Text = value.Substring(0, AmountOfCharactersToBeDisplayed
End I
End Se
End Propert

Private Function inDesignMode() As System.Boolea
If Object.ReferenceEquals(System.Web.HttpContext.Current, Nothing) The
Return Tru
Els
Return Fals
End I
End Functio

End Clas

Greeting
Daniel Walzenbach
 
J

Jeffrey Tan[MSFT]

Hi Daniel,

Thanks very much for your feedback.

I am glad you have found the problem :)

If you have any further concern, please feel free to post, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top