Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do you replace line breaks with <br> for displaying in a .net database?

    Hi everyone, I'm new here. I've been trying for like a week to get this to work and finally my friend recommended this forum.

    Basically I want the line breaks entered in a form to be displayed when i show my database results.

    Here's the code for the page (yes i know the main problem i have is that i'm using frontpage, but the other person who's going to be using this needs to be able to access and edit the database through FP ):
    Code:
    <%@ Page Language="C#" Debug="true"%>
    <%@ Register Tagprefix="FPDB" TagName="DBRegion" Src="_fpclass/dbregion.ascx"%>
    <html>
    
    <head>
    <meta name="ProgId" content="SharePoint.WebPartPage.Document">
    <meta name="WebPartPageExpansion" content="full">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Poem</title>
    <script language="C#" runat="server" src="_fpclass/fputil.cs"></script>
    </head>
    
    <body>
    
    <form runat="server">
    	<asp:DataGrid ID="Region0" ItemStyle-BackColor="LightGrey" AlternatingItemStyle-BackColor="White" HeaderStyle-Font-Bold="" BorderWidth="1" BorderColor="Black" AutoGenerateColumns="False" OnItemDataBound="Item_Bound" Runat="server">
    <Columns>
    <asp:BoundColumn DataField="Poem" HeaderText="Poem"/>
    </Columns>
    </asp:DataGrid>
    	<p>&nbsp;<asp:Label ID="Region0NoResults" Text="No records returned." Runat="server" Visible="False"></asp:Label><FPDB:DBRegion Target="Region0" DBName="poems" TableKey="Key" RootPath="./" NoResults="Region0NoResults" TableName="Results" MaxRecords="256" Runat="server"></FPDB:DBRegion></p>
    </form>
    
    </body>
    
    </html>
    Last edited by Cloud9WD; 01-02-2007 at 08:24 PM. Reason: meh, forget the code tags

  • #2
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Not sure if this is what you are looking for. But I use the replace function before I add something to the database that is going to require a <br /> between lines when called for display.

    Poem = Replace(Poem,vbCrLf,"<br />")

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah see i don't really know where to put that though

    btw, it says page language is c# but it seems kinda sketchy that ms wouldn't be using visual basic...

  • #4
    Regular Coder CurtWRC's Avatar
    Join Date
    May 2005
    Location
    UK
    Posts
    224
    Thanks
    9
    Thanked 1 Time in 1 Post
    This thread may help you:

    HTML tags in posts

    It should teach you how to create a function to replace parts of your text when displaying it. I found it very very useful

  • #5
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Cloud9WD View Post
    yeah see i don't really know where to put that though

    btw, it says page language is c# but it seems kinda sketchy that ms wouldn't be using visual basic...
    I am studing VB. But C# and VB is close in dot.net, the Syntax is the big differents. I think the replace function is the same in both VB and C#. What you need is someone who know C# and how to use the replace function within a DataField to post to this thread.

    I use the replace function before I put it into my database. This way I don't to worry about having to use it when I am trying to display it.

    Edit: You may want to do a google search "Replace Fuction in C#" and see what you get. Sorry I could not be of more help.
    Last edited by Freon22; 01-04-2007 at 04:07 AM. Reason: Add more infromation

  • #6
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My first idea was to use a javascript function to replace things before they were entered into the database, but I figured all the frontpage-generated **** in my form page might keep that from working out...

    here's the code though...

    Code:
    <%
    ' FP_ASP ASP Automatically generated by a FrontPage Component. Do not Edit.
    
    On Error Resume Next
    Session("FP_OldCodePage") = Session.CodePage
    Session("FP_OldLCID") = Session.LCID
    Session.CodePage = 1252
    Err.Clear
    
    strErrorUrl = ""
    
    If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    If Request.Form("VTI-GROUP") = "0" Then
    	Err.Clear
    
    	Set fp_conn =  Server.CreateObject("ADODB.Connection")
    	FP_DumpError strErrorUrl, "Cannot create connection"
    
    	Set fp_rs = Server.CreateObject("ADODB.Recordset")
    	FP_DumpError strErrorUrl, "Cannot create record set"
    
    	fp_conn.Open Application("poems_ConnectionString")
    	FP_DumpError strErrorUrl, "Cannot open database"
    
    	fp_rs.Open "Results", fp_conn, 1, 3, 2 ' adOpenKeySet, adLockOptimistic, adCmdTable
    	FP_DumpError strErrorUrl, "Cannot open record set"
    
    	fp_rs.AddNew
    	FP_DumpError strErrorUrl, "Cannot add new record set to the database"
    	Dim arFormFields0(3)
    	Dim arFormDBFields0(3)
    	Dim arFormValues0(3)
    
    	arFormFields0(0) = "Title"
    	arFormDBFields0(0) = "Title"
    	arFormValues0(0) = Request("Title")
    	arFormFields0(1) = "Poem"
    	arFormDBFields0(1) = "Poem"
    	arFormValues0(1) = Request("Poem")
    	arFormFields0(2) = "Name"
    	arFormDBFields0(2) = "Name"
    	arFormValues0(2) = Request("Name")
    
    	FP_SaveFormFields fp_rs, arFormFields0, arFormDBFields0
    
    
    	fp_rs.Update
    	FP_DumpError strErrorUrl, "Cannot update the database"
    
    	fp_rs.Close
    	fp_conn.Close
    
    	FP_FormConfirmation "text/html; charset=windows-1252",_
    						"Form Confirmation",_
    						"Thank you for submitting the following information:",_
    						"test_form.asp",_
    						"Return to the form."
    
    End If
    End If
    
    Session.CodePage = Session("FP_OldCodePage")
    Session.LCID = Session("FP_OldLCID")
    
    %>
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 1</title>
    
    
    </head>
    
    <body>
    
    <form name="poemsubmission" method="POST" action="--WEBBOT-SELF--">
    	<!--webbot bot="SaveDatabase" SuggestedExt="asp" S-DataConnection="poems" S-RecordSource="Results" U-Database-URL="fpdb/poems.mdb" S-Form-Fields="Title Poem Name" S-Form-DBFields="Title Poem Name" U-ASP-Include-Url="_fpclass/fpdbform.inc" startspan --><input TYPE="hidden" NAME="VTI-GROUP" VALUE="0"><!--#include file="_fpclass/fpdbform.inc"--><!--webbot bot="SaveDatabase" endspan i-checksum="40548" -->
    	<p><input type="text" name="Name" size="20" value="Name"></p>
    	<p><input type="text" name="Title" size="20" value="Title"></p>
    	<p><textarea rows="10" name="Poem" cols="40">Poem</textarea></p>
    	<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>
    </body>
    
    </html>
    thanks for all the help guys, if it wasnt for all of frontpage's quirks you probably would have solved my problem...

  • #7
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    I was hoping someone else who knows C# would have posted to your thread. But since they haven't try this.

    Comment out this line
    'arFormValues0(1) = Request("Poem")

    and add this line in its place. Test it and see if it works.
    arFormValues0(1) = Request(Replace("Poem",vbCrLf,"<br />"))

    If it works then great if not then just delete the line and uncomment the first line and you are back to what it was before.

  • #8
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    You don't need to do anything during your insert because the lines break are inserted automatically in the database as an hidden character when the user press on "enter". All you need to do is to replace these characters by <br/> when you display your data like below:

    C#
    Code:
    string input = "database content here";
    string output = Regex.Replace(input, @"\r", "<br/>", RegexOptions.Multiline);
    Response.Write(output);
    VB.NET
    Code:
    Dim input As String = "database content here"
    Dim output As String = input.Replace(Chr(13), "<br/>")
    Response.Write(output)
    To display the result into your datagrid, you should change your BoundColumn to TemplateColumn and call the DisplayPoem function that receive the "Poem" as parameter:
    Code:
    <Columns>
    	<asp:TemplateColumn HeaderText="Poem">
    		<ItemTemplate>
    			<%# DisplayPoem(Container.DataItem("Poem")) %> 
    		</ItemTemplate>
    	</asp:TemplateColumn>
    </Columns>
    And here is the DisplayPoem function:
    Code:
    string DisplayPoem(string Poem)
    {
        string output = Regex.Replace(Poem, @"\r", "<br/>", RegexOptions.Multiline);
        return output;
    }
    Hope this help!
    Last edited by otaku149; 01-05-2007 at 03:02 PM.

  • #9
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the "Parser Error" i get:

    Parser Error Message: System.Web.UI.WebControls.DataGridColumnCollection must have items of type 'System.Web.UI.WebControls.DataGridColumn'. 'Itemtemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.

    at the line where it says <ItemTemplate> of course


    also, should the DisplayPoem function code go between the script tags on my page (that's what i did), should I make new script tags (this one says:
    <script language="C#" runat="server" src="_fpclass/fputil.cs">
    ), or am i putting it in the wrong place altogether

  • #10
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The TemplateColumn in your DataGrid:
    Code:
    <asp:DataGrid ID="Region0" AutoGenerateColumns="False" Runat="server">
    	<Columns>
    		<asp:TemplateColumn HeaderText="Poem">
    			<ItemTemplate>
    				<%# DisplayPoem(Container.DataItem("Poem")) %> 
    			</ItemTemplate>
    		</asp:TemplateColumn>
    	</Columns>
    </asp:DataGrid>
    The function in your script runat="server":
    Code:
    <script runat="server">
      
        	protected void Page_Load(object sender, EventArgs e)
        	{       
            	//code to populate your datagrid
        	}
    
    	string DisplayPoem(string Poem)
    	{
        		string output = Regex.Replace(Poem, @"\r", "<br/>", RegexOptions.Multiline);
        		return output;
    	}
    
    </script>
    Also you should use the "completely free" Visual Web Developer 2005 Express Edition instead, no one is using Frontpage for doing asp.net

    Download Visual Web Developer 2005 Express Edition:
    http://msdn.microsoft.com/vstudio/express/vwd/download/
    Last edited by otaku149; 01-08-2007 at 08:57 PM.

  • #11
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah but as stated originally, the other user needs to be able to have full control of the database through frontpage

    if vwd05 integrates with frontpage that well then i'll definitely give it a try

  • #12
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    otaku, thanks for all the help

    unfortunately...there's a new problem now...

    ok so i did what you said and I get a compilation error:

    CS0118: 'System.Web.UI.WebControls.DataGridItem.DataItem' is a 'property' but is used like a 'method'

    for: <%# DisplayPoem(Container.DataItem("Poem")) %>

    I did some googling and the only useful thing i found was to change the () around "Poem" to []...

    so, after doing that i get this error:

    CS0021: Cannot apply indexing with [] to an expression of type 'object'


    thanks for all the help so far, only one line of code now stands between me and victory....or something....i hope...

  • #13
    Regular Coder
    Join Date
    May 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This should fix the problem:
    Code:
    <%#DisplayPoem(DataBinder.Eval(Container.DataItem, "Poem").ToString())%>

  • #14
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works!

    Thank you so much for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •