Discussion of ASP. NET through the machine cookie imitation Baidu (Google) Implementation of search input search prompt pop-up box own initiative

Source: Internet
Author: User

To achieve their own initiative to pop through the user input keyword-related search results, here, I give two solutions for two different scenarios.


A common approach is to create a user database to find relational tables. Then enter the user search box keyword the related data in the asynchronous call data table. Displayed in a hidden div.
The other way is the way I focus on the discussion now. Applies to 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, the following to get to the chase. This article mainly describes the implementation steps, code can be based on their actual needs to change.
One, how to write a cookie? In order to write cookies. His steps are mainly three-step, in detail such as the following:
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.

Details such as the following code:

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"; Suppose the cookie value you want to write is not a simple string, but rather a complex data type, and we know that these data types cannot be stored directly into a cookie. Because only strings can be stored in cookies. But you can do this by converting this complex data type into multiple strings, and assigning the strings to the resulting cookie value at the same time, so that the contents of the cookie are enriched and the later use of the cookie is powerful.

At this point you may be able to figure out why when you browse webserver,webserver you will know when you've browsed, and have been waiting too long for information. Since this information is stored on the first time you browse the page, webserver generated the cookie. The following code is a sample that stores multiple strings in a cookie:

Cookie ["name"] = "Wang Tian"; Cookie ["gender"] = "male"; cookie ["age"] = "26";
The cookie is temporary. And there's always. Persistent cookies are stored on your computer as files and remain on your computer when you turn off Internet Explorer.

When you revisit the site, the site where the Cookie was created can be read. This cookie is written at the time of detailed programming. Set the life cycle of the cookie, in details such as 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 create a cookie that has a lifetime of "one Day" and you can set the detailed lifetime of the cookie by altering the TimeSpan attribute.
OK, combine. Operation codes with cookies such as the following

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);//Join 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");        }//Change 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 available 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) {//infer 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 user search data. Then the user enters a search data pop-up hint to make some experiments.

This is the whole approach. There are interested classmates can discuss, we want to have a more effective realization.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Discussion of ASP. NET through the machine cookie imitation Baidu (Google) Implementation of search input search prompt pop-up box own initiative

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.