Get the session and current page in the class that does not inherit the page

Source: Internet
Author: User

 /*************** Obtain the session and current page ************ in the class that does not inherit the page *********** * ******* <br/> * 1. method for obtaining the session: <br/> * system. web. httpcontext. current. session ["uname"] <br/> * 2. the method for getting the page (handler is equivalent to a handle): <br/> * system. web. httpcontext. current. handler as system. web. UI. page <br/> * 3. this. user. identity. name: Obtain the login name of the current user. <br/> * For example: gv2008/Administrator <br/> ********************************* ***************************************/ <Br/> using system; <br/> using system. data; <br/> using system. configuration; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. web. UI. htmlcontrols; </P> <p> /// <summary> <br/> // Summary of common <br/> /// </Summary> <br/> Public class common <br/>{< br/> Public static stri Ng username <br/> {<br/> Get <br/> {<br/> If (system. Web. httpcontext. Current. session ["uname"]! = NULL) <br/>{< br/> return system. web. httpcontext. current. session ["uname"]. tostring (); <br/>}< br/> else <br/>{< br/> system. web. UI. page curpage = system. web. httpcontext. current. handler as system. web. UI. page; <br/> int regid; // out type parameter. You can also use <br/> // Int. tryparse ("A123", out regid); // if it can be converted to data, data is returned. If it cannot be converted, 0 is returned. <br/> Int. tryparse (curpage. user. identity. name. split (',') [0], out regid); <br/> return curpage. title + curpage. user. identity. name + regid; <br/>}< br/> Public common () <br/> {<br/> // todo: add the constructor logic here <br/> // <br/>}< br/>Using system; <br/> using system. data; <br/> using system. configuration; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. web. UI. htmlcontrols; </P> <p> Public partial class _ default: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br />{< Br/> If (! Ispostback) <br/> BIND (); <br/>}< br/> private void BIND () <br/> {<br/> // session ["uname"] = "greatverve"; <br/> response. write (Common. username); <br/>}< br/>

Related Article

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.