我们在网页中填写在TextBox中文本文件中可以有一定的格式,但是当存入数据
库后再次显示的时候,这些格式已经当然无存,怎么才能保存住格式呢?
在这里用了一个函数(HtmlEncode)来处理,在保存时将数据处理,添加了格式
在存储的字段里。下面是效果图(有点粗糙^_^)
![]()
Codebehind part
Public Class WebForm1
Inherits System.Web.UI.Page
![]()
#Region " Web Form Designer Generated Code "
![]()
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
![]()
End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
![]()
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
![]()
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
![]()
#End Region
![]()
Public sss As String
Public Function HtmlEncode(ByVal str As String) As String
Dim L As Integer
Dim result As String
If str = "" Then
Return ""
End If
L = Len(str)
Dim i As Integer
For i = 1 To L
Select Case Mid(str, i, 1)
Case Chr(13)
result = result + "<br>"
Case Chr(34)
result = result + """
Case Chr(32)
'result=result+" "
If i + 1 <= L And i - 1 > 0 Then
If Mid(str, i + 1, 1) = Chr(32) Or Mid(str, i + 1, 1) = Chr(9) Or Mid(str, i - 1, 1) = Chr(32) Or Mid(str, i - 1, 1) = Chr(9) Then
result = result + " "
Else
result = result + " "
End If
Else
result = result + " "
End If
Case Chr(9)
result = result + " "
Case Else
result = result + Mid(str, i, 1)
End Select
Next
Return result
End Function
![]()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sss = HtmlEncode(TextBox1.Text)
' sss = TextBox1.Text
End Sub
End Class
HTML Part
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication3.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"
Width="360px" Height="144px" TextMode="MultiLine"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 200px" runat="server"
Width="112px" Text="Display"></asp:Button>
<TABLE id="Table1" style="Z-INDEX: 103; LEFT: 40px; WIDTH: 360px; POSITION: absolute; TOP: 248px; HEIGHT: 128px"
cellSpacing="1" cellPadding="1" width="360" border="1">
<TR>
<td><font size="2"><%=sss%><font></font></font></td>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>
库后再次显示的时候,这些格式已经当然无存,怎么才能保存住格式呢?
在这里用了一个函数(HtmlEncode)来处理,在保存时将数据处理,添加了格式
在存储的字段里。下面是效果图(有点粗糙^_^)

Codebehind part
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Public sss As String
Public Function HtmlEncode(ByVal str As String) As String
Dim L As Integer
Dim result As String
If str = "" Then
Return ""
End If
L = Len(str)
Dim i As Integer
For i = 1 To L
Select Case Mid(str, i, 1)
Case Chr(13)
result = result + "<br>"
Case Chr(34)
result = result + """
Case Chr(32)
'result=result+" "
If i + 1 <= L And i - 1 > 0 Then
If Mid(str, i + 1, 1) = Chr(32) Or Mid(str, i + 1, 1) = Chr(9) Or Mid(str, i - 1, 1) = Chr(32) Or Mid(str, i - 1, 1) = Chr(9) Then
result = result + " "
Else
result = result + " "
End If
Else
result = result + " "
End If
Case Chr(9)
result = result + " "
Case Else
result = result + Mid(str, i, 1)
End Select
Next
Return result
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sss = HtmlEncode(TextBox1.Text)
' sss = TextBox1.Text
End Sub
End ClassHTML Part
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication3.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"
Width="360px" Height="144px" TextMode="MultiLine"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 200px" runat="server"
Width="112px" Text="Display"></asp:Button>
<TABLE id="Table1" style="Z-INDEX: 103; LEFT: 40px; WIDTH: 360px; POSITION: absolute; TOP: 248px; HEIGHT: 128px"
cellSpacing="1" cellPadding="1" width="360" border="1">
<TR>
<td><font size="2"><%=sss%><font></font></font></td>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>




浙公网安备 33010602011771号