0

I'm trying to use a textarea to grab user input as separate entries from a textarea.

My Textarea:

<asp:TextBox runat="server" TextMode="MultiLine" ID="uploadFieldChoices"></asp:TextBox>

Value of textarea as seen on textarea:

enter image description here

Value of textarea as retrieved by code:

enter image description here

Then I'll want store that in XML and use it to generate a dropdownlist that has those three entries as options.

To grab that what I'm trying to do is split the textarea value on line breaks and use that as my values to store as XmlNodes:

Code

If lstrFieldType = "Dropdown" Then
    Dim lnodChoices As XmlNode = ldocFieldList.CreateElement("options")
    Dim larrSplit() As String = uploadFieldChoices.Text.Split(Environment.NewLine)
    For Each lstrOption As String In larrSplit
        If Not lstrOption.Trim = "" Then
            Dim lnodOption As XmlNode = ldocFieldList.CreateElement("option")
            lnodOption.InnerText = lstrOption
            lnodChoices.AppendChild(lnodOption)
        End If
    Next
    lnodUploadField.AppendChild(lnodChoices)
End If

But so far I can't get larrSplit to actually give me more than one value. Weird thing is, even though the above only gives me a single XmlNode, the xml file will actually have line breaks in the entry!

I know that a normal VB textbox has a ".lines" property that gives what I'm looking for, but isn't a property in the Web.Textbox control.

I've also tried:

Dim larrSplit() As String = uploadFieldChoices.Text.Split("\n\r")
Dim larrSplit() As String = uploadFieldChoices.Text.Split(vbNewLine)
Dim larrSplit() As String = uploadFieldChoices.Text.Split(vbCrLf)

But none work.

Why is this not working, and is there a better way completely different than the above?

Thanks!

Edit

Included code for textarea as it may be relevant.

Edit 2

Included images of textbox and value.

Blunderfest
  • 1,854
  • 1
  • 28
  • 46
  • 1
    What is the *exact* value of `uploadFieldChoices.Text`? – Patrick Hofman Jul 21 '14 at 18:08
  • If you output the values of the characters in `uploadFieldChoices.Text` then you will be able to see what to split on. For testing purposes, you can temporarily put an `asp:Label` on the form and use that for the display, or write to a file. – Andrew Morton Jul 21 '14 at 18:41

2 Answers2

1

I made a crude web form in VS2008 with these controls:

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />

and used this code:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim t = TextBox1.Text
    Dim s As String = ""
    For Each c In t
        s &= Asc(c).ToString() & " "
    Next
    Label1.Text = s

    ' this part added after checking what the line separators are in a multi-line TextBox
    Dim myLines = t.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
    Dim u As String = ""
    For Each l In myLines
        u &= String.Format("*{0}* ", l)
    Next
    Label2.Text = u

End Sub

To get this result:

enter image description here

The data visualizer in the debugger will not show line breaks as line breaks unless you click the expander next to the little magnifying glass icon and choose "Text Visualizer".

Related: What character represents a new line in a text area.

So the answer is to confirm that the .Text from the TextArea has the expected line separators and split it as shown above.

Community
  • 1
  • 1
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • I used your code exactly, and I receive asterisks around the outside of my string. `*First Second Third Answer Fourth*` There must be a setting I'm missing. – Blunderfest Jul 30 '14 at 13:36
  • @Blunderfest How are you populating the TextArea? What character codes do you see between 116 and 83 (the "t" of "First" and the "S" of "Second")? – Andrew Morton Jul 30 '14 at 13:44
  • I see the same character codes as above. `10`, however, I'm unable to split on them. I've worked around the issue for now by splitting it client-side with javascript and using the value from that split. – Blunderfest Jul 30 '14 at 14:36
  • You should be seeing `13 10` as those are the codes for CR and LF respectively. If you change the `{vbCrLf}` in my code to `{vbCrLf, vbLf}` then it should work. – Andrew Morton Jul 30 '14 at 15:26
  • That works! Why is it necessary since vbCrLf is both a carriage return and a linefeed? Would you mind editing your answer to include what you put in that comment? – Blunderfest Jul 30 '14 at 16:21
  • 1
    @Blunderfest Splitting on vbCrLf means that exactly vbCrLf is required, not just part of it. Somehow your TextArea has the non-standard vbLf as its line separators. You might as well leave the vbCrLf in as a separator just in case it reverts to the standard. – Andrew Morton Jul 30 '14 at 16:23
0

Did you try something like this? This will split on "\n", "\r", "\n\r" or a combination of them and will store the result in the string array.

Dim larrSplit As String() = uploadFieldChoices.Text.Split(new String() {Environment.NewLine},
                                   StringSplitOptions.None)

Edit: Added picture with the result. enter image description here Edit 1: Based on your edited question it seems like your textarea values are not really split by linebreaks but rather whitespaces. Try the updated code by specifying String.Split with no parameters and this will split by whitespace and you'll get your three values if you additionally check for If Not lstrOption.Trim = "" Then in your For Each loop.

 Dim larrSplit As String() = uploadFieldChoices.Text.Split()

 For Each lstrOption As String In larrSplit
     If Not lstrOption.Trim = "" Then
         ....
         Response.Write(lstrOption.ToString())
     End If
 Next
Dennis R
  • 3,195
  • 1
  • 19
  • 24