pipRaptor
  • Posts: 14
  • Joined: 18/11/2014
Before a note for the Administrator(s): I think this is the right forum for this matter... because it's a different approach to redirect users and visitors where you want, but in case move it to the most appropriate place, thanks


And now the point: I needed a BLOG, so I immediately thought to my loved YAF, since I used it as forum and it demonstrated me it's robust and reliable


1) The structure

The structure is very simple: I use only 1 forum and every topic is a BLOG

The first post of the topic is my BLOG and all the answers are the comments, so I simply put the visitors as ReadOnly while I disabled the ability to open new TOPICS for the register users, they can only answer (= comment) my BLOGs

...and the first step is terminated... very easy!


2) Every time a user or a new visitors open the site, he MUST see my last blog (topic) as first page

This was a bit more complex... but not so much!

I used a session variable, when you arrive into my BLOG for the first time or after a while, the variable is null, then I set it and it remains valid all the time you keep the BLOG open and also for some time after you left my site (or you keep it open but you don't use it)

Well, if you arrive to my site for the first time or after a while, you see my last BLOG, otherwise you see the normal YAF menu page

You simply must add this code at the beginning of the default.aspx page


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %>

<script runat="server">
    private void Page_Load(object sender, EventArgs e)
    {
        if (Request.RawUrl == "/" && Session["inSession"] == null)
        {
            Session["inSession"] = "Yes!";

            Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/db");
            ConnectionStringSettings connString = rootWebConfig.ConnectionStrings.ConnectionStrings["yafnet"];

            string lastTopic = string.Empty;
            SqlConnection sqlConnection = new SqlConnection(connString.ToString());
            SqlCommand sqlCommand = new SqlCommand();
            SqlDataReader sqlDataReader;

            sqlCommand.CommandText = "SELECT TOP(1) TopicID, Topic FROM yaf_Topic ORDER BY TopicID DESC";
            sqlCommand.CommandType = CommandType.Text;
            sqlCommand.Connection = sqlConnection;

            sqlConnection.Open();

            sqlDataReader = sqlCommand.ExecuteReader();
            sqlDataReader.Read();
            lastTopic = "t" + sqlDataReader[0].ToString() + '-' + sqlDataReader[1].ToString().Replace(" ", "-");

            sqlConnection.Close();

            Response.Redirect("~/posts/" + lastTopic);
        }
    }
</script>


The Request.RawUrl == "/" is only a further check to avoid infinite loops, but I saw it doesn't need...


3 and the last) I want you see always the page from the beginning

Since it's a BLOG, I want the users and visitors always start from the first post of the topic, my BLOG exactly, and not from the last comment!

So I added a client side javascript that reset the page position to 0,0 every time

You add this at the end of default page code (merge it to previous last row Response.Redirected....) and the client javascript prevails over the server side setting


            Response.Redirect("~/posts/" + lastTopic);
        }

        Page.ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "delayedScroll()", true);
    }
</script>

<script type="text/javascript">
    function delayedScroll() {
        timeoutID = window.setTimeout(window.scrollTo(0, 0), 2500);
    }
</script>


That's all and it can be easily improved or you can adapt the code to redirect new visitors and users where you want!

Thanks for your time, Marco
Sponsor
pipRaptor
  • Posts: 14
  • Joined: 18/11/2014
A couple of quick updates

1) If you have multiple forums and you want to transform only one of them in a BLOG, you must simply see the ForumID in the yaf_Topic table and substitute the query with this


sqlCommand.CommandText = "SELECT TOP(1) TopicID, Topic FROM yaf_Topic WHERE ForumID = 2 ORDER BY TopicID DESC";


Where ForumID is your BLOG

2) I don't check the SqlDataReader because I'm sure I've already more posts in my BLOG, but for a good programming, you shoud use a


while (sqlDataReader.Read())
{
    .....
}


Or a sqlDataReader.FieldCount

3) You can easily replace the Response.Redirect(".....your url...."); to send visitors or users that arrive to your site after a while, to a particular NEWS or ADVISE page, for example

4) If you put the final javascript call within the session variable check, you visualize the page from the top only the first time


            Response.Redirect("~/posts/" + lastTopic);
			Page.ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "delayedScroll()", true);
        }
    }


...or you can eliminate it at all!
Users browsing this topic
    Forum Jump  
    • You cannot post new topics in this forum.
    • You cannot reply to topics in this forum.
    • You cannot delete your posts in this forum.
    • You cannot edit your posts in this forum.
    • You cannot create polls in this forum.
    • You cannot vote in polls in this forum.

    About Us

    The YAF.NET is an open source .NET forum project. YAF.NET is supported by an team of international developers who are build community by building community software.

    Powered by Resharper Donate with PayPal button

    Project Twitter Updates

    Copyright © YetAnotherForum.NET & Ingo Herbote. All rights reserved