假設date
是一個字符串變量,你想做到以下幾點:
log = trim(request.form("log1")) & "<br>" & [date] & ": " & trim(request.form("txb1"))
而且,如果date
是一個DateTime變量,你會想要使用date.ToShortDateString()
而不是<br/>
我會建議使用Environment.NewLine
,甚至更好,你應該使用StringBuilder:
Dim SB As New StringBuilder()
SB.AppendLine(trim(request.form("log1")))
SB.AppendLine([date] & ": " & trim(request.form("txb1")))
log = SB.ToString()
UPDATE:如果你想整個日誌存儲在一個記錄,而不是一個單獨的表
,你最好將其保存作爲varbinary(MAX)
列中的日誌列表。
這裏是如何做到這一點一個完整的例子:
1.我們通過創建一個<div>
元素,將舉行我們的漂亮日誌,並將由服務器處理啓動,併爲新的日誌文本框:
<asp:TextBox ID="txb1" runat="server"></asp:TextBox>
<div id="Text_Div1" runat="server"></div>
2.現在後面的代碼中,我們創建一個類來保存日誌1個一行:
'create a log class and state that it serializable
<Serializable> _
Public Class MyLogRecord
Public Sub New(_msg As String)
[Date] = DateTime.Now
Message = _msg
End Sub
Public Property [Date]() As DateTime
Get
Return m_Date
End Get
Set
m_Date = Value
End Set
End Property
Private m_Date As DateTime
Public Property Message() As [String]
Get
Return m_Message
End Get
Set
m_Message = Value
End Set
End Property
Private m_Message As [String]
Public Function ToText() As String
Return [Date].ToShortDateString() & ": " & Convert.ToString(Message)
End Function
End Class
3。無論你更新日誌,無論是其button_click或textbox_keydown,請執行以下操作:
' create a list of logs
Dim MyLogs As List(Of MyLogRecord)
'check if we stored the logs already in the session,
'if yes, retrieve it from the session var,
'if not then create a new one.
If Session("MyLogs") IsNot Nothing Then
MyLogs = DirectCast(Session("MyLogs"), List(Of MyLogRecord))
Else
MyLogs = New List(Of MyLogRecord)()
End If
' create a new log record from the new textbox value
Dim _tempLog As New MyLogRecord(txb1.Text)
'add the new log to the list
MyLogs.Add(_tempLog)
'save it back in a session var:
Session("MyLogs") = MyLogs
4.在你的日誌保存到MySQL數據庫的一部分,你做這種方式:首先轉換列表中的字節數組並將其存儲在在您檢索從MySQL數據庫日誌的地方varbinary(MAX)
列
'create a new binary formatter, include System.Runtime.Serialization.Formatters.Binary;
Dim formatter As New BinaryFormatter()
'create a byte array to store our logs list
Dim _logsBinary As Byte()
'create a memory stream to write the logs list into
Using _logStream As New MemoryStream()
'use the formatter to serialize the list in to an array of bytes
'directly into the memory stream
formatter.Serialize(_logStream, MyLogs)
'dump the memory stream into the byte array
_logsBinary = _logStream.ToArray()
End Using
' ... save the _logsBinary into mysql as a 'varbinary(max)' ...
5,你的字節數組反序列化回日誌列表:
Dim MyLogs As New List(Of MyLogRecord)()
Dim formatter As New BinaryFormatter()
Using _logStream As New MemoryStream()
_logStream.Write(_logsBinary, 0, _logsBinary.Length)
_logStream.Position = 0
' de-serialize the byte array back into a logs list
MyLogs = DirectCast(formatter.Deserialize(_logStream), List(Of MyLogRecord))
End Using
在你寫在你的網頁日誌的地方6,你做這種方式:
Dim SB As New StringBuilder()
' create a temp date to compare against all the records,
' and initialize it with the first value or else you will have
' a orizontal line before the first row
Dim _prevDate As DateTime = MyLogs.First().[Date]
For Each _logRec As MyLogRecord In MyLogs
'take the date of the currently iterrated item and
'compare against the temp date, note that comparing months is not enough,
'month might be same/earlier but year can be higher
Dim _currentDate As DateTime = _logRec.[Date]
If _currentDate.Month > _prevDate.Month OrElse _currentDate.Year > _prevDate.Year Then
'append horizontal line
SB.AppendLine("<hr/>")
'update temp value
_prevDate = _currentDate
End If
'finally append the log: ToText() is the class custom
'function that we created above
SB.AppendLine(_logRec.ToText())
Next
'dump the logs into the server managed div:
Text_Div1.InnerHtml = SB.ToString()
你想這樣做,在客戶端或服務器端,並推到分貝? – 2014-12-05 07:24:06
@SivaGopal Request.Form應該放棄它的服務器操作... – Banana 2014-12-05 07:25:43
@Banana我想知道的是,因爲他使用Asp.net,他可以使用這些文本框/ textarea ID本身(例如:txb1。文本)。爲什麼他要去那個Request.Form的困難路線?他有沒有客戶端的HTML元素? – 2014-12-05 07:28:08