Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a problem on resizing the asp.net textbox automatically according to text growing size which is retrieve from database.
Problem is Until i scroll or press the enter key the textbox wont resize. Any idea or hint to fixed this issue will be greatly appreciated.Thank you

What I have tried:

1. Control declaration:
ASP.NET
<asp:TextBox ID="txtrez" runat="server" width="100%" CssClass="img-responsive center" Text='<%# Eval("imgtext") %>' TextMode="MultiLine" style="resize:none;overflow:hidden; text-align:center" ReadOnly="True" onkeyup="AutoExpand(this)" Rows="2" BorderStyle="None"></asp:TextBox>




2. JavaScript function:


<script type="text/javascript">
function AutoExpand(txtbox) {
txtbox.style.height = "1px";
txtbox.style.height = (25+ txtbox.scrollHeight) + "px";
}
</script>
Posted
Updated 11-Jun-16 4:32am
v2

1 solution

Here is the simplest sample
HTML
<html>
   <head>
      <title>Adaptive input width sample</title>
      <style type="text/css">
         #meter { visibility: hidden; padding-left: 1em;
                  font-family: serif; font-size: 12pt; border: thick solid; }
         input { padding-left: 0.5em; font-family: serif; font-size: 12pt; }
      </style>
   </head>
<body>

<input id="test" type="text"/><span id="meter"/>

<script>

   setWidthSync(
	document.getElementById("test"),
	document.getElementById("meter"));

   function setWidthSync(target, meter) {
      var adjustWidth = function() {
         meter.innerHTML = target.value;
         target.style.width = meter.offsetWidth;
      };
      adjustWidth();
      target.onkeydown = function() { adjustWidth(); };
      target.onkeyup = function() { adjustWidth(); };
   } //setWidthSync

</script>

</body>
</html>

The idea is: you are using the hidden element meter (span, in my example) to measure actual width of the same text. The results can be slightly inaccurate due to different styles of the elements, so they should be kept close, via CSS style, with enough padding value for the meter element.

Also, this sample shows some flicker, because the modified text is rendered first, and only then the width of input is adjusted. Besides, the width is not accurately calculated, if a user inputs several blank space character in a row, because in HTML rendering such sequences are always rendered as one blank space. I'll leave improving this code for your home exercise. I hope you got the idea. :-)

—SA
 
Share this answer
 
v3
Comments
Kasar_7 11-Jun-16 10:58am    
Thank you,it worked on normal asp.net page but it wont work when im using master page since i can't add <html><body> tag and all. Any suggestion will be greatful
Vincent Maverick Durano 11-Jun-16 12:24pm    
You could place the script within your content page particularly under contenthead section.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900