Search, and cache search history

Source: Internet
Author: User

1. Add keywords to cookie cache

 //<summary>       //Add search keywords        //</summary>  &N Bsp    //<param name= "KeyWord" ></param>        private void Addkeyword (String ke Yword)         {            KeyWord = Keyword.trim ();            HttpCookie cook;            if (request.cookies["keywordlist"] = = NULL )             {                Cook = new HttpCookie ( "KeywordList");                Cook. Expires = DateTime.Now.AddMonths (1);           }            E lse            {                Cook = Request.cooki es["KeywordList"];           }&NBSP;           list<string> List = Getkeyword (HttpContext);          &N Bsp if (list. Contains (KeyWord)             {                list. RemoveAll (p = p = = KeyWord);           }            if (l Ist. Count >= 3)             {                list. RemoveAt (0);           }            list. ADD (KeyWord);            String productlist = Jsonconvert.serializeobject (list);  & nbsp         Cook. Value = Httputility.urlencode (productlist, Encoding.UTF8);            Cook. Expires = DateTime.Now.AddDays (5);            RESPONSE.COOKIES.ADD (Cook);    & nbsp  }

 2, getting search keywords

 ///<summary>       ///  get search keywords        //</summary>& nbsp      //<returns></returns>        public list<string> Getkeyword (HttpContextBase context)         {            HttpCookie Cook = context. request.cookies["KeywordList"];            list<string> result = new list<string& gt; ();            if (Cook = = null)             {                return result;           }      &N Bsp     else            {                Jsons Erializer jserial = new Jsonserializer ();                StringReader strread = NE W StringReader (HttputIlity. UrlDecode (Cook. Value, Encoding.UTF8));                result = jserial. Deserialize<list<string>> (New JsonTextReader (Strread));           }  & nbsp         return result;       }

3. Render calls on the page

 <div class= "Search" >            <div class= "Demo-container Demo" >            <div class= "Search1" >               @* &L T;img src= "~/storeweb/shop/images/searchinput.png" id= "img"/>*@              & nbsp <input type= "text" value= "@ (Keywordlist.count > 0? keywordlist[0]:") "id=" Searchinput "class=" Sinput "Placehold Er= "Please enter find content"/>                <a class= "sbtn" href= "javascript:; id=" Searc H "title=" Search > Search </a>            </div>          &NBS P </div>            <div class= "Searchwork" >          &NB Sp     <p class= "searchworklist" >                    @{&nbs P     &NBSp                 foreach (var item in keywordlist)         &NBSP ;               {                    &N Bsp       <span> @item </span>                    & nbsp  }                   }            & nbsp   </p>            </div>        </div>

Search, and cache search history

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.