带有超链接的动态表

时间:2011-06-10 15:35:21

标签: asp.net

我想基于记录集中的数据构建一个表,其中一个超链接调用一个方法,该方法从链接获取消息ID并重定向到另一个页面以查看消息。我尝试过HTMLAnchors,LinkBut​​tons,但似乎都没有。

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim iUserID As Integer
    Dim sMsgId, sMsgSubject, sMsgDateSent, sMsgRead, sMsgFrom, sFirstname As String
    iUserID = Session("UserID")
    Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString)
    Dim cmd As New SqlCommand()
    Dim tblRow As TableRow
    Dim tblCell1 As TableCell
    Dim tblCell2 As TableCell
    Dim tblCell3 As TableCell
    Dim tblCell4 As TableCell

    tblRow = New TableRow

    tblCell1 = New TableCell
    tblCell1.Text = ""
    tblRow.Cells.Add(tblCell1)

    tblCell2 = New TableCell
    tblCell2.Text = "From"
    tblRow.Cells.Add(tblCell2)

    tblCell3 = New TableCell
    tblCell3.Text = "Subject"
    tblRow.Cells.Add(tblCell3)

    tblCell4 = New TableCell
    tblCell4.Text = "Received"
    tblRow.Cells.Add(tblCell4)

    tblMessages.Rows.Add(tblRow)

    tblCell1 = Nothing
    tblCell2 = Nothing
    tblCell3 = Nothing
    tblRow = Nothing

    cmd.CommandText = "SELECT msg.msg_id, msg.msg_usr_from, usr.usr_firstname, msg.msg_subject, msg.msg_date_sent, msg.msg_read FROM messages msg inner join users usr on msg.msg_usr_to = usr.usr_id WHERE msg.msg_usr_to='" & iUserID & "'"
    cmd.Connection = cn
    cn.Open()
    Dim r As SqlDataReader = cmd.ExecuteReader
    Dim objControl As Button


    While r.Read()
        tblCell1 = New TableCell
        tblCell2 = New TableCell
        tblCell3 = New TableCell
        tblCell4 = New TableCell
        tblRow = New TableRow
        sMsgId = CStr(r("msg_id"))
        sMsgFrom = CStr(r("msg_usr_from"))
        sFirstname = CStr(r("usr_firstname"))
        sMsgSubject = CStr(r("msg_subject"))
        sMsgDateSent = CStr(r("msg_date_sent"))
        sMsgRead = CStr(r("msg_read"))

        If sMsgRead = "N" Then
            tblCell1.Text = "<img width=30px src=images/new_email.png>"
        Else
            tblCell1.Text = "<img width=30px src=images/email_open.png>"
        End If

        Dim objLinkbutton As 

        tblRow.Cells.Add(tblCell1)
        tblCell2.Text = sFirstname
        tblRow.Cells.Add(tblCell2)
        tblCell3.controls.add(       HYPERLINK HERE!!!
        tblRow.Cells.Add(tblCell3)
        tblCell4.Text = FormatDateTime(sMsgDateSent, DateFormat.LongDate)
        tblRow.Cells.Add(tblCell4)
        If sMsgRead = "N" Then
            tblCell2.Font.Bold = True
            tblCell3.Font.Bold = True
            tblCell4.Font.Bold = True
        End If
        tblMessages.Rows.Add(tblRow)
        tblCell1 = Nothing
        tblCell2 = Nothing
        tblCell3 = Nothing
        tblCell4 = Nothing
        tblRow = Nothing
    End While

    r.Close()
    cn.Close()
    cn = Nothing
    r = Nothing
    cmd = Nothing
End Sub

Protected Sub viewmessage(messageid As String)
    Context.Items("messageid") = "messageid"
    Server.Transfer("viewmessage.aspx")
End Sub
End Class

3 个答案:

答案 0 :(得分:1)

我看到你有两个选择。

  1. 按摩数据集,使其包含完整的URI和ID,然后绑定到网格
  2. 使用例程创建URI,然后通过行绑定绑定网格
  3. 使用超链接控件
  4. 继续手动绑定表格

    所有这些都很容易,但#3选项最少“ASP.NET”。我不喜欢手工构建表,当我可以对控件执行相同操作时,只需a)在绑定之前更改数据或b)停止行绑定事件并添加信息。

答案 1 :(得分:0)

尝试这样的事情:

Dim a As New HyperLink()
a.Text = "your text"
a.NavigateUrl = "yourPage.aspx?idMsg=" + r["msg_id"]

tblCell3.controls.add(a)

答案 2 :(得分:0)

使用查询字符串存储所选链接的ID值并重定向到您想要的页面。