separating line by line from textbox with textmode=multiline

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 34
Reputation: shy_wani is an unknown quantity at this point 
Solved Threads: 0
shy_wani shy_wani is offline Offline
Light Poster

separating line by line from textbox with textmode=multiline

 
0
  #1
Mar 29th, 2007
hi..i'm new in using ASP.NET application. Please guide me as i've only about 30% knowledge on developing web application in .NET

ok..my problem is..
i have a textbox..
i wanted user to create multiple option by entering each option per line in the textbox...
in other words..if user want to enter 4 option, 1st option will be at 1st line, 2nd option at 2nd line....and so on..

so..when i create button "Submit"..it will automatically assume that data in the first line will be inserted to first row in table in my database...

can this be done? please somebody help me...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #2
Mar 29th, 2007
Consider using a ListBox web control.

1. You supply the options for the user to choose rather than free type (free text is always a validation headache for you to keep sh** out of your db)

2. ListBox has a multiselect mode (user can use shift or ctrl key to select more than one option if you set the property to true)

3. You can iterate the ListBox as it implements IEnumarable, which has gotta be easier than parsing a fat wedge of text from a textarea (which is what a multiline textbox is rendered as to the client)

Example code:

the aspx page
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>Untitled Page</title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <asp:ListBox ID="lstOptions" runat="server" SelectionMode="Multiple">
  13. <asp:ListItem Value="Option1" Text="Option1"></asp:ListItem>
  14. <asp:ListItem Value="Option2" Text="Option2"></asp:ListItem>
  15. <asp:ListItem Value="Option3" Text="Option3"></asp:ListItem>
  16. <asp:ListItem Value="Option4" Text="Option4"></asp:ListItem>
  17. </asp:ListBox>
  18. <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
  19. <br />
  20. <asp:Repeater ID="rprChoices" runat="server">
  21. <HeaderTemplate><ul></HeaderTemplate>
  22. <ItemTemplate><li><%# Container.DataItem %></li></ItemTemplate>
  23. <FooterTemplate></ul></FooterTemplate>
  24. </asp:Repeater>
  25. </div>
  26. </form>
  27. </body>
  28. </html>

the code behind
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections.Specialized;
  11.  
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16.  
  17. }
  18. protected void btnSubmit_Click(object sender, EventArgs e)
  19. {
  20. //declare an array to hold the selected options
  21. StringCollection options = new StringCollection();
  22.  
  23. //iterate through the list items in the list box
  24. foreach (ListItem item in lstOptions.Items)
  25. {
  26. //if the item was selected by the user this
  27. //store it in our array
  28. if(item.Selected)
  29. options.Add(item.Value);
  30. }
  31.  
  32. //I am merely confirming the choices back to the client
  33. //this is where you would save the contents of options
  34. //to your database.
  35. rprChoices.DataSource = options;
  36. rprChoices.DataBind();
  37. }
  38. }
  39. ;
Last edited by hollystyles; Mar 29th, 2007 at 4:32 pm. Reason: Add code example
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 34
Reputation: shy_wani is an unknown quantity at this point 
Solved Threads: 0
shy_wani shy_wani is offline Offline
Light Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #3
Mar 30th, 2007
thanks for your help..
so...i must use listbox ya? hehe..thanks
but...
this is c#...
can u write in vb.net?

huhu....
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #4
Mar 30th, 2007
can u write in vb.net?
Yes thanks, but personally I find typing VB syntax akin to scraping honey off a teaspoon, whereas typing c# syntax is more like licking it off! IYKWIM.

for example:
  1. StringCollection options = new StringCollection(); //MMMm MMmm baby!
  1. Dim options As New StringCollection() 'wordy wordy yuck!
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 34
Reputation: shy_wani is an unknown quantity at this point 
Solved Threads: 0
shy_wani shy_wani is offline Offline
Light Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #5
Mar 30th, 2007
hehe....thanks ya hollystyles..
may God bless u...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 34
Reputation: shy_wani is an unknown quantity at this point 
Solved Threads: 0
shy_wani shy_wani is offline Offline
Light Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #6
Mar 30th, 2007
aiyak..
but what if i want user to enter their choices...

my program is like...
i want to create a survey creator system..
user will enter their question..1 question may have more than 1 answers...
i want user to enter their choices..one question per line, then i'm gonna save the option line by line as list of options into my database b4 the system generate the template..

so..right now, my problem is to reaa line by line text in the textbox..and to save the list of options into database..

can u help me?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #7
Mar 30th, 2007
Oh dear, fussy fussy :cheesy:

Ok I add a TextBox for the user to add their free text and an add button that adds there text to the ListBox on postback.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>Untitled Page</title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <asp:TextBox ID="TextBox1" runat="server" Width="280" MaxLength="128"></asp:TextBox>
  13. <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
  14. <br />
  15. <asp:ListBox ID="lstOptions" runat="server" SelectionMode="Multiple" Width="280"></asp:ListBox>
  16. <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
  17.  
  18. <asp:Repeater ID="rprChoices" runat="server">
  19. <HeaderTemplate><ul></HeaderTemplate>
  20. <ItemTemplate><li><%# Container.DataItem %></li></ItemTemplate>
  21. <FooterTemplate></ul></FooterTemplate>
  22. </asp:Repeater>
  23. </div>
  24. </form>
  25. </body>
  26. </html>

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections.Specialized;
  11.  
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16.  
  17. }
  18. protected void btnSubmit_Click(object sender, EventArgs e)
  19. {
  20. //declare an array to hold the selected options
  21. StringCollection options = new StringCollection();
  22.  
  23. //iterate through the list items in the list box
  24. foreach (ListItem item in lstOptions.Items)
  25. {
  26. //store it in our array
  27. options.Add(item.Value);
  28. }
  29.  
  30. //I am merely confirming the choices back to the client
  31. //this is where you would save the contents of options
  32. //to your database.
  33. rprChoices.DataSource = options;
  34. rprChoices.DataBind();
  35. }
  36. protected void btnAdd_Click(object sender, EventArgs e)
  37. {
  38. if (TextBox1.Text != string.Empty)
  39. {
  40. lstOptions.Items.Add(new ListItem(TextBox1.Text, TextBox1.Text));
  41. TextBox1.Text = string.Empty;
  42. }
  43. }
  44. }
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 34
Reputation: shy_wani is an unknown quantity at this point 
Solved Threads: 0
shy_wani shy_wani is offline Offline
Light Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #8
Mar 30th, 2007
dear hollystyles...
this method works....
thank u...
arigatou gozaimas...
hope someday i'll be master in programming... :p
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: separating line by line from textbox with textmode=multiline

 
0
  #9
Mar 30th, 2007
Ne nadda mi amigo. With bells on !:p
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC