Monday, 18 July 2011

ASP.NET Forums Website Part 4 Adding the Post Page


This is part four of the ASP.NET Forums Website tutorial. In this tutorial we will create the Post.aspx which will display all of the comments for a given post.

Adding the Post.aspx Page
The next thing that we need to do is add our Post page that will display all of the comments from the Comments table for a given PostId. To begin:

  1. Right click the project in your Solution Explorer.
  2. Select Add New Item…
  3. Select a Web Form and name it Post.aspx.
  4. Ensure that the Select master page checkbox is checked and click Add.
  5. Select the MasterPage.master we added earlier and click OK.

Similar to the other pages, we will be using a repeater to display all of the comments given a PostId. To add the repeater, open Post.aspx up to Design mode and:

  1. Expand the Data tab in your toolbox.
  2. Drag and drop a Repeater into the ContentPlaceHolder1 on the Web Form.
  3. Expand the Repeater Tasks Menu.
  4. In the Choose Data Source DropDownList, select <New Data Source…>.
  5. In the Data Source Configuration Wizard, select a Sql Database and click OK.
  6. It will now prompt you to choose your data connection, in the DropDownList select the ForumDBConnectionString we added earlier.
  7. Next, we need to configure the Select Statement, to do this select the Comments table from the Name DropDownList.
  8. Click WHERE… to add a Where clause.
  9. Set the Column to PostId.
  10. Set the Operator to =.
  11. Set the Source to QueryString.
  12. Set the QueryString field to id.
  13. Click Add.
  14. Click OK.
  15. Click ORDER BY…
  16. Set the Sort by DropDownList to CommentDateTime and choose Descending.
  17. Click OK.
  18. Click Next.
  19. Click Finish.

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Next, we need to format this repeater to output all of the comments given a PostId into a neat table. To do this, open up the Post.aspx to Source mode and:

  1. In the Repeater tags, add the following code for the header:
    <HeaderTemplate>
        <table border="1" width="800px" cellpadding="5px">
    </HeaderTemplate>
  2. Under the Header Template tag, add in the following code for the items:
    <ItemTemplate>
        <tr>
            <td align="right">
                By: <%# Eval("CommentUserName"%> @ <%# Eval("CommentDateTime"%>
            </td>
        </tr>
        <tr>
            <td>
                <%# Eval("CommentMessage"%>
            </td>
        </tr>
    </ItemTemplate>
  3. Under the ItemTemplate tag, add in the following code for the footer:
    <FooterTemplate>
        <tr>
            <td>
                <a href="NewComment.aspx?id=<%= Request.QueryString["id"] %>">Comment</a>
            </td>
        </tr>
        </table>
    </FooterTemplate>
What this has done is created a table, and populated it with two rows for each comment, first a row to show the user and date of the comment and then to show the actual message. Then, we have added a footer with a link to the New Comment page that we will add later and pass the PostId as a query string to that page.

Yes, it is possible to find a good
web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

At this point, we need to test this out. However, we don’t have any comments in our database so we will need to add a few manually. To do this:

1.       Open up the Server or Database Explorer.

2.       Right click the Comments table and select Show Table Data.

3.       Add in the following entries:

CommentId PostId CommentUserName CommentMessage CommentDateTime
1 1 admin Hello I'm admin 1/1/2001 12:00:00 AM
2 1 joe Hello I'm joe 1/1/2001 12:00:00 AM

Now that we have come content, let’s test this out. Go ahead and load up the home page and navigate to General Discussion -> Introductions. You should see something like this:


That is pretty much it for the Past page, next we need to add some functionality to our New Post and Comment links.

If you're ever in the market for some great
Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The Post.aspx source looks like this:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
        <HeaderTemplate>
            <table border="1" width="800px" cellpadding="5px">
        </HeaderTemplate>

        <ItemTemplate>
            <tr>
                <td align="right">
                    By: <%# Eval("CommentUserName"%> @ <%# Eval("CommentDateTime"%>
                </td>
            </tr>
            <tr>
                <td>
                    <%# Eval("CommentMessage"%>
                </td>
            </tr>
        </ItemTemplate>

        <FooterTemplate>
            <tr>
                <td>
                    <a href="NewComment.aspx?id=<%= Request.QueryString["id"] %>">Comment</a>
                </td>
            </tr>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ForumDBConnectionString %>" 
        
        SelectCommand="SELECT * FROM [Comments] WHERE ([PostId] = @PostId) ORDER BY [CommentDateTime] DESC">
        <SelectParameters>
            <asp:QueryStringParameter Name="PostId" QueryStringField="id" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
</asp:Content>

No comments:

Post a Comment