如何在 ASP.NET 中将文本框的宽度设置为与 MaxLength 相同
有没有办法可以将文本框的宽度限制为等于 MaxLength 属性?就我现在的情况而言,文本框控件放置在表格单元格中,如以下代码片段所示:(
<td class=TDSmallerBold style="width:90%">
<asp:textbox id="txtTitle" runat="server"
CausesValidation="true"
Text="Type a title here..be concise but descriptive. Include price."
ToolTip="Describe the item with a short pithy title (most important keywords first). Include the price. The title you provide here will be the primary text found by search engines that index Zipeee content."
MaxLength="60"
Width="100%">
</asp:textbox>
我知道我不应该使用 HTML 表格来控制布局..肯定是另一个主题)但是有没有办法将实际宽度限制为输入框中允许的最大字符数?
Is there a way I can limit the width of a textbox to be equal to the MaxLength property? In my case right now, the textbox control is placed in a table cell as in this snippet:
<td class=TDSmallerBold style="width:90%">
<asp:textbox id="txtTitle" runat="server"
CausesValidation="true"
Text="Type a title here..be concise but descriptive. Include price."
ToolTip="Describe the item with a short pithy title (most important keywords first). Include the price. The title you provide here will be the primary text found by search engines that index Zipeee content."
MaxLength="60"
Width="100%">
</asp:textbox>
(I know I should not use HTML tables to control layout..another subject for sure) but is there a way to constrain the actual width to the max number of characters allowed in the typed box?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以从代码(在 Page_Load 中调用)中设置它,如下所示:
我们的系统是数据库驱动的,我们有一个元数据表,用于存储每个变量的长度属性。我个人通过迭代 ControlCollection 和每个 TextBox 项来使用元数据,拉出变量元数据的长度,然后将其分配给 MaxLength 和 Width,但如果您已经设置了 MaxLength,则可以让它在此处执行 Widths方式。
You could set it from code (called in your Page_Load) as such:
Our system is database driven and we have a meta data table that stores a length property for each variable. I personally used the meta data by iterating over the ControlCollection and for each TextBox item, pulling the length of the variable's meta data and then assigning it to MaxLength and to Width though if you already have MaxLength set, you could have it do Widths in this manner.
它不会很精确,因为字符的宽度可能会有所不同。但如果您真的对此很认真,您可以使用一些 Javascript (jQuery) 来完成。步骤如下:
这是一种类似于您在 Facebook 上看到的自动扩展文本区域的技术,例如: http://james.padolsey.com/javascript/jquery-plugin-autoresize/
(在这种情况下,您是水平而不是垂直地进行操作。)
如果您需要真正的代码,请告诉我,我'我会尽力的。
It won't be precise, because characters can vary in width. But if you were really serious about this, you could do it with a bit of Javascript (jQuery). The steps would be:
It's a technique similar to the auto-expanding textareas you see on Facebook and such: http://james.padolsey.com/javascript/jquery-plugin-autoresize/
(In this case, you are doing it horizontally instead of vertically.)
If you need real code, let me know, I'll do my best.
我意识到这是一篇非常古老的帖子 - 这是我的答案,供其他人将来参考!
使用 Style 属性将宽度设置为 100%,而不是单独设置 width 属性。
例如,
这是在我工作的生产环境中尝试和测试的。它比使用 Javascript 更简单,并且可以在现有的 HTML 中工作。
至于为什么会这样,不幸的是我缺乏知识。
I realise this is a very old post - here's my answer for future reference for others!
Use the Style property to set width to 100% instead of the width property on its own.
e.g.
This is tried and tested in a production environment I work on. It's simpler than using Javascript and works in the existing HTML.
As to exactly why this works, unfortunately I am short of knowledge.
我使用 HTML 属性 cols
所以对于你的例子我会使用
我从未尝试过这个但我想知道是否有一种方法将两者结合在一起。因此,设置 MaxLength 也设置为 cols,将是一个有趣的练习
I use the HTML property cols
So for your example I would use
I have never tried this but i wonder if there is a way to tie both together. So setting MaxLength also sets to cols, would be an interesting exercise
我意识到这是一个老问题,但对于那些遇到它寻找如何基于 MaxLength 属性设置文本框样式的人,我使用以下 CSS:
如果您有很多具有不同 MaxLengths 的文本框,那么这可能是不会是你的解决方案。但是,如果您希望文本框的呈现与要输入的内容相匹配,我认为这是一个很好的解决方案。
I realise that this is a old question, but for those people who come across it looking for how to style text boxes based on the MaxLength attribute I use the following CSS:
If you have lots of text boxes with different MaxLengths, then this probably isn't going to be the solution for you. However if you want the presentation of a text box to match what is to be input into it, I think it's a good solution.
这是假设字符大约 10 像素。它非常适合我的需要。
This is making an assumption that characters are roughly 10px. It works quite well for my needs.