Creating a Simple Blog with RadGrid

2 posts, 1 answers
  1. Answer
    Terfa
    Terfa avatar
    11 posts
    Member since:
    Jan 2009

    Posted 04 Dec 2009 Link to this post

    Requirements

    RadControls version

    RadControls for ASPNET AJAX Q3
    .NET version

    Visual Basic.NET
    Visual Studio version

    Visual Studio 2008
    programming language

    Visual Basic.NET, SQL Server 2005
    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    If you want to save yourself some time you could use WordPress to install a blog on your website, but for other web developers who are like me, who want to create stuff for themselves, I'd say this little program could help you create a Simple Blog with RADGrid Control. As you might have already known Blogs or blogging is a way of life of the internet and has completely shaped our understanding and use of the internet in our modern world. Blogs are simple components integrated into websites, some are websites in their own right, usually maintained by an individual with regular entries of commentary, descriptions of events, or other material such as graphics or video.

    Entries are commonly displayed in reverse-chronological order. "Blog" can also be used as a verb, meaning to maintain or add content to a blog. Many blogs provide commentary or news on a particular subject; others function as more personal online diaries. A typical blog combines text, images, and links to other blogs, as well as web pages and other media related to its topic.

    The ability for readers to leave comments in an interactive format is an important part of many blogs. Most blogs are primarily textual, although some focus on art and design, photography , videos, music, and audio also know as podcasting.

    Many Blogs require users to sign-up, and in this project I will include a simple sign-up module, which serves to identify individual users in the blog. The RadGrid is plugged into SQL Server 2005 to retrieve topics and threads. I would not go into security implementation, that part is left to the developer who should structure their own web security anyway that suits them. If you do not know how to set security features please refer to Chapter 8: ActiveSkill - Getting Started in the Ajax Courseware found here.



    How to:

    Start up and create a new website in Visual Studio 2008
    Drop a RadScriptManager on the page
    Drop a RadGrid Control on the page

    User Credentials
    Create a Sign-up module, either in another page or on the same page to collect user's info needed to identify them in the blog, such a module would have the following fields enumerated below... these would be saved in a login table on your database
    UserID
    First Name
    Last Name
    Username
    Password
    EmailAddress          -           A field length that captures the users email address
    Picture_Path           -           A field length that captures the path to users pictures saved on your webserver

    Blog Topics
    Next create a table and call it BlogTopics, every new thread created by a user would be saved in this table... fields should include

    BlogID                                       int                                IDENTITY(1,1) NOT NULL
    BlogTopic                                 varchar(500)               
    BlogMessage                            varchar(max)              
    UserID                                       int                                 NOT NULL
    BlogTopicCreatedDate             varchar(15)                  
    BlogTopicCreatedTime             varchar(15)
    CONSTRAINT                           [PK_BlogTopics] PRIMARY KEY CLUSTERED

    Blog Threads
    Finally create a table, you could call it BlogThreads, this one saves all comments made by other users to the topic presented...

    BlogCommentID                         int                                     IDENTITY(1,1) NOT NULL,
    BlogID                                        int                                     NOT NULL,
    BlogComment                            varchar(max)                   
    UserID                                       int                                      NOT NULL,
    BlogCommentDate                    varchar(15)                     
    BlogCommentTime                    varchar(15)       

    The Code in the SignUp.aspx file

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="SignUp.aspx.vb" Inherits="SignUp" %> 
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml"
    <head runat="server"
        <title>Blog: Using RadGrid: SignUp</title> 
         <style type="text/css"
          .thislink:hover 
                { 
                    color:#DDD; 
                    text-decoration:underline; 
                     
                    } 
                    .LinkStyle 
                {color:Navy; font-size:small; font-weight:bold;} 
                 
                .LinkStyle:hover 
                { 
                    text-decoration:underline; 
                    color:Blue; 
                     
                    } 
        </style> 
    </head> 
    <body> 
        <form id="frmSignUp" runat="server"
        <div style="font-family: Tahoma"
        <div id="SubscribeMessage"><br /> 
                 
                <h2><span class="style53">Joining</span> <span class="style54">the discussion!</span></h2
                 
                <p>Your views, comments, ideas, questions are important to us, to contribute however 
                    we need you to sign up in one easy step, please read our <class="thislink" style="font-size:small; color:Green; font-weight:bold;" href="#">conditions of service</a> 
                    then complete the form below!</p><br /> 
                 
                <div id="AddUsers" runat="server" style="margin-left:150px;"
                <h6 id="UserHeader" runat="server">Your Login Details...</h6> 
                <div id="usererror" runat="server" style="font-weight:bold; color:Red; font-size:smaller;"></div><br /> 
                <table> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >First Name</label></td
                <td><em>*</em></td
                <td><input id="txtUserFirstName" type="text" size="25" runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >Last Name</label></td
                <td><em>*</em></td
                <td><input id="txtUserLastName" type="text" size="25"  runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >User Name</label></td
                <td><em>*</em></td
                <td><input id="txtUserName" type="text" size="25" runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >Password</label></td
                <td><em>*</em></td
                <td><input id="txtPassword" type="password" size="25" runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >Confirm Password</label></td
                <td><em>*</em></td
                <td><input id="txtRePassword" type="password" size="25" runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >Email Address</label></td
                <td><em>*</em></td
                <td><input id="txtEmailAddr" type="text" size="25" runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >Security Question</label></td
                <td><em>*</em></td
                <td><input id="txtSecurityQuestion" type="text" size="25" runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td style="text-align:right;"><label style="font-size:small; color:Navy; font-weight:bold;  text-align:right;" >Answer</label></td
                <td><em>*</em></td
                <td><input id="txtSecurityAnswer" type="text" size="25" runat="server" style="font-size:small;" /></td
                </tr> 
                <tr> 
                <td></td
                <td></td
                <td style="font-size:small; color:Navy; text-align:left; font-weight:bold;" ><input type="checkbox" id="blogterms" runat="server" style="width:15px; height:15px; " /> I agree to the <class="thislink" style="font-size:small; color:Green; font-weight:bold;" href="#" >terms and conditions</a></td
                </tr> 
                <tr> 
                <td></td
                <td></td
                <td><button id="btnCreateUser" runat="server" onserverclick ="SaveNewUser">Sign up</button></td
                </tr> 
                </table> 
             
                  </div>   
                     
             </div> 
        </div> 
        </form> 
    </body> 
    </html> 

           
    Code in the SignUp.aspx.vb file    

    Imports System.Data 
    Imports System.Data.SqlClient 
    Imports System.Data.Sql 
     
    Partial Class SignUp 
        Inherits System.Web.UI.Page 
     
        Function IsValidEmail(ByVal strIn As StringAs Boolean 
            ' Return true if strIn is in valid e-mail format. 
            Return Regex.IsMatch(strIn, "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
        End Function 
     
        Public Function IfRecordExist(ByVal ChkRecordStr As StringAs Boolean 
     
            Try 
     
                Using GlobalConnectionString As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     
                    Dim CheckRecordComm As New SqlClient.SqlCommand(ChkRecordStr, GlobalConnectionString) 
     
                    CheckRecordComm.Parameters.AddWithValue("UserName", txtUserName.Value.Trim()) 
                    CheckRecordComm.Parameters.AddWithValue("UserPassword", txtPassword.Value.Trim()) 
     
                    CheckRecordComm.Connection.Open() 
     
                    Dim ChkRecordRdr As SqlDataReader = CheckRecordComm.ExecuteReader() 
     
                    ChkRecordRdr.Read() 
     
                    If ChkRecordRdr.HasRows Then 
     
                        IfRecordExist = True 
     
                    Else 
     
                        IfRecordExist = False 
     
                    End If 
     
     
                End Using 
     
            Catch AnyException As Exception 
     
                IfRecordExist = False 
     
            End Try 
     
        End Function 
     
        Public Sub SaveNewUser() 
     
            usererror.InnerHtml = "" 
     
     
            If txtUserFirstName.Value = "" Or txtUserLastName.Value = "" Or txtUserName.Value = "" Or txtPassword.Value = "" Or txtRePassword.Value = "" Or txtSecurityQuestion.Value = "" Or txtSecurityAnswer.Value = "" Or txtEmailAddr.Value = "" Then 
     
                usererror.Visible = True 
                usererror.InnerHtml = "Error: No field should be left empty..." 
                Exit Sub 
     
            End If 
     
            If txtPassword.Value <> txtRePassword.Value Then 
     
                usererror.Visible = True 
                usererror.InnerHtml = "Error: Passwords do not match...!" 
                Exit Sub 
     
            End If 
     
            If IsValidEmail(txtEmailAddr.Value) = False Then 
     
                usererror.InnerHtml = "Error: User's email address is wrongly typed..." 
                Exit Sub 
     
            End If 
     
     
            If blogterms.Checked = False Then 
     
                usererror.InnerHtml = "Error: you must agree to our terms and conditions..." 
                Exit Sub 
     
            End If 
     
     
            Try 
     
                Using GlobalConnectionString As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     
                    Dim LoginTimes As Integer = 0 
     
                    Dim StaffLoginCredentialsStr As String = "SELECT Username, UserEmailAddr FROM Login WHERE UserName = @UserName OR UserPassword = @UserPassword" 
                    Dim InsertStaffLoginCredentialsStr As String = "INSERT INTO Login (UserFirstName, UserLastName, Username, UserPassword, UserSecurityQuestion, UserAnswer, UserEmailAddr, UserLoginDate, UserLevel, UserLoginNumber) VALUES (@UserFirstName, @UserLastName, @Username, @UserPassword, @UserSecurityQuestion, @UserAnswer, @UserEmailAddr, @UserLoginDate, @UserLevel, @UserLoginNumber)" 
     
                    Dim SaveRecordsComm As New SqlClient.SqlCommand(InsertStaffLoginCredentialsStr, GlobalConnectionString) 
     
                    With SaveRecordsComm.Parameters 
     
                        .AddWithValue("UserFirstName", txtUserFirstName.Value.Trim()) 
                        .AddWithValue("UserLastName", txtUserLastName.Value.Trim()) 
                        .AddWithValue("UserName", txtUserName.Value.Trim.ToLower()) 
                        .AddWithValue("UserPassword", txtPassword.Value.Trim.ToLower()) 
                        .AddWithValue("UserSecurityQuestion", txtSecurityQuestion.Value.Trim()) 
                        .AddWithValue("UserAnswer", txtSecurityAnswer.Value.Trim()) 
                        .AddWithValue("UserLevel""Blog"
                        .AddWithValue("UserLoginDate", Format(DateTime.Now.Date"Short Date")) 
                        .AddWithValue("UserEmailAddr", txtEmailAddr.Value.Trim()) 
                        .AddWithValue("UserLoginNumber", LoginTimes) 
     
                        If IfRecordExist(StaffLoginCredentialsStr) = True Then 
                            usererror.Visible = True 
                            usererror.InnerHtml = "User profile already exist, please specify another..." 
                            Exit Sub 
                        Else 
     
                            With SaveRecordsComm 
                                .Connection.Open() 
                                .ExecuteScalar() 
                                .Connection.Close() 
                            End With 
     
                            usererror.InnerHtml = "User Successfully Created..." 
     
                        End If 
     
                    End With 
     
                End Using 
     
            Catch AnyException As Exception 
     
                usererror.Visible = True 
                usererror.InnerHtml = AnyException.ToString() 
                Exit Sub 
            End Try 
     
            Response.Redirect("Default.aspx"
     
        End Sub 
     
    End Class 
     

    Code in the Default.aspx

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml"
    <head runat="server"
        <title>Blog: Using RadGrid</title> 
         
         <style type="text/css"
          .thislink:hover 
                { 
                    color:#DDD; 
                    text-decoration:underline; 
                     
                    } 
                    .LinkStyle 
                {color:Navy; font-size:small; font-weight:bold;} 
                 
                .LinkStyle:hover 
                { 
                    text-decoration:underline; 
                    color:Blue; 
                     
                    } 
        </style> 
    </head> 
    <body> 
        <form id="frmBlog" runat="server"
        <div> 
         
         
         <div id="SubscribeMessage"><br /> 
             <telerik:RadScriptManager ID="RadScriptManager1" Runat="server"
             </telerik:RadScriptManager> 
             <br /> 
                 
                <id="BloggerName" runat="server" style="font-size:small; font-weight:bold; color:Navy;" ></a
                <h2><span class="style53">Welcome to our</span> <span class="style54">Podium!</span></h2
                 
                <p>Your views, comments, ideas, questions are all welcome here, feel free to share your mind with us!</p> 
                     
             </div> 
              
        <div id="viewdirectorysection"
           
                             
                            <asp:SqlDataSource ID="SqlDataBlogTopics" runat="server"  
                                ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
                                SelectCommand="SELECT BlogID, BlogTopic, UserID, BlogMessage, BlogTopicCreatedDate FROM BlogTopics ORDER BY BlogID DESC"
                            </asp:SqlDataSource> 
                 
                <div style="font-size:small; color:Green; border-bottom:dotted 1px #DDD; padding-bottom:5px; padding-top:5px; margin-left:75px; width:670px;"><id="A1" class="thislink" style="color:green; font-size:small; font-weight:bold;" runat="server" onserverclick ="ShowNewTopic" href="#">Start a Topic</a> <style="color:#DDD; font-size:small;"> | </a> <style="font-size:small; color:green; font-weight:bold; ">Join the discussion Today!</a><a style="color:#DDD; font-size:small;"> | </a> <class="thislink" style="color:Green; font-size:small; font-weight:bold;" href="SignUp.aspx">Sign-up Free!</a></div
                 
                <telerik:RadGrid ID="RadGridBlogTopics" runat="server" AllowPaging="True"  
                DataSourceID="SqlDataBlogTopics" GridLines="None" Skin="Web20" Width="670px"  
                style="margin-left:70px;" BorderColor="White" BorderStyle="Solid"  
                BorderWidth="1px"
     
                <HeaderContextMenu EnableTheming="True"
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                </HeaderContextMenu> 
     
                <MasterTableView AutoGenerateColumns="False" BorderColor="White"  
                    DataKeyNames="BlogID, BlogTopic, UserID, BlogMessage, BlogTopicCreatedDate"  
                    DataSourceID="SqlDataBlogTopics" GridLines="None" BorderStyle="None"  
                    BorderWidth="1px"
                <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
     
                <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                    <Columns> 
                        <telerik:GridBoundColumn DataField="BlogID" DataType="System.Int32"  
                            HeaderText="BlogID" ReadOnly="True" SortExpression="BlogID" UniqueName="BlogID"  
                            Visible="False"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="BlogTopic" HeaderText="BlogTopic"  
                            SortExpression="BlogTopic" UniqueName="BlogTopic" Visible="False"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="UserID" DataType="System.Int32"  
                            HeaderText="UserID" SortExpression="UserID" UniqueName="UserID" Visible="False"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="BlogMessage" HeaderText="BlogMessage"  
                        SortExpression="BlogMessage" UniqueName="BlogMessage" Visible="False"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="BlogTopicCreatedDate"  
                            HeaderText="BlogTopicCreatedDate" SortExpression="BlogTopicCreatedDate"  
                            UniqueName="BlogTopicCreatedDate" Visible="False"
                        </telerik:GridBoundColumn> 
                        <telerik:GridTemplateColumn HeaderText="Topics" UniqueName="Topics"
                            <ItemTemplate> 
                                <asp:LinkButton ID="LinkButton1" runat="server" Font-Bold="True"  
                                    Font-Size="Small" Font-Underline="False" ForeColor="#333399"  
                                    Text='<%# Eval("BlogTopic") %>' onclick="LinkButton1_Click"></asp:LinkButton> 
                            </ItemTemplate> 
                            <HeaderStyle Font-Bold="True" Font-Size="Medium" /> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 
                    <HeaderStyle BackColor="Navy" ForeColor="White" /> 
                </MasterTableView> 
     
                <SelectedItemStyle BackColor="White" BorderColor="White" ForeColor="White" /> 
     
                <FilterMenu EnableTheming="True"
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                </FilterMenu> 
                        </telerik:RadGrid> 
           
                 
                <br /> 
                 
                  <div id="StartNewTopic" style="border-top:dotted 1px Gray; font-size:small;width:670px; padding-top:5px; height:25px; margin-top:10px; margin-left:70px;" visible ="false" runat="server"
                        <table> 
                            <tr> 
                            <td><label id="Label8" style="font-size:small; color:#CC0000; font-weight:bold;" runat="server">Subject</label></td
                            </tr> 
                            <tr> 
                            <td><input id="txtTopic" runat="server" size="40" /></td
                            </tr>  
                            <tr> 
                            <td><label id="Label7" style="font-size:small; color:#CC0000; font-weight:bold;" runat="server">Discussion</label></td
                            </tr> 
                            <tr> 
                            <td><textarea id="TextArticle" runat="server" cols="60" rows ="8" /></td
                            </tr>  
                            <tr> 
                            <td><button id="Button1" runat ="server" onserverclick = "PostNewTopic" >Submit</button><button id="Button2" onserverclick ="CancelPost" runat ="server">Cancel</button></td
                            </tr> 
                        </table><br /> 
            </div> 
                 
                <div id="Discussion" style="width:783px;" visible="false" runat="server"
                    <div id="TopicDiscussion"  
                        style="color:#CC0000; font-family:'Tahoma'; font-size:14pt; font-weight:bold; margin-left:70px; margin-top:20px;  width:660px;"  
                        runat="server"></div> 
                    <div id="DiscussionBody" style="margin-left:70px; font-size:medium;width:670px;"
                    <table style="font-family:'Tahoma' "
                        <tr> 
                        <td><label id="Author" style="font-size:small; color:black; font-weight:bold;" runat="server"></label></td
                        <td><label id="Label1" style="font-size:small; font-weight:bold;" runat="server">|</label></td
                        <td><label id="Label2" style="font-size:small; font-weight:bold;" runat="server"</label></td
                        <td><label id="articledate" style="color:black; color:Gray;  font-size:small; font-weight:bold;" runat="server"></label></td
                        <td><label id="articletime" style="color:black; font-size:small; font-weight:bold;" runat="server"></label></td
                        <td><label id="Label3" style="font-size:small; font-weight:bold;" runat="server">|</label></td
                        <td><label id="lblComments" style="font-size:small; font-weight:bold;" runat="server"></label></td
                        </tr> 
                    </table><br /> 
                    </div> 
                    <div id="maindiscussion" style="color:black; font-family:'Tahoma'; border-bottom:dotted 1px Gray;font-size:small;width:670px; margin-left:70px; padding-bottom:10px;" runat="server"
                     
                    </div> 
                    <div id="GridWrap" style="margin-top:10px; width:670px; margin-left:70px;"
                        <telerik:RadGrid ID="RadGridComments" runat="server"  
                            DataSourceID="SqlDataComments" GridLines="None" Skin="Web20"  
                            BorderColor="White"
    <HeaderContextMenu EnableTheming="True" Skin="Office2007"
    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
    </HeaderContextMenu> 
     
    <MasterTableView DataSourceID="SqlDataComments" AutoGenerateColumns="False" BorderColor="White"  
                                BorderWidth="1px" Frame="Void" GridLines="None"
    <RowIndicatorColumn> 
    <HeaderStyle Width="20px"></HeaderStyle> 
    </RowIndicatorColumn> 
     
    <ExpandCollapseColumn> 
    <HeaderStyle Width="20px"></HeaderStyle> 
    </ExpandCollapseColumn> 
        <Columns> 
            <telerik:GridBoundColumn DataField="BlogComment" HeaderText="BlogComment"  
                SortExpression="BlogComment" UniqueName="BlogComment" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="BlogCommentDate"  
                HeaderText="BlogCommentDate" SortExpression="BlogCommentDate"  
                UniqueName="BlogCommentDate" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="BlogCommentTime"  
                HeaderText="BlogCommentTime" SortExpression="BlogCommentTime"  
                UniqueName="BlogCommentTime" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="UserName" HeaderText="UserName"  
                SortExpression="UserName" UniqueName="UserName" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="BlogID" DataType="System.Int32"  
                HeaderText="BlogID" SortExpression="BlogID" UniqueName="BlogID" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="BlogCommentID" DataType="System.Int32"  
                HeaderText="BlogCommentID" ReadOnly="True" SortExpression="BlogCommentID"  
                UniqueName="BlogCommentID" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridTemplateColumn HeaderText="Comments" UniqueName="Comments"
                <ItemTemplate><br /> 
                    <asp:Label ID="lblComments" runat="server" Font-Size="Small"  
                        ForeColor="#666666" Text='<%# Eval("BlogComment") %>'></asp:Label> 
                    <br /> 
                    <br /> 
                    <asp:Label ID="lblUser" runat="server" Font-Bold="True" Font-Size="Small"  
                        ForeColor="#CC0000" Text='<%# Eval("UserName") %>'></asp:Label><a> | </a><asp:Label ID="Label5" runat="server" Font-Bold="True" Font-Size="Small"  
                        ForeColor="Gray" Text='<%# Eval("BlogCommentDate") %>'></asp:Label><a> | </a><asp:Label ID="Label6" runat="server" Font-Bold="True" Font-Size="Small"  
                        ForeColor="Gray" Text='<%# Eval("BlogCommentTime") %>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
        </Columns> 
        <HeaderStyle BackColor="White" Font-Bold="True" Font-Size="Medium"  
            ForeColor="Black" /> 
    </MasterTableView> 
     
    <FilterMenu EnableTheming="True" Skin="Office2007"
    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
    </FilterMenu> 
                        </telerik:RadGrid> 
                        <asp:Label ID="lblSelectedTopic" runat="server" style="color:White; font-size:small;"></asp:Label> 
                        <asp:Label ID="lblUserID" runat="server" style="color:White; font-size:small;"></asp:Label> 
                        <asp:SqlDataSource ID="SqlDataComments" runat="server"  
                            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
                             
                             
                            SelectCommand="SELECT BlogThreads.BlogComment, BlogThreads.BlogCommentDate, BlogThreads.BlogCommentTime, Login.UserName, BlogThreads.BlogID, BlogThreads.BlogCommentID FROM BlogThreads INNER JOIN BlogTopics ON BlogThreads.BlogID = BlogTopics.BlogID INNER JOIN Login ON BlogThreads.UserID = Login.UserID WHERE (BlogTopics.BlogID = @BlogID) ORDER BY BlogThreads.BlogCommentID"
                            <SelectParameters> 
                                <asp:ControlParameter ControlID="lblSelectedTopic" Name="BlogID"  
                                    PropertyName="Text" /> 
                            </SelectParameters> 
                        </asp:SqlDataSource> 
                    </div> 
                    <div id="CommentsMenu" style="margin-left:70px;" runat="server" > 
                       <id="A2" class="LinkStyle" runat="server" onserverclick ="MakeComments" href="#">Add Comments</a> | <id="A3" class="LinkStyle" href="#" runat ="server" onserverclick ="MakeComments" >Reply</a> | <class="LinkStyle" href="#">Print</a>  
                    </div> 
                     
                    <div id="CommentsBody" style="border-top:dotted 1px Gray; font-size:small;width:670px; padding-top:5px; height:25px; margin-top:10px; margin-left:70px;" visible ="false" runat="server"
                        <table> 
                            <tr> 
                            <td><label id="Label4" style="font-size:small; color:#CC0000; font-weight:bold;" runat="server">Type your comments...</label></td
                            </tr> 
                            <tr> 
                            <td><textarea id="txtMessage" runat="server" cols="60" rows ="8" /></td
                            </tr>  
                            <tr> 
                            <td><button id="btnComments" runat ="server" onserverclick = "ApplyComments" >Submit</button><button id="btnCancelComments" onserverclick ="CancelComments" runat ="server">Cancel</button></td
                            </tr> 
                        </table><br /> 
            </div> 
            </div> 
          </div> 
           <asp:label id="blognumber" runat="server" style="color:White; font-size:6pt;"></asp:label> 
        </div> 
         
        </div> 
        </form> 
    </body> 
    </html> 
     


    Code in the Default.aspx.vb file

    Imports System.Data 
    Imports System.Data.SqlClient 
    Imports System.Data.Sql 
     
    Imports Telerik.Web.UI 
     
    Partial Class _Default 
        Inherits System.Web.UI.Page 
     
        Public Shared SelectedTopicID As Integer 
        Public Shared GlobalUserID As Integer 
     
        Function IsValidEmail(ByVal strIn As String) As Boolean 
            ' Return true if strIn is in valid e-mail format. 
            Return Regex.IsMatch(strIn, "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") 
        End Function 
     
        Public Function GetBloggerName(ByVal UserID As Int64) As String 
     
            Try 
     
                Using GlobalConnectionString As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     
                    Dim GetStaffNameStr As String = "SELECT * FROM Login WHERE UserID = @UserID" 
     
                    Dim GetStaffNameComm As New SqlClient.SqlCommand(GetStaffNameStr, GlobalConnectionString) 
     
                    GetStaffNameComm.Parameters.AddWithValue("UserID", UserID) 
     
                    If IfRecordExist(GetStaffNameStr) = True Then 
     
                        GetStaffNameComm.Connection.Open() 
     
                        Dim rdrGetSiteMail As SqlDataReader = GetStaffNameComm.ExecuteReader() 
     
                        rdrGetSiteMail.Read() 
     
                        GetBloggerName = rdrGetSiteMail.Item("UserFirstName") & " " & rdrGetSiteMail.Item("UserLastName") 
     
                        rdrGetSiteMail.Close() 
     
                        GetStaffNameComm.Connection.Close() 
     
                    Else 
     
                        GetBloggerName = "Guest" 
     
                    End If 
     
                End Using 
     
            Catch AnyException As Exception 
     
                GetBloggerName = "Guest" 
     
            End Try 
     
     
        End Function 
     
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     
            RadGridBlogTopics.MasterTableView.Rebind() 
     
            lblUserID.Text = Request.QueryString(".diresu") 
     
            BloggerName.InnerHtml = "Welcome, " & GetBloggerName(Val(lblUserID.Text)) 
     
        End Sub 
     
     
        Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
     
            CType(CType(sender, LinkButton).Parent.Parent, GridItem).Selected = CType(sender, LinkButton).Visible 
            RadGridBlogTopics_SelectedIndexChanged(sender, e) 
     
            Discussion.Visible = True 
     
        End Sub 
     
        Protected Sub RadGridBlogTopics_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGridBlogTopics.SelectedIndexChanged 
     
            SelectedTopicID = (RadGridBlogTopics.SelectedItems(0).OwnerTableView.DataKeyValues(RadGridBlogTopics.SelectedItems(0).ItemIndex)("BlogID")) 
            DisplaySelectedTopic((RadGridBlogTopics.SelectedItems(0).OwnerTableView.DataKeyValues(RadGridBlogTopics.SelectedItems(0).ItemIndex)("BlogID"))) 
            lblSelectedTopic.Text = (RadGridBlogTopics.SelectedItems(0).OwnerTableView.DataKeyValues(RadGridBlogTopics.SelectedItems(0).ItemIndex)("BlogID")) 
            RadGridComments.MasterTableView.Rebind() 
     
        End Sub 
     
        Public Sub DisplaySelectedTopic(ByVal BlogID As Integer) 
     
            Try 
     
                Using GlobalConnection As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     
                    Dim SelectBlogTopicStr As String = "SELECT BlogTopics.BlogTopic, BlogTopics.BlogMessage, Login.UserFirstName + ' ' + UserLastName AS Author, BlogTopics.BlogTopicCreatedDate AS CrtDate FROM BlogTopics INNER JOIN Login ON BlogTopics.UserID = Login.UserID WHERE (BlogTopics.BlogID = @BlogID)" 
                    Dim CountAllComments As String = "SELECT COUNT (BlogComment) AS Comments FROM BlogThreads WHERE BlogID = @BlogID" 
                    Dim SelectBlogTopicComm As New SqlClient.SqlCommand(SelectBlogTopicStr, GlobalConnection) 
     
                    SelectBlogTopicComm.Parameters.AddWithValue("BlogID", BlogID) 
     
                    SelectBlogTopicComm.Connection.Open() 
     
                    Dim rdrSelectBlogTopic As SqlDataReader = SelectBlogTopicComm.ExecuteReader() 
     
                    rdrSelectBlogTopic.Read() 
     
                    TopicDiscussion.InnerHtml = rdrSelectBlogTopic.Item("BlogTopic") 
                    Author.InnerHtml = "By " & rdrSelectBlogTopic.Item("Author") 
                    articledate.InnerHtml = Format(rdrSelectBlogTopic.Item("CrtDate"), "Long Date") 
     
                    maindiscussion.InnerHtml = rdrSelectBlogTopic.Item("BlogMessage") 
     
                    rdrSelectBlogTopic.Close() 
     
     
     
                    If IfRecordExist(CountAllComments) = True Then 
     
                        SelectBlogTopicComm.CommandText = CountAllComments 
     
                        rdrSelectBlogTopic = SelectBlogTopicComm.ExecuteReader() 
     
                        rdrSelectBlogTopic.Read() 
                        lblComments.InnerHtml = "Comments: " & rdrSelectBlogTopic.Item("Comments") 
                        rdrSelectBlogTopic.Close() 
     
                    Else 
     
                        lblComments.InnerHtml = "Comments: 0" 
     
                    End If 
     
                    SelectBlogTopicComm.Connection.Close() 
     
                End Using 
     
     
            Catch ex As Exception 
     
                TopicDiscussion.InnerHtml = ex.ToString() 
     
            End Try 
     
     
     
        End Sub 
     
        Public Function IfRecordExist(ByVal ChkRecordStr As String) As Boolean 
     
            Try 
     
                Using GlobalConnectionString As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     
                    Dim CheckRecordComm As New SqlClient.SqlCommand(ChkRecordStr, GlobalConnectionString) 
     
     
                    CheckRecordComm.Parameters.AddWithValue("BlogID", SelectedTopicID) 
                    CheckRecordComm.Parameters.AddWithValue("UserID", Val(lblUserID.Text)) 
     
                    CheckRecordComm.Connection.Open() 
     
                    Dim ChkRecordRdr As SqlDataReader = CheckRecordComm.ExecuteReader() 
     
                    ChkRecordRdr.Read() 
     
                    If ChkRecordRdr.HasRows Then 
     
                        IfRecordExist = True 
     
                    Else 
     
                        IfRecordExist = False 
     
                    End If 
     
     
                End Using 
     
            Catch AnyException As Exception 
     
                IfRecordExist = False 
     
            End Try 
     
        End Function 
     
        Public Sub MakeComments() 
     
            If Val(lblUserID.Text) <= 0 Then 
     
                Response.Redirect("SignUp.aspx") 
     
            Else 
     
                CommentsBody.Visible = True 
     
            End If 
     
        End Sub 
     
        Public Sub ApplyComments() 
     
            If Val(lblUserID.Text) = 0 Then 
                Response.Redirect("SignUp.aspx") 
                Exit Sub 
            End If 
     
            Try 
     
                Using GlobalConnection As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     
                    Dim InsertCommentsStr As String = "INSERT INTO BlogThreads (BlogID, BlogComment, UserID, BlogCommentDate, BlogCommentTime) VALUES (@BlogID, @BlogComment, @UserID, @BlogCommentDate, @BlogCommentTime)" 
                    Dim CountAllComments As String = "SELECT COUNT (BlogComment) AS Comments FROM BlogThreads WHERE BlogID = @BlogID" 
     
                    Dim InsertCommentComm As New SqlClient.SqlCommand(InsertCommentsStr, GlobalConnection) 
     
     
                    If txtMessage.Value = Nothing Then 
                        Exit Sub 
     
                    Else 
     
                        InsertCommentComm.Parameters.AddWithValue("UserID", Val(lblUserID.Text)) 
                        InsertCommentComm.Parameters.AddWithValue("BlogID", Val(lblSelectedTopic.Text)) 
                        InsertCommentComm.Parameters.AddWithValue("BlogComment", txtMessage.Value) 
                        InsertCommentComm.Parameters.AddWithValue("BlogCommentDate", Format(System.DateTime.Now.Date, "Short Date")) 
                        InsertCommentComm.Parameters.AddWithValue("BlogCommentTime", Format(System.DateTime.Now, "Short Time")) 
     
                        InsertCommentComm.Connection.Open() 
                        InsertCommentComm.ExecuteScalar() 
     
                        InsertCommentComm.CommandText = CountAllComments 
     
                        Dim rdrSelectBlogTopic As SqlDataReader = InsertCommentComm.ExecuteReader() 
     
                        rdrSelectBlogTopic.Read() 
                        lblComments.InnerHtml = "Comments: " & rdrSelectBlogTopic.Item("Comments") 
                        rdrSelectBlogTopic.Close() 
     
                        InsertCommentComm.Connection.Close() 
     
                        CancelComments() 
                    End If 
     
                End Using 
     
     
            Catch ex As Exception 
     
            End Try 
     
            RadGridComments.MasterTableView.Rebind() 
     
            CancelComments() 
     
        End Sub 
     
        Public Sub ShowNewTopic() 
     
            Discussion.Visible = False 
            StartNewTopic.Visible = True 
            CommentsMenu.Visible = False 
            CommentsBody.Visible = False 
     
        End Sub 
     
        Public Sub PostNewTopic() 
     
            'errormessage.Visible = False 
     
            If Val(lblUserID.Text) = 0 Then 
     
                Response.Redirect("SignUp.aspx") 
                Exit Sub 
            End If 
     
            Try 
     
                Using GlobalConnection As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     
                    Dim InsertCommentsStr As String = "INSERT INTO BlogTopics (BlogTopic, BlogMessage, UserID, BlogTopicCreatedDate, BlogTopicCreatedTime) VALUES (@BlogTopic, @BlogMessage, @UserID, @BlogTopicCreatedDate, @BlogTopicCreatedTime)" 
                    Dim InsertCommentComm As New SqlClient.SqlCommand(InsertCommentsStr, GlobalConnection) 
     
                    InsertCommentComm.Parameters.AddWithValue("UserID", Val(lblUserID.Text)) 
                    InsertCommentComm.Parameters.AddWithValue("BlogTopic", txtTopic.Value.Trim) 
                    InsertCommentComm.Parameters.AddWithValue("BlogMessage", TextArticle.Value) 
                    InsertCommentComm.Parameters.AddWithValue("BlogTopicCreatedDate", Format(System.DateTime.Now.Date, "Short Date")) 
                    InsertCommentComm.Parameters.AddWithValue("BlogTopicCreatedTime", Format(System.DateTime.Now, "Short Time")) 
     
                    InsertCommentComm.Connection.Open() 
                    InsertCommentComm.ExecuteScalar() 
                    InsertCommentComm.Connection.Close() 
     
     
                End Using 
     
     
            Catch ex As Exception 
     
                BloggerName.InnerHtml = ex.ToString 
     
            End Try 
     
            RadGridBlogTopics.MasterTableView.Rebind() 
     
            CancelPost() 
     
        End Sub 
     
        Public Sub CancelPost() 
     
            Discussion.Visible = False 
            TextArticle.Value = "" 
            txtTopic.Value = "" 
            StartNewTopic.Visible = False 
            CommentsMenu.Visible = False 
            CommentsBody.Visible = False 
     
        End Sub 
     
        Public Sub CancelComments() 
            txtMessage.Value = "" 
            txtMessage.Value = Nothing 
            CommentsBody.Visible = False 
        End Sub 
     
     
    End Class 
     

    Now test your blog and make any further modifications to suit your needs...thank you!

    tzb806@yahoo.com, facebook.com/terfabinda






  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 09 Dec 2009 Link to this post

    Hello Terfa,

    Thank you for sharing your experience with the community. I hope the other users will find your sample useful.
    I have updated your Telerik point respectively.


    All the best,
    Iana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top