For the user input keyword implementation automatically pop-up related search results, here I give two solutions for two different situations.
A common method is to create a user search relationship table in the database and then asynchronously invoke the relevant data from the data table by using the keywords entered by the user search box to appear in a hidden div.
The second way, which I am now focusing on, is for a single user, based on the user's previous search data, to implement the search hint function. The key technique is to record the user's previous search data, write a cookie, and then invoke the data from the user's native cookie.
OK, let's get down to the chase. This article mainly describes the implementation steps, the code can be changed according to their actual needs.
One, how to write a cookie? In order to write a cookie, his steps are mainly three-step, as follows:
The first thing to do is create a HttpCookie object that constructs a cookie with the name of the cookie that will be generated later. The following code is specific:
HttpCookie cookie = new HttpCookie ("Myonlycookiename");//define your own marked cookie name
Then assign a string value to the "value" property of the created HttpCookie object, and the value of "value" is the value of the cookie that is subsequently generated.
such as: Mynamecookie.value = "User to the cookie assignment"; If the value of the cookie you want to write is not a simple string, but rather a complex data type, we know that these data types cannot be stored directly into a cookie because the cookie can only store strings. But you can do this by converting this complex data type into multiple strings and assigning the multiple strings to the resulting cookie value so that the content in the cookie is rich and the functionality of the cookie is powerful. At this point you may understand why when you browse the Web server, the Web server will know when you've browsed, and have spent too much time waiting for information. Because this information is stored in the cookie that was generated by the Web server the first time you browsed the page. The following code is an example of storing multiple strings to a cookie:
Cookie ["name"] = "Wang Tian"; Cookie ["gender"] = "male"; cookie ["age"] = "26";
Cookies are temporary and permanent. Persistent cookies are stored on your computer as files and remain on your computer when you turn off Internet Explorer. When you visit the site again, the Web site where the Cookie was created can be read. In the specific programming time, when writing this cookie, set the life cycle of the cookie, the following code:
DateTime Dtnow = DateTime. Now;
TimeSpan Tsminute = new TimeSpan (1, 0, 0, 0);
Cookies. Expires = Dtnow + Tsminute;
The above code is set to produce a cookie that has a lifetime of "one Day" and you can modify the "TimeSpan" attribute to set the specific lifetime of the cookie being generated.
OK, together, the operation code with the cookie is as follows
public partial class cookietest:system.web.ui.page{string Cookiekye = "Jinwebcookies"; protected void Page_Load (object sender, EventArgs e) {} protected void Writecookie () {Cookiekye = Rea Dcookie (); HttpCookie cookie = new HttpCookie (Cookiekye);//define Cookie object and item named info DateTime dt = datetime.now;//Define Time Object Tim Espan ts = new TimeSpan (0, 0, 0),//cookie effective time. Cookies. Expires = dt. Add (ts);//Added action time string Searchkey=searchid.text.trim (); Cookies. Values.add (Searchkey, Searchkey);//Add attribute Response.appendcookie (cookie);//OK to write cookie} private string R Eadcookie () {int i=0; while (I >= 0) {if (request.cookies[cookiekye+i] = = null) {return Cookiek Ye + i; } i++; } return ""; } protected void Democookie () {//Read cookie if (Request.cookies[cookiekye]! = null) { String temp = convert.tostring (request.cookies["Info"). Value); if (temp = = "") {Response.Write ("empty"); } else Response.Write (temp); } else {Response.Write ("error"); }//Modify Cookie response.cookies["Info" ["user"] = "2"; response.cookies["Info"]. Expires = DateTime.Now.AddDays (1); Delete the attribute under cookie HttpCookie acookie = request.cookies["Info"]; Acookie. Values.remove ("userid"); Acookie. Expires = DateTime.Now.AddDays (1); RESPONSE.COOKIES.ADD (Acookie); Delete all cookies, that is, set the expiration time is now on line int limit = request.cookies.count-1; for (int i = 0; i < limit; i++) {acookie = request.cookies[i]; Acookie. Expires = DateTime.Now.AddDays (-1); RESPONSE.COOKIES.ADD (Acookie); }} protected void Unnamed1_click (object sender, EventArgs e) {Writecookie (); }
Two, OK, now is the page end of the code for the page end, where I read the cookie data through JS, and then write the data to the prompt box
First, it's the JavaScript code.
<script> var Setsearchflag; function Showsearch (obj) {clearsearchflag (); var = (document.getElementById)? True:false; The standard var NS6 = (Navigator.appname = = "Netscape")? True:false; Netscape browser Standard, Var left, top; if (!NS6) {//judgment ie var nLt = 0; var nTp = 0; var offsetParent = obj; while (offsetParent = null && offsetParent! = document.body) {nLt + = Offsetparent.offsetleft; NTp + = Offsetparent.offsettop; OffsetParent = offsetparent.offsetparent; } left = NLt; top = nTp + obj.offsetheight; } else {//standard for the left = obj.offsetleft-5; top = obj.offsettop + obj.offsetheight; } $ (' #showInfo '). CSS (' Display ', ' block '); $ (' #showInfo '). CSS (' top ', top); var seach = GetCookie ("jinwebcookies"); $ (' #showInfo '). HTML (seach); } function Hidesearch (obj) {setsearchflag = SetTimeout (function () {$ (' #showInfo '). CSS (' Display ', ' none ');}, 10 0); } function Clearsearchflag () {window.cleartimeout (Setsearchflag);//cancels the timer set by the SetTimeout () method. } function GetCookie (cookiename) {//read cookie var cookiecontent = '; var cookieary = Document.cookie.split (";"); Get the cookie array for (var i = 0; i < cookieary.length; i++) {//var cookiename = cookiename + i; var temp = cookieary[i].split ("="); if (temp[0] = = cookiename) {cookiecontent = Unescape (temp[1]); }} return cookiecontent; }//write Cookie//document.cookie = "cookiename=" + Escape ("What to write"); CookieName is the name of the cookie to be written </script>
OK, then insert a search box, then trigger JS
<form id= "Form1" runat= "Server" > <asp:textbox runat= "Server" id= "Searchid" onkeyup= "Showsearch (This)" Onblur= "Hidesearch (This)"/> <asp:button runat= "Server" text= "Btn_search Addcookie" onclick= " Unnamed1_click "/></form><div style=" width:150px;height:300px;border:1px solid #817F82;d Isplay:none; Position:absolute; "id=" Showinfo "></div>
The above is my cookie to save the user search data, and then the user input search data pop-up tips to make some experiments. The overall approach is this way, interested students can discuss together, hoping to have a more effective way to achieve.