计算没有换行符的asp:TextBox(textarea)的高度 [英] Calculate the height of an asp:TextBox (textarea) without linebreaks

查看:42
本文介绍了计算没有换行符的asp:TextBox(textarea)的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp:TextBox(呈现为 textarea),我需要计算该 TextBox 的高度以适合打印内容.我用这个代码计算 textarea 的高度:

I have an asp:TextBox (which renders as an textarea) and I need to calculate the height of that TextBox to fit the content for printing. I calculate the height of the textarea with this code:

// get the number of lines 
int numberOflines = txtText.Text.Split(new Char[] { '\n' }, 
    StringSplitOptions.RemoveEmptyEntries).Length;
    
//calculate the target hight of the textBox
txtText.Rows = numberOflines;
txtText.Style.Add("height", TextBoxHeight.calculate(11, 8, numberOflines, 9999999) 
    + "px;");

代码工作正常 - 只要每一行都有一个换行符,但如果不是这样,结果高度就会很小.如何获得与换行符无关的正确高度?

The code works fine - as long there is a line break for every line, but if this is not the case the resulting height is to small. How can I get the correct height independend of line breaks?

我已经尝试了多种使用 javascript 的方法,但它们需要像 onkeyup 这样的用户操作(在我的情况下,用户可以更改文本,但他们不应该这样做),或者他们还根据换行符的数量进行一些计算.

I have tried multiple ways with javascript but they require user actions like onkeyup (in my case users can change the text but they shouldn't have to) or they also do some calculations based on the number of linebreaks.

提前致谢.

推荐答案

如果你有一个可靠的方法,它在某种用户交互后起作用,比如 onkeyup 为什么不在使用可靠方法之前先手动触发它?

If you have a reliable method which works after some sort of user interaction like onkeyup why not just trigger that first manually before using the reliable method?

   $(document).ready(function(){ 
     $("button").click( function(){
       $("#txt").trigger("onkeyup");  // manually invoke keyup event
     });
   });

  <button>Manual trigger keyup</button>
  <input type="text" onkeyup="alert('up');" id="txt" />

这篇关于计算没有换行符的asp:TextBox(textarea)的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆