根据其中包含的数据设置文本框大小 [英] Set textbox size as per data contained in it

查看:91
本文介绍了根据其中包含的数据设置文本框大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想根据其中包含的文字设置文本框的大小。我知道它可以使用在文本框的keyup事件上调用的javascript来实现。但在我的情况下,我从数据库中选择一个数据,所以我不会写入它。 caode在.ascx.cs页面上,所以我不想在这里使用javascript。请帮助





如何设置标签的最大宽度,高度按文字自动调整大小,但宽度应该是限制的



我尝试过:



Hello,

I want to set a size of textbox according to the text contain in it. I know it can be implemented using javascript called on keyup event of textbox. but in my case I selecting a data from database so i am not going to write into it. the caode is on .ascx.cs page so I do not want to use javascript here. please help


how to set max width of label and height is autosize as per text but width should be in limit

What I have tried:

<asp:TextBox ID="post" runat="server"  CommandArgument="<%#Container.DataItemIndex %>"  Text=' <%#DataBinder.Eval(Container.DataItem, "Message")%>' Font-Size="Smaller" TextMode="MultiLine"  Enabled="true" OnLoad="resizeTextBox(this)"   OnCommand="txtSuggestion_Command"  Width="290px"  ><%--onkeyup="AutoGrowTextArea(this)"   --%>

this is textbox in .ascx page

<script type = "text/javascript">
        function resizeTextBox(txt) {
            txt.style.height = "1px";
            txt.style.height = (1 + txt.scrollHeight) + "px";
        }
    </script>m





这是用于调整主页中文本框大小的脚本在.ascx中写它没有用。



this is script for resize a textbox which is in masterpage as to write it in .ascx does not make use.

推荐答案

这是一个错误的想法。 文本框呈现为HTML输入元素或 textarea 。如果它总是不可编辑,那么使用这个类是没有意义的,但如果它是可编辑的,那么用户可以在那里放置任何文本,因此,无论你如何操纵大小,你都不可能适合所有文本。此外,几乎不可能正确评估文本边界。另外,不要忘记 textarea 可以选择包装单词,以显示多行中的单行文本,具体取决于当前控件的宽度。所以,我的建议是:选择页面布局定义的控件的大小,而不是文本的值。在需要时使用滚动条。



对于所有不可编辑的HTML元素,这已经完成:大小由元素的内部内容控制,除非你用一些不合理的样式来防止这种默认行为,所以不要阻止它。



-SA
This is a wrong idea. "Text box" is rendered as HTML input element or textarea. If it is always not editable, it would be pointless to use this class, but if it is editable, the user can put any text there, so, no matter how you manipulate the size, you cannot possibly fit all the text. Besides, it's nearly impossible to evaluate the text bounds correctly. Also, don't forget that textarea can optionally wrap words, to show a single line of text in several lines, depending on current control width. So, my advice is: choose the size of the control defined by your page layout, not text value of it. Use scroll bars instead, when required.

And for all non-editable HTML element, this is already done: the size is controlled by the inner content of the element, unless you prevent this default behavior with some unreasonable styles, so just don't prevent it.

—SA


你没有很多选择,只能使用javascript:你不知道文本的大小,直到它实际在客户端浏览器上呈现,因为它将取决于使用的字体 - 而你可以建议一个字体,除非它安装在浏览器将替代的客户端上,你无法告诉服务器端是否会发生这种情况。并且用户可以自由地使用系统范围的文本放大以及在浏览器中更改默认字体大小。

您可以使用C#和MeasureString,假设客户端具有特定的字体和文本放大率,但是根本不能保证显示的结果适合。
You don't have a lot of choice but to use javascript for this: you don't know the size of the text until it's actually rendered at the client browser because it will depend on the font used - and while you can suggest a font unless it is installed on the client the browser will substitute, and you can't tell at the server side if this is going to happen. And the user is at liberty to use system wide text magnification as well as changing the default font size in his browser.
You can use C# and MeasureString assuming a particular font and text magnification at the client, but there is no guarantee at all that the displayed result will fit.


这篇关于根据其中包含的数据设置文本框大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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