如何在 ASP.NET 中将文本框的宽度设置为与 MaxLength 相同

发布于 2024-09-05 00:40:45 字数 697 浏览 8 评论 0原文

有没有办法可以将文本框的宽度限制为等于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

剧终人散尽 2024-09-12 00:40:45

您可以从代码(在 Page_Load 中调用)中设置它,如下所示:

 txtTitle.Width = new Unit(txtTitle.MaxLength, UnitType.Em);

我们的系统是数据库驱动的,我们有一个元数据表,用于存储每个变量的长度属性。我个人通过迭代 ControlCollection 和每个 TextBox 项来使用元数据,拉出变量元数据的长度,然后将其分配给 MaxLength 和 Width,但如果您已经设置了 MaxLength,则可以让它在此处执行 Widths方式。

You could set it from code (called in your Page_Load) as such:

 txtTitle.Width = new Unit(txtTitle.MaxLength, UnitType.Em);

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.

夏天碎花小短裙 2024-09-12 00:40:45

它不会很精确,因为字符的宽度可能会有所不同。但如果您真的对此很认真,您可以使用一些 Javascript (jQuery) 来完成。步骤如下:

  • 在屏幕外创建一个跨度(顶部:-1000px 或其他)
  • 确保跨度与文本框具有相同的样式/类
  • 用 60 个字符填充跨度
  • 使用 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:

  • Create a span offscreen (top:-1000px or something)
  • Make sure the span has the same styles/classes as your text box
  • Fill the span with 60 characters
  • Use jQuery to measure the width of the span
  • Apply that width to the text box

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.

行至春深 2024-09-12 00:40:45

我意识到这是一篇非常古老的帖子 - 这是我的答案,供其他人将来参考!

使用 Style 属性将宽度设置为 100%,而不是单独设置 width 属性。

例如,

<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="Blah Blah I don't like horizontal scroll-bars" 
    MaxLength="60"
    Style="Width: 100%"
  >
</asp:textbox>

这是在我工作的生产环境中尝试和测试的。它比使用 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.

<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="Blah Blah I don't like horizontal scroll-bars" 
    MaxLength="60"
    Style="Width: 100%"
  >
</asp:textbox>

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.

橘香 2024-09-12 00:40:45

我使用 HTML 属性 cols

所以对于你的例子我会使用

     <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="Blah Blah I don't like horizotal scroll-bars" 
        MaxLength="60"
        Width="100%"
         Cols="60" 
      >
    </asp:textbox>

我从未尝试过这个但我想知道是否有一种方法将两者结合在一起。因此,设置 MaxLength 也设置为 cols,将是一个有趣的练习

I use the HTML property cols

So for your example I would use

     <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="Blah Blah I don't like horizotal scroll-bars" 
        MaxLength="60"
        Width="100%"
         Cols="60" 
      >
    </asp:textbox>

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

别低头,皇冠会掉 2024-09-12 00:40:45

我意识到这是一个老问题,但对于那些遇到它寻找如何基于 MaxLength 属性设置文本框样式的人,我使用以下 CSS:

input[maxlength='2'] { width: 40px;}

如果您有很多具有不同 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:

input[maxlength='2'] { width: 40px;}

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.

孤独患者 2024-09-12 00:40:45
$(':input[maxlength]').each(function (i, e) {$(e).width(e.maxLength * 10)});

这是假设字符大约 10 像素。它非常适合我的需要。

$(':input[maxlength]').each(function (i, e) {$(e).width(e.maxLength * 10)});

This is making an assumption that characters are roughly 10px. It works quite well for my needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文