Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

c# - How can I make a Sharepoint textbox (input, type=text) dynamically Multiline?

I want a textbox on my Web Part that will grow vertically on demand. That is, it will be one line unless the user enters too much text for that line, at which point it word wraps and grows vertically to accommodate the verbosity of the user.

I am creating my controls/elements dynamically, and I create this element like so:

boxPaymentExplanation = new TextBox()
{
    CssClass = "dplatypus-webform-field-input"
};
boxPaymentExplanation.Width = 660;
boxPaymentExplanation.Style.Add("display", "inline-block");

I tried adding this line, in the hopes of achieving this functionality:

boxPaymentExplanation.Style.Add("TextMode", "MultiLine");

...but it makes no apparent change to the textbox's behavior - I can enter text into it "until the bovines come back to the barn" but it simply keeps adding the characters to the end of the textbox on a single row. It never wraps, so it never grows.

UPDATE

This is the jQuery that works (derived from the link that Christopher Jennings provided):

$(document).on("keyup", "[id$=explainPaymentTextBox]", function (e) {
    while ($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth"))) {
        $(this).height($(this).height() + 1);
    };
});

...along with this C#:

boxPaymentExplanation = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    ID = "explainPaymentTextBox"
};
boxPaymentExplanation.Width = 660;
boxPaymentExplanation.Style.Add("display", "inline-block");
boxPaymentExplanation.TextMode = TextBoxMode.MultiLine;

UPDATE 2

Unfortunately, although the descent-into-the-m?lstr?m-esque jQuery above works for dynamically growing the textbox, it doesn't work if the user removes text; I would like it also to shrink when that happens...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're on the right track. You need to set the TextMode property to Multiline. However, the approach you took is to add an HTML tag attribute rather than set the .NET property. Simply replace boxPaymentExplanation.Style.Add("TextMode", "MultiLine"); with boxPaymentExplanation.TextMode = TextBoxMode.MultiLine;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...