...

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



Cloud9WD
01-02-2007, 08:22 PM
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:mad: ):


<%@ 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>

Freon22
01-03-2007, 06:03 PM
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 />")

Cloud9WD
01-03-2007, 11:09 PM
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...

CurtWRC
01-04-2007, 02:06 AM
This thread may help you:

http://www.codingforums.com/showthread.php?t=89002

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

Freon22
01-04-2007, 04:01 AM
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.

Cloud9WD
01-04-2007, 04:29 AM
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...


<%
' 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...

Freon22
01-05-2007, 01:50 PM
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.

otaku149
01-05-2007, 02:44 PM
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#


string input = "database content here";
string output = Regex.Replace(input, @"\r", "<br/>", RegexOptions.Multiline);
Response.Write(output);

VB.NET


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:


<Columns>
<asp:TemplateColumn HeaderText="Poem">
<ItemTemplate>
<%# DisplayPoem(Container.DataItem("Poem")) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

And here is the DisplayPoem function:


string DisplayPoem(string Poem)
{
string output = Regex.Replace(Poem, @"\r", "<br/>", RegexOptions.Multiline);
return output;
}

Hope this help!

Cloud9WD
01-06-2007, 07:47 AM
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

otaku149
01-08-2007, 08:48 PM
The TemplateColumn in your DataGrid:


<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":


<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/

Cloud9WD
01-09-2007, 04:39 AM
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

Cloud9WD
01-09-2007, 05:37 AM
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...

otaku149
01-09-2007, 12:42 PM
This should fix the problem:


<%#DisplayPoem(DataBinder.Eval(Container.DataItem, "Poem").ToString())%>

Cloud9WD
01-09-2007, 08:46 PM
It works!

Thank you so much for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum