2010-07-14 74 views
0

我的问题,如果用户在多文本文本框中输入一个句子/单词,并按下输入按钮 那么该单词被压入beuton应存储在数据库中。 表示如果用户以格式输入一个文本区域 1 GB 2 GB 3 GB 80 GB 作为过滤器选项,则在数据库中这些应该存储为不同的行。希望你了解我的问题。多行文本框

+0

家庭作业?请告诉我们你的代码。 – adatapost 2010-07-14 13:26:12

回答

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 
    } 

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

0

我会建议更好地使用文本框单行和列表框的组合。但是,如果你想有一个多行TextBox(和没有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> 

和代码隐藏:

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