开发者

Dynamic tables with hyperlinks

开发者 https://www.devze.com 2023-03-12 14:13 出处:网络
I want to build a table based on data in a recordset with a hyperlink that calls a method which takes the message id from the link and redirects to another page to view the message. I\'ve tried HTMLAn

I want to build a table based on data in a recordset with a hyperlink that calls a method which takes the message id from the link and redirects to another page to view the message. I've tried HTMLAnchors, LinkButtons but none seem to work.

    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 TableCe开发者_开发问答ll
        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


You have two choices, as I see it.

  1. Massage the data set so it contains the complete URI, with the ID, and then bind to a grid
  2. Use a routine to create the URI and then bind the grid via row binding
  3. Continuing hand binding the table, using hyperlink controls

All are easy enough to do, but the #3 option is the least "ASP.NET" like. I am not fond of hand building tables, when I can do the same with a control, simply by either a) altering the data before bind or b) stopping on the row binding event and adding information.


Use query string to store the id value of the selected link and redirect to the page you want.


try something like this:

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

tblCell3.controls.add(a)
0

精彩评论

暂无评论...
验证码 换一张
取 消