多行文本框

时间:2010-07-14 13:21:42

标签: asp.net

我的问题是,如果用户在mulitiline文本框中输入一个句子/单词并按Enter键 那个按下进入buuton的单词应该存储在数据库中。 表示用户是否以格式输入一个textarea 1 GB 2 GB 3 GB 80 GB 作为过滤器选项,然后在数据库中这些应存储为不同的行。 希望你能理解我的问题。

2 个答案:

答案 0 :(得分:1)

所以您的用户输入:

1GB
2GB
3GB
80GB

并且您希望将它们分开,以便将它们保存为数据库中的四个单独记录?

在字符串上使用Split函数允许您根据字符串分隔符将字符串分割为数组。在您的情况下,我们使用换行符分割原始文本框字符串:

protected void Button1_Click(object sender, EventArgs e)
    {
        string textLines;
        string[] textLine;

        textLines=MultilineTextBox.Text;

        textLine = textLines.Split(Environment.NewLine.ToArray(),  StringSplitOptions.RemoveEmptyEntries);

        // At this point the textLines array will have four entries in it, one for each line in the textbox on the form
    }

然后,您可以遍历数组以将行保存到数据库中。

答案 1 :(得分:0)

我建议最好使用文本框Singleline和Listbox的组合。但是如果你想要一个Multiline文本框(并且没有Ajax)看看:

function checkEnter(e){ 
        var characterCode
        if(e && e.which){ 
            e = e
            characterCode = e.which 
        } else {
            e = event
            characterCode = e.keyCode
        }
        if(characterCode == 13){ 
            var txt=document.getElementById('<%= TextBox1.ClientId%>');
            txt.blur(); 
        }
    }

    function moveCursorToEndOfTextBox(){
        var txt=document.getElementById('<%= TextBox1.ClientId%>');
        txt.focus();
        txt.value = txt.value+'\n';
    }


<asp:TextBox ID="TextBox1" onkeypress="checkEnter(this)" AutoPostBack="true" OnTextChanged="TextChanged" TextMode="MultiLine"  runat="server"></asp:TextBox>

并在Codebehind中:

Protected Sub TextChanged(ByVal sender As Object, ByVal evt As EventArgs)
    Dim allLines As New List(Of String)(Microsoft.VisualBasic.Strings.Split(Me.TextBox1.Text.Trim, vbCrLf))
    Dim newLine As String = allLines(allLines.Count - 1)
    'save new item to db .....

    'register script to jump into textbox and got to end of text
    ScriptManager.RegisterStartupScript(Me, Me.GetType, "moveCursorToEndOfTextBox", "moveCursorToEndOfTextBox();", True)
    End Sub