Asp.net 组合两个文本框值

Asp.net 组合两个文本框值,asp.net,vb.net,Asp.net,Vb.net,我将mysql中的一个字段调用为只读文本区域,并创建了另一个文本框,允许用户将字段添加到文本区域。如何将文本框中的值组合到文本区域中 我想做的一个例子是: textarea 15/12:今天没什么特别的 16/12:另一天 17/12:等等 文本框 这是一个新的输入 结果 15/12:今天没什么特别的 16/12:另一天 17/12:等等 18/12:这是一个新的输入 文本区域是“log1”,文本框是“txb1”。我目前正在使用 log = trim(request.form("log1"))

我将mysql中的一个字段调用为只读文本区域,并创建了另一个文本框,允许用户将字段添加到文本区域。如何将文本框中的值组合到文本区域中

我想做的一个例子是:

textarea
15/12:今天没什么特别的
16/12:另一天
17/12:等等

文本框
这是一个新的输入

结果
15/12:今天没什么特别的
16/12:另一天
17/12:等等
18/12:这是一个新的输入

文本区域是
“log1”
,文本框是
“txb1”
。我目前正在使用

log =  trim(request.form("log1"))
我该怎么做

log = trim(request.form("log1")) <br> date ": " trim(request.form("txb1")) 
log=trim(request.form(“log1”))
date:“trim(request.form(“txb1”))
假设
date
是一个字符串变量,则需要执行以下操作:

log = trim(request.form("log1")) & "<br>" & [date] & ": " & trim(request.form("txb1"))
更新:

<asp:TextBox ID="txb1" runat="server"></asp:TextBox>
<div id="Text_Div1" runat="server"></div>
'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
' 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
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
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()
如果希望将整个日志存储在一条记录中而不是单独的表中,最好将其作为日志列表保存到
varbinary(MAX)
列中

下面是一个完整的示例,说明如何执行此操作:

1。我们首先创建一个
元素,该元素将保存漂亮的日志,并由服务器处理,以及一个用于新日志的文本框:

<asp:TextBox ID="txb1" runat="server"></asp:TextBox>
<div id="Text_Div1" runat="server"></div>
'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
' 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
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
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()
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数据库检索日志的地方,将字节数组反序列化回日志列表:

<asp:TextBox ID="txb1" runat="server"></asp:TextBox>
<div id="Text_Div1" runat="server"></div>
'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
' 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
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
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()
6。在页面中写入日志的地方,您可以这样做:

<asp:TextBox ID="txb1" runat="server"></asp:TextBox>
<div id="Text_Div1" runat="server"></div>
'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
' 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
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
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()
Dim SB作为新的StringBuilder()
'创建临时日期以与所有记录进行比较,
'并用第一个值初始化它,否则
第一排前面的水平线
Dim _prevdateas DateTime=MyLogs.First()[日期]
对于每个_logRec作为MyLogs中的MyLogRecord
'获取当前安排项目的日期,然后
'与临时日期比较,注意比较月份是不够的,
“月份可能相同/更早,但年份可能更高
Dim _currentDateas DateTime=_logRec.[Date]
如果_currentDate.Month>_prevDate.Month或lse _currentDate.Year>_prevDate.Year,则
'附加水平线
某人的附录(“
”) '更新临时值 _prevDate=\u currentDate 如果结束 '最后附加日志:ToText()是自定义类 '我们在上面创建的函数 SB.AppendLine(_logRec.ToText()) 下一个 '将日志转储到服务器管理的div中: Text_Div1.InnerHtml=SB.ToString()
谢谢您的回复,有没有办法让
功能像enter一样?因为现在它只写
environment.newline,而字符串生成器应该负责这一点。对于字符串生成器,您需要包含
System.Text
非常有用!谢谢还有一个问题,有没有办法添加水平行?如果您计划在页面上以html部分的形式显示数据,只需添加一个“
”。我想你不能用asp:TextArea来做,但是你可以用asp:Literal,或者用runat=server来放一个div,我实际上想按月份分开日志。意思是每个月开始前的一行。如果不可能,我可以手动操作。您想在客户端或服务器端执行此操作,然后按db?@SivaGopal
请求。表单
应该放弃服务器端操作…@Banana我想知道的是,由于他使用的是Asp.net,他可以使用那些文本框/textarea ID本身(例如:txb1.Text)。为什么他要走那种艰难的请求路线?他有任何客户端html元素吗?@SivaGopal你是对的,如果这些是id,那么他可以。也许这正是他的船漂浮的原因,也许是从另一种语言改编的?