2010-07-14 77 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