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