我将一个字段从mysql调用到一个只读textarea,我创建了另一个文本框,允许用户将字段添加到textarea中。如何将文本框中的值组合到textarea?
我想做的一个例子是:
textarea的
15/12:今天没什么特别的
16/12:另一天
17/12:等等
文本框
这是一个新的输入
结果
15/12:今天没什么特别的
16/12:另一天
17/12:等等
18/12:这是一个新的输入
textarea为"log1"
,文本框为"txb1"
。我目前正在使用
log = trim(request.form("log1"))
我该怎么做
log = trim(request.form("log1")) <br> date ": " trim(request.form("txb1"))
答案 0 :(得分:1)
假设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()
<强>更新强>
如果要将整个日志存储在一个记录而不是单独的表中,最好将其保存为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数据库的部分中,您可以这样做:首先将列表转换为字节数组并将其存储在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。在从mysql数据库中检索日志的位置,您将字节数组反序列化回日志列表:
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()