开发者

Asp.Net textbox, how to wrap splitting word instead of moving to the next line

开发者 https://www.devze.com 2023-03-07 06:37 出处:网络
Im using Asp.Net and VB on Visual Studio 2010 Hi, i am using a System.Web.UI.WebControls.TextBox with the wrap property set to \"true\", my problem is that when it wraps a word, it moves the whole wo

Im using Asp.Net and VB on Visual Studio 2010

Hi, i am using a System.Web.UI.WebControls.TextBox with the wrap property set to "true", my problem is that when it wraps a word, it moves the whole word to the next line, what i want to accomplish is for the textbox to wrap, but not move the whole word, just move to the new line when the text reaches the right side of the textbox.

Example, imagine a textbox 10 chars wide.

This is what happens:

0123456789  
i am a  
textbox 

This is what i need:

0123456789  
i am a tex  
tbox

But if开发者_开发知识库 i set the wrapping to false, it just will keep expanding the width of the textbox on the same line.

I was thinking of setting the wrap to false, and use the onTextChanged event to check the length of the text and manually trigger the new line. is there any easier way to acomplish this?

Thanks a lot!


This is a web browser implementation issue rather than something that can be accomplished with ASP.NET.

The only way to control that, theoretically, would be to run JavaScript on the client end, but the route would require some heroics. You'd need to

  1. Have the JS calculate the length of the text entered
  2. Have the JS keep the original value of the text in a hidden INPUT field with some sort of line separator (i.e., "i am a tex<LINEBREAK>tbox"), that way you can discern real linebreaks from the user with the linebreaks that the JS puts in to chop off the end of the line
  3. On submitting the form, don't use what's in the actual textbox but rather what's in the hidden INPUT field, with the <LINEBREAK>s replaced with empty strings.

But all of the above would require an enormous amount of effort, lots of testing across the various browsers, and breaks some of the built-in ASP.NET stuff (i.e., not using the contents of a textbox but rather a client side INPUT).


Replace all spaces with &nbsp; and it will wrap as you required in your question.

However, the behavior fails if the user types any actual spaces into the text area.

I've not tested the return-value on post-back. You would need to test post-back results with and without edits included.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号