コード例 #1
0
    protected string ViewResult()
    {
        string category    = "";
        string res         = "";
        string spname      = "";
        int    numarticles = 0;
        string apath       = anm_Utility.GetWebAppRoot();

        if (Request.QueryString["category"] != null)
        {
            spname   = "anm_SearchNewsbyCatPaged";
            category = Request.QueryString["category"];
        }
        else
        {
            spname = "anm_SearchNewsPaged";
        }

        anm_Utility ut    = new anm_Utility();
        string      text  = ut.UrlDecode(Request.QueryString["title"].ToString()).Trim();
        string      value = text.Replace("[", "[[]");

        value       = value.Replace("%", "[%]");
        value       = value.Replace("_", "[_]");
        value       = value.Trim();
        Label1.Text = text;

        string        strConn      = ConfigurationManager.ConnectionStrings["anmcs"].ToString();
        SqlConnection myConnection = new SqlConnection(strConn);
        SqlCommand    myCommand    = new SqlCommand(spname, myConnection);

        myCommand.CommandType = CommandType.StoredProcedure;
        if (category != "")
        {
            Page.Title = GetGlobalResourceObject("language", "Search") + ": " + text + " - " + ut.GetCategory(Request.QueryString["category"]) + " - " + ut.GetSetting("SiteName");
            myCommand.Parameters.Add("@idcategory", SqlDbType.VarChar).Value = category;
        }
        else
        {
            Page.Title = GetGlobalResourceObject("language", "Search") + ": " + text + " - " + ut.GetSetting("SiteName");
        }
        myCommand.Parameters.Add("@title", SqlDbType.NVarChar).Value = value;
        int startrow = 0;
        int rows     = 15;
        int page     = 1;

        if (Request.QueryString["page"] != null)
        {
            startrow = rows * (Convert.ToInt32(Request.QueryString["page"]) - 1);
        }
        myCommand.Parameters.Add("@startRowIndex", SqlDbType.VarChar).Value = startrow;
        myCommand.Parameters.Add("@maximumRows", SqlDbType.VarChar).Value   = rows;
        myConnection.Open();
        SqlDataReader reader = myCommand.ExecuteReader();

        while (reader.Read())
        {
            res += Result(reader["title"].ToString(), reader["idnews"].ToString(), reader["image"].ToString(), reader["summary"].ToString(), reader["news"].ToString());
        }
        myConnection.Close();

        if (res == "")
        {
            string[] words     = value.Split(' ');
            string   condition = "(";
            for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Length > 3)
                {
                    string val = words[i].ToString();
                    condition += "((title LIKE '%' + '" + val + "' + '%') OR (news LIKE '%' + '" + val + "' + '%') OR (summary LIKE '%' + '" + val + "' + '%')) AND ";
                }
            }
            if (condition != "(")
            {
                condition = condition.Remove(condition.Length - 4, 4) + ") and";
            }
            else
            {
                condition = "";
            }
            if (Request.QueryString["category"] != null)
            {
                SqlCommand myCommand2 = new SqlCommand();
                myCommand2.Connection = myConnection;
                myConnection.Open();
                myCommand2.CommandText = "SELECT title,idnews,image,Summary,news FROM (SELECT title,idnews,image,Summary,news, ROW_NUMBER() OVER(ORDER BY idnews  DESC) AS RowNumber FROM anm_News,anm_Categories WHERE " + condition + " published='true' and anm_News.idcategory = anm_Categories.idcategory and (anm_News.idcategory = " + Request.QueryString["category"] + " or anm_Categories.idfather = " + Request.QueryString["category"] + " or anm_Categories.idrootcat = " + Request.QueryString["category"] + ")) AS NewsWithRowNumbers WHERE RowNumber > " + startrow + " AND RowNumber <= " + (startrow + rows) + "";
                SqlDataReader reader2 = myCommand2.ExecuteReader();
                while (reader2.Read())
                {
                    res += Result(reader2["title"].ToString(), reader2["idnews"].ToString(), reader2["image"].ToString(), reader2["summary"].ToString(), reader2["news"].ToString());
                }
                myConnection.Close();
                Page.Title          = GetGlobalResourceObject("language", "Search") + ": " + text + " - " + ut.GetCategory(Request.QueryString["category"]) + " - " + ut.GetSetting("SiteName");
                DDcat.SelectedValue = Request.QueryString["category"];
                numarticles         = ut.GetNumberSearchResults("SELECT COUNT(*) FROM anm_News,anm_Categories WHERE " + condition + " published='true' and anm_News.idcategory = anm_Categories.idcategory and (anm_News.idcategory = " + Request.QueryString["category"] + " or anm_Categories.idfather = " + Request.QueryString["category"] + " or anm_Categories.idrootcat = " + Request.QueryString["category"]);
            }
            else
            {
                SqlCommand myCommand3 = new SqlCommand();
                myCommand3.Connection = myConnection;
                myConnection.Open();
                myCommand3.CommandText = "SELECT title,idnews,image,Summary,news FROM (SELECT title,idnews,image,Summary,news, ROW_NUMBER() OVER(ORDER BY idnews  DESC) AS RowNumber FROM anm_News WHERE " + condition + " published='true') AS NewsWithRowNumbers WHERE RowNumber > " + startrow + " AND RowNumber <= " + (startrow + rows) + "";
                SqlDataReader reader3 = myCommand3.ExecuteReader();
                while (reader3.Read())
                {
                    res += Result(reader3["title"].ToString(), reader3["idnews"].ToString(), reader3["image"].ToString(), reader3["summary"].ToString(), reader3["news"].ToString());
                }
                myConnection.Close();
                Page.Title  = GetGlobalResourceObject("language", "Search") + ": " + text + " - " + ut.GetSetting("SiteName");
                numarticles = ut.GetNumberSearchResults("SELECT COUNT(*) FROM anm_News WHERE " + condition + " published='true'");
            }
            if (res == "")
            {
                condition = "(";
                for (int i = 0; i < words.Length; i++)
                {
                    if (words[i].Length > 3)
                    {
                        string val = words[i].ToString();
                        condition += "(title LIKE '%' + '" + val + "' + '%') OR (news LIKE '%' + '" + val + "' + '%') OR (summary LIKE '%' + '" + val + "' + '%') OR ";
                    }
                }
                if (condition != "(")
                {
                    condition = condition.Remove(condition.Length - 4, 4) + ") and";
                }
                else
                {
                    condition = "";
                }
                if (Request.QueryString["category"] != null)
                {
                    SqlCommand myCommand2 = new SqlCommand();
                    myCommand2.Connection = myConnection;
                    myConnection.Open();
                    myCommand2.CommandText = "SELECT title,idnews,image,Summary,news FROM (SELECT title,idnews,image,Summary,news, ROW_NUMBER() OVER(ORDER BY idnews  DESC) AS RowNumber FROM anm_News,anm_Categories WHERE " + condition + " published='true' and anm_News.idcategory = anm_Categories.idcategory and (anm_News.idcategory = " + Request.QueryString["category"] + " or anm_Categories.idfather = " + Request.QueryString["category"] + " or anm_Categories.idrootcat = " + Request.QueryString["category"] + ")) AS NewsWithRowNumbers WHERE RowNumber > " + startrow + " AND RowNumber <= " + (startrow + rows) + "";
                    SqlDataReader reader2 = myCommand2.ExecuteReader();
                    while (reader2.Read())
                    {
                        res += Result(reader2["title"].ToString(), reader2["idnews"].ToString(), reader2["image"].ToString(), reader2["summary"].ToString(), reader2["news"].ToString());
                    }
                    myConnection.Close();
                    Page.Title          = GetGlobalResourceObject("language", "Search") + ": " + text + " - " + ut.GetCategory(Request.QueryString["category"]) + " - " + ut.GetSetting("SiteName");
                    DDcat.SelectedValue = Request.QueryString["category"];
                    numarticles         = ut.GetNumberSearchResults("SELECT COUNT(*) FROM anm_News,anm_Categories WHERE " + condition + " published='true' and anm_News.idcategory = anm_Categories.idcategory and (anm_News.idcategory = " + Request.QueryString["category"] + " or anm_Categories.idfather = " + Request.QueryString["category"] + " or anm_Categories.idrootcat = " + Request.QueryString["category"]);
                }
                else
                {
                    SqlCommand myCommand3 = new SqlCommand();
                    myCommand3.Connection = myConnection;
                    myConnection.Open();
                    myCommand3.CommandText = "SELECT title,idnews,image,Summary,news FROM (SELECT title,idnews,image,Summary,news, ROW_NUMBER() OVER(ORDER BY idnews  DESC) AS RowNumber FROM anm_News WHERE " + condition + " published='true') AS NewsWithRowNumbers WHERE RowNumber > " + startrow + " AND RowNumber <= " + (startrow + rows) + "";
                    SqlDataReader reader3 = myCommand3.ExecuteReader();
                    while (reader3.Read())
                    {
                        res += Result(reader3["title"].ToString(), reader3["idnews"].ToString(), reader3["image"].ToString(), reader3["summary"].ToString(), reader3["news"].ToString());
                    }
                    myConnection.Close();
                    Page.Title  = GetGlobalResourceObject("language", "Search") + ": " + text + " - " + ut.GetSetting("SiteName");
                    numarticles = ut.GetNumberSearchResults("SELECT COUNT(*) FROM anm_News WHERE " + condition + " published='true'");
                }
            }
        }
        else
        {
            if (category == "")
            {
                numarticles = ut.GetNumberSearchResults("SELECT COUNT(*) FROM [anm_News] WHERE (([title] LIKE '%' + '" + value + "' + '%') OR ([news] LIKE '%' + '" + value + "' + '%') OR ([summary] LIKE '%' + '" + value + "' + '%')) and published='true' and date<GETDATE()");
            }
            else
            {
                numarticles = ut.GetNumberSearchResults("SELECT COUNT(*) FROM [anm_News],[anm_Categories] WHERE (([title] LIKE '%' + '" + value + "' + '%') OR ([news] LIKE '%' + '" + value + "' + '%') OR ([summary] LIKE '%' + '" + value + "' + '%')) and published='true' and anm_News.idcategory = anm_Categories.idcategory and (anm_News.idcategory = " + category + " or anm_Categories.idfather = " + category + " or anm_Categories.idrootcat = " + category + ") and date<GETDATE()");
            }
        }
        if (res != "")
        {
            string linknav     = "<div class='linkpage'>";
            int    maximumRows = rows;
            int    maxpage     = (numarticles / maximumRows) + 1;
            if (numarticles % maximumRows == 0)
            {
                maxpage = numarticles / maximumRows;
            }
            if (currentpage != 0)
            {
                page = currentpage;
                if (numarticles > maximumRows)
                {
                    for (int i = (page - 5); i < (page + 10); i++)
                    {
                        if (i >= 1 && i <= (maxpage))
                        {
                            if (category != "")
                            {
                                if (page == i)
                                {
                                    linknav += "<a href='" + apath + "/page" + i + "_cat" + category + "_search/" + text + ".aspx' class='pagenavselected'>" + i + "</a> ";
                                }
                                else
                                {
                                    linknav += "<a href='" + apath + "/page" + i + "_cat" + category + "_search/" + text + ".aspx' class='pagenav'>" + i + "</a> ";
                                }
                            }
                            else
                            {
                                if (page == i)
                                {
                                    linknav += "<a href='" + apath + "/page" + i + "/search/" + text + ".aspx' class='pagenavselected'>" + i + "</a> ";
                                }
                                else
                                {
                                    linknav += "<a href='" + apath + "/page" + i + "/search/" + text + ".aspx' class='pagenav'>" + i + "</a> ";
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 1; i < 11; i++)
                {
                    if (category != "")
                    {
                        if (i == 1)
                        {
                            linknav += "<a href='" + apath + "/page" + i + "_cat" + category + "_search/" + text + ".aspx' class='pagenavselected'>" + i + "</a> ";
                        }
                        if (i > 1 && i <= maxpage)
                        {
                            linknav += "<a href='" + apath + "/page" + i + "_cat" + category + "_search/" + text + ".aspx' class='pagenav'>" + i + "</a> ";
                        }
                    }
                    else
                    {
                        if (i == 1)
                        {
                            linknav += "<a href='" + apath + "/page" + i + "/search/" + text + ".aspx' class='pagenavselected'>" + i + "</a> ";
                        }
                        if (i > 1 && i <= maxpage)
                        {
                            linknav += "<a href='" + apath + "/page" + i + "/search/" + text + ".aspx' class='pagenav'>" + i + "</a> ";
                        }
                    }
                }
            }
            if (linknav != "<div class='linkpage'>")
            {
                linknav += "- " + GetGlobalResourceObject("language", "Page") + " " + page + " " + GetGlobalResourceObject("language", "Of") + " " + maxpage + "</div>";
            }
            else
            {
                linknav = "";
            }
            LTpagelink.Text = linknav;
            if (res == "" || maxpage == 1)
            {
                LTpagelink.Visible = false;
            }
        }
        else
        {
            res = "- No results.";
        }

        return(res);
    }