View Single Post
  #1 (permalink)  
Old September 12th, 2003, 06:49 PM
drfunkie drfunkie is offline
Authorized User
 
Join Date: Jun 2003
Location: New orleans, Louisiana, USA.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Forum DataGrid help

I'm trying to get the reply author's info from codebehind I can already get the topic author's info. Example in red and green for clarity.


Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Imports Wrox.WebModules.Accounts.Business

Namespace WebModules.Forums.Web

  Public Class Topic
    Inherits Wrox.ThePhile.Web.PhilePage


    ' Private Variable
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Private myCanModerateForums As Boolean



    ' Page Controls
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Protected Header As Web.Controls.User.Header

    Protected RepliesGrid As DataGrid
    Protected PageNumber As Label
    Protected TotalPages As Label
    Protected FirstPage As LinkButton
    Protected PreviousPage As LinkButton
    Protected NextPage As LinkButton
    Protected LastPage As LinkButton
    Protected DeleteTopic As LinkButton
    Protected EditTopic As HyperLink
    Protected TopicTable As HtmlTable
    Protected TopicAuthor As Label
    Protected TopicSubject As Label
    Protected TopicMessage As Label
    Protected TopicAuthorSignature As Label
    Protected TopicAuthorAvatar As WebControls.Image
    Protected TopicAuthorHomepage As HyperLink
    Protected TopicDate As Label
    Protected TopicTime As Label
    Protected GridItemStyle As Label
    Protected GridAlternatingItemStyle As Label
    Protected NewTopic As HyperLink
    Protected NewReply As HyperLink




    ' Page Events
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
      MyBase.OnInit(e)
      InitializeComponent()
    End Sub


    ' This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
      AddHandler Me.Load, AddressOf Page_Load
    End Sub


    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

      ' Check if the user has the permission to moderate the forums
      myCanModerateForums = Context.User.Identity.IsAuthenticated _
        AndAlso CType(Context.User, SitePrincipal).HasPermission( _
          CInt(ForumsPermissions.ModerateForums))

      Dim topicId As String = "-1"

      ' Get the ForumId from the QueryString
      If Not (Request.QueryString("TopicId") Is Nothing) Then
        topicId = Request.Params("TopicId")
      End If

      ' Show or hide the Edit and Delete topic links, according
      ' to the permissions of the current user
      EditTopic.NavigateUrl = "PostMessage.aspx?Action=EditTopic&TopicId=" _
        & topicId.ToString()
      EditTopic.Visible = myCanModerateForums
      DeleteTopic.Visible = myCanModerateForums

      If Not Page.IsPostBack Then

        Dim topic As Business.Topic

        ' If the TopicKey value is not null, find the topic with that
        ' key and redirect to it
        If Not (Request.QueryString("TopicKey") Is Nothing) Then

          topic = New Business.Topic( _
            Request.QueryString("TopicKey").ToString())

          ' If found, redirect to it, otherwise redirect to Default.aspx
          If topic.Id <> -1 Then
            Response.Redirect( _
              "Topic.aspx?TopicId=" & topic.Id.ToString(), True)
          Else
            Response.Redirect("Default.aspx", True)
          End If

          ' Otherwise it means that we have a TopicId to use directly
        Else


          ' Set the links for posting a new topic/reply
          topic = New Business.Topic(Integer.Parse(topicId))
          NewTopic.NavigateUrl &= topic.Forum.Id.ToString()
          NewReply.NavigateUrl &= topic.Id.ToString()

          ' Set the navigation bar TopicId
          Header.TopicId = Integer.Parse(topicId)

          ' If there is the paramter idicating to show the last page, set
          ' the(number) in the label. This will be read in the BindGrid
          ' procedure

          If Not (Request.QueryString("Display") Is Nothing) _
            AndAlso Request.QueryString("Display").ToString().ToLower( ) _
              = "lastpage" Then

            Dim pageSize As Integer = _
              Configuration.ModuleConfig.GetSettings().RepliesPe rPage
            Dim totalPages As Integer = CInt(Math.Ceiling(( _
              System.Convert.ToDouble(topic.Replies) / pageSize)))
            PageNumber.Text = totalPages.ToString()

          End If

          ' Bind the data to the grid
          RepliesGrid.Attributes("TopicId") = topicId
          BindGrid()

        End If

     'I can get the topic's author's info like this

                    Dim author As Business.Member = topic.Member
                    Dim authemail As String

                    authemail = CType(author.Email, String)

      End If

    ‘ how do I get the replies author’s info. Note: Its in a datagrid unlike the topic authors. Tried this: but not working. its return 0 as member id

     Dim reply As New Business.Reply(topicId)

                Dim reauthor As Business.Member = reply.Member

                Dim recurruser As String

                recurruser = CType(reply.MemberId, String)

    End Sub



    ' Control Events
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Protected Sub RepliesGrid_Delete( _
      ByVal sender As Object, _
      ByVal e As DataGridCommandEventArgs)

      ' If the user cannot moderate forums redirect to the login page
      If Not myCanModerateForums Then
        Response.Redirect( _
          "/ThePhileVB/WebModules/Accounts/Login.aspx?ShowError=true", True)
      End If

      ' Delete this reply
      Dim reply As New Business.Reply( _
        CInt(RepliesGrid.DataKeys(e.Item.ItemIndex)))
      reply.Delete()

      BindGrid()

    End Sub


    Protected Sub DeleteTopic_Click( _
      ByVal sender As Object, _
        ByVal e As EventArgs)

      ' If the user cannot moderate forums redirect to the login page
      If Not myCanModerateForums Then
        Response.Redirect( _
          "/ThePhileVB/WebModules/Accounts/Login.aspx?ShowError=true", True)
      End If

      Dim topicId As Integer = Integer.Parse(Request.QueryString("TopicId"))
      Dim topic As New Business.Topic(topicId)
      Dim forumId As Integer = topic.ForumId

      topic.Delete()
      Response.Redirect(("Forum.aspx?ForumId=" + forumId.ToString()))

    End Sub


    Protected Sub RepliesGrid_PageChanged( _
      ByVal sender As Object, _
      ByVal e As CommandEventArgs)

      If PageNumber.Text.Trim().Length = 0 Then
        Return
      End If

      Select Case e.CommandName
        Case "FirstPage"
          PageNumber.Text = "1"
        Case "PreviousPage"
          PageNumber.Text = (Integer.Parse(PageNumber.Text) - 1).ToString()
        Case "NextPage"
          PageNumber.Text = (Integer.Parse(PageNumber.Text) + 1).ToString()
        Case "LastPage"
          PageNumber.Text = TotalPages.Text
      End Select

      ' Show the new page
      BindGrid()

    End Sub



    ' Methods
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Public Function GetAuthorText( _
      ByVal memberName As Object, _
      ByVal eMail As Object, _
      ByVal showEmail As Object) _
      As String

      ' Return the member Name only or a link to its e-mail address,
      ' according to the ShowEmail value
      If Not Convert.ToBoolean(showEmail) Then
        Return memberName.ToString()
      Else
        Return String.Format("<a href=""mailto:{0}"">{1}</a>", _
          eMail.ToString(), memberName.ToString())
      End If

    End Function


    Public Function ProcessTags(ByVal rawText As Object) As String
      Return Business.Helper.ProcessSpecialTags(rawText.ToStrin g())
    End Function



    ' Properties
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Public ReadOnly Property CanModerateForums() As Boolean
      Get
        Return myCanModerateForums
      End Get
    End Property



    ' Private Code
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Protected Sub BindGrid()

      ' Get the ForumId value from the Grid's attributes.
      ' if null, redirect to the Default.aspx page
      If RepliesGrid.Attributes("TopicId") Is Nothing Then
        Response.Redirect("Default.aspx", True)
      End If

      ' Get the current forum's Id
      Dim topicId As Integer = _
        Integer.Parse(RepliesGrid.Attributes("TopicId"))

      ' Get the number of topics per page
      Dim pageSize As Integer = _
        Configuration.ModuleConfig.GetSettings().RepliesPe rPage

      ' Get the current replies page
      Dim pageNum As Integer = _
        IIf(PageNumber.Text.Trim().Length = 0, _
          1, Integer.Parse(PageNumber.Text))

      ' Retrieve and bind the records to the grid
      Dim topic As New Business.Topic(topicId)
      Dim replies As DataSet = topic.GetReplies(pageNum)
      RepliesGrid.DataSource = replies.Tables(0).DefaultView
      RepliesGrid.DataBind()

      ' Show the total number of pages
      Dim numPages As Integer = CInt(Math.Ceiling( _
        (System.Convert.ToDouble(topic.Replies) / pageSize)))


      If numPages = 0 Then
        numPages = 1
      End If
      TotalPages.Text = numPages.ToString()

      ' Enable/disable the links to navigate through the pages
      FirstPage.Enabled = pageNum <> 1
      PreviousPage.Enabled = pageNum <> 1
      NextPage.Enabled = pageNum <> numPages
      LastPage.Enabled = pageNum <> numPages

      ' Show also the topic, if this is the first page
      If pageNum = 1 Then

        BindTopicControls(topicId)
        TopicTable.Visible = True
        ' If this is the first page, also swap the styles for the Item
        ' and AlternatingItem of the DataGrid, because the Topic has
        ' the Item style
        RepliesGrid.ItemStyle.CssClass = GridAlternatingItemStyle.Text
        RepliesGrid.AlternatingItemStyle.CssClass = GridItemStyle.Text

      Else

        TopicTable.Visible = False
        RepliesGrid.ItemStyle.CssClass = GridItemStyle.Text
        RepliesGrid.AlternatingItemStyle.CssClass = _
          GridAlternatingItemStyle.Text

      End If

    End Sub


    Private Sub BindTopicControls(ByVal topicId As Integer)

      ' Retrieve all the topic's info
      Dim topic As New Business.Topic(topicId)
      Dim author As Business.Member = topic.Member

      ' Show the data
      TopicAuthor.Text = GetAuthorText( _
        author.Name, _
        author.Email, _
        author.ShowEmail)
      TopicAuthorAvatar.ImageUrl = author.AvatarUrl
      TopicAuthorAvatar.Visible = author.AvatarUrl.Length > 0
      TopicAuthorHomepage.NavigateUrl = author.Homepage
      TopicAuthorHomepage.Visible = author.Homepage.Length > 0
      TopicDate.Text = String.Format("{0:MM/dd/yy}", topic.AddedDate)
      TopicTime.Text = String.Format("{0:HH:mm:ss tt}", topic.AddedDate)
      TopicSubject.Text = topic.Subject
      TopicMessage.Text = Business.Helper.ProcessSpecialTags(topic.Message)
      TopicAuthorSignature.Text = _
        Business.Helper.ProcessSpecialTags(author.Signatur e)

    End Sub


  End Class

End Namespace

Note: Each topic can have many replies, so I would like to get the replies author's info and hold it in a string or something.

This will allow me to allow the logged on member to edit his/her topics and replies.

HELLLLP.... :)