{"id":540,"date":"2009-07-23T17:35:00","date_gmt":"2009-07-23T09:35:00","guid":{"rendered":""},"modified":"2009-07-23T17:35:00","modified_gmt":"2009-07-23T09:35:00","slug":"","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/540.html","title":{"rendered":"ASP.NET\u9875\u9762\u4f20\u503c\u6c47\u603b"},"content":{"rendered":"<p><html><head><meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>ASP.NET\u9875\u9762\u4f20\u503c\u6c47\u603b<\/title><\/head><body><\/p>\n<h1 style=\"display:none\">ASP.NET\u9875\u9762\u4f20\u503c\u6c47\u603b<\/h1>\n<div>\n<p><font size=\"2\">\u3000\u3000\u4ecb\u7ecd\uff1a<\/font><\/p>\n<p><font size=\"2\">\u3000 \u3000\u5728\u7f51\u9875\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u4e2d\uff0c\u9875\u9762\u4e4b\u95f4\u7684\u4f20\u503c\u5e94\u8be5\u662f\u6700\u5e38\u89c1\u7684\u95ee\u9898\u4e86\u3002 \u5728\u8fd9\u7bc7\u6587\u7ae0\u91cc\uff0cazamsharp \u5c06\u4e3a\u6211\u4eec\u4ecb\u7ecd\u4e00\u4e9bASP.NET\u9875\u9762\u4f20\u503c\u7684\u65b9\u5f0f\u3002\u672c\u6587\u6240\u4e3e\u7684\u4f8b\u5b50\u975e\u5e38\u7b80\u5355\uff0c\u4ec5\u4ec5\u5305\u542b\u4e86\u4e00\u4e2a\u6587\u672c\u6846\u548c\u51e0\u4e2a\u6309\u94ae\uff0c\u5f53\u6309\u94ae\u70b9\u51fb\u65f6\u6587\u672c\u6846\u4e2d\u7684\u5b57\u7b26\u4e32\u5c06\u4f1a\u4ee5\u4e0d\u540c\u7684\u65b9 \u5f0f\u4f20\u9012\u5230\u53e6\u5916\u7684\u9875\u9762\u53bb\u3002<\/font><\/p>\n<p style=\"background-color: rgb(255,255,255)\"><font size=\"2\">\u3000\u30001. Response.Redirect \uff08\u6216\u79f0 Query String \u65b9\u5f0f\u3001URL\u65b9\u5f0f\uff09<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><font size=\"2\">\u3000\u3000Response.Redirect(&quot;WebForm5.aspx&quot;);<\/font><\/p>\n<p><font size=\"2\">\u3000 \u3000\u9996\u5148\u8ba9\u6211\u4eec\u770b\u770b Response.Redirect \u65b9\u6cd5\uff0c\u8fd9\u5e94\u8be5\u662f\u6700\u7b80\u5355\u7684\u4e86\uff0c\u5f53\u6211\u4eec\u70b9\u51fbResponse.Redirect\u6309\u94ae\u540e\u89e6\u53d1\u4e0a\u9762\u7684\u90a3\u884c\u4ee3\u7801\u5373\u53ef\u3002\u5728\u8fd9\u91cc\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e2a\u5c0f\u6280\u5de7\uff1a\u6709\u4e9b\u65f6\u5019\u6211\u4eec\u4f1a\u628a \u4e0a\u9762\u7684\u4ee3\u7801\u653e\u5728try{ &#8230; }catch() \u4e2d\u53bb\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u6211\u4eec\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u628a\u5f02\u5e38\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u7f51\u9875\u3002\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u8fd9\u6837\u505a\uff0c\u4e5f\u8bb8\u4f1a\u5f97\u5230\u4e00\u4e2a&ldquo;System.Threading&rdquo; \u5f02\u5e38\uff0c\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u8df3\u8f6c\u5230\u4e86\u53e6\u5916\u4e00\u4e2a\u9875\u9762\uff0c\u800c\u539f\u6765\u7684\u9875\u9762\u7ebf\u7a0b\u4ecd\u7136\u5728\u8fd0\u884c\u4e2d\u3002\u53ef\u4ee5\u8fd9\u6837\u6765\u89e3\u51b3\u6b64\u95ee\u9898\uff1a<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><font size=\"2\">\u3000\u3000Response.Redirect(&quot;WebForm5.aspx&quot;,false);<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u8fd9\u610f\u5473\u7740\u544a\u8bc9\u7f16\u8bd1\u5668\u53bb&ldquo;WebForm5.aspx&rdquo;\uff0c \u540c\u65f6&ldquo;false&rdquo;\u53c2\u6570\u544a\u8bc9\u7f16\u8bd1\u5668\u4e0d\u8981\u505c\u6b62\u5728\u539f\u6765\u9875\u9762\u4e0a\u7684\u6d3b\u52a8\uff0c \u8fd9\u65b9\u9762\u6211\u4eec\u53ef\u4ee5\u53c2\u89c1System.Threading\u7c7b\u3002<\/font><\/p>\n<p><clk><\/clk><font size=\"2\">\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c&ldquo;txtName&rdquo;\u662f\u5f85\u4f20\u503c\u7684\u6587\u672c\u6846\u7684\u540d\u5b57\uff0c\u90a3\u4e2a\u8ddf\u5728&ldquo;?&rdquo;\u540e\u9762\u7684&ldquo;Name&rdquo; \u662f\u4e00\u4e2a\u4e34\u65f6response\u53d8\u91cf\u7684\u6807\u5fd7\uff0c\u5b83\u627f\u8f7d\u4e86txtName\u7684\u503c\u3002<\/font><\/p>\n<p><\/p>\n<div>\n<div class=\"article_c\">\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">privatevoidButton1_Click(objectsender,System.EventArgse)<br \/>\n{<br \/>\n\/\/ValuesentusingHttpResponse<br \/>\nResponse.Redirect(&quot;WebForm5.aspx?Name=&quot;+txtName.Text);<br \/>\n}<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000 \u3000OK\uff0c\u5230\u6b64\u4f4d\u7f6e\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86Response\u7684\u4f20\u503c\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5728&quot;WebForm5.aspx&quot;\u4e2d\u600e\u6837\u63a5\u6536\u4f20\u6765\u7684\u503c\u5462\uff1f\u4e0d\u8981\u6025\uff0c\u6211\u4eec\u5c06 \u5728&quot;WebForm5.aspx&quot;\u7684page_Load\u4e8b\u4ef6\u4e2d\u5199\u4e0b\u9762\u7684\u4ee3\u7801\u3002\u9996\u5148\uff0c\u6211\u4eec\u8981\u786e\u8ba4\u4f20\u6765\u7684\u503c\u4e0d\u662f&ldquo;null&rdquo;\uff0c\u5982\u679c\u4e0d\u662f&ldquo;null&rdquo;\uff0c\u6211\u4eec\u5c31\u53ef \u4ee5\u7528Label\u5c06\u8fd9\u4e2a\u503c\u663e\u793a\u51fa\u6765\u3002<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u6ce8\uff1a\u5f53\u6211\u4eec\u4f7f\u7528Response.Redirect\u4f20\u9012\u53d8\u91cf\u65f6\uff0c\u6240\u6709\u7684\u53d8\u91cf\u90fd\u80fd\u5728\u6d4f\u89c8\u5668\u7684URL\u4e2d\u770b\u5230\uff0c\u6211\u4eec\u4e0d\u80fd\u7528\u8fd9\u79cd\u65b9\u6cd5\u6765\u4f20\u9012\u91cd\u8981\u7684\u673a\u5bc6\u6570\u636e\uff0c\u5982\u4fe1\u7528\u5361\u53f7\u7b49\u3002<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">if(Request.QueryString[&quot;Name&quot;]!=null)<br \/>\nLabel3.Text=Request.QueryString[&quot;Name&quot;];<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000\u30002. Cookies \u65b9\u5f0f<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u63a5\u4e0b\u6765\u662f\u7528cookies, Cookies\u7531\u670d\u52a1\u7aef\u521b\u5efa\uff0c\u4f46\u4fdd\u5b58\u5728\u5ba2\u6237\u7aef\uff0c\u5f53\u6211\u4eec\u70b9\u51fb&ldquo;Cookies&rdquo;\u6309\u94ae\u65f6\uff0c\u8fd0\u884c\u5982\u4e0b\u4ee3\u7801\uff1a<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">HttpCookiecName=newHttpCookie(&quot;Name&quot;);<br \/>\ncName.Value=txtName.Text;<br \/>\nResponse.Cookies.Add(cName);<br \/>\nResponse.Redirect(&quot;WebForm5.aspx&quot;);<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000 \u3000\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u53eb&ldquo;cName&rdquo;\u7684cookie\u5b9e\u4f8b\uff0c\u7531\u4e8e\u4e00\u4e2acookie\u5b9e\u4f8b\u53ef\u4ee5\u4fdd\u5b58\u591a\u4e2a\u503c\uff0c\u6211\u4eec\u9700\u8981\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u4e2acookie\u5c06\u4fdd\u5b58 &ldquo;Name&rdquo;\u503c\uff0c\u5e76\u628atxtName.Text\u7684\u503c\u8d4b\u7ed9\u5b83\uff0c\u5e76\u628a\u5b83\u52a0\u5230&quot;\u8f93\u51fa\u6d41&rdquo; \u4e2d\uff0c\u5e76\u4f7f\u7528Response.Redirect\u8f93\u51fa\u5230\u53e6\u5916\u4e00\u4e2a\u7f51\u9875\u3002<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u7136\u540e\u8ba9\u6211\u4eec\u770b\u770b\u600e\u6837\u5728\u76ee\u6807\u9875\u9762\u4e2d\u53d6\u51fa\u8fd9\u4e2acookie\u4e2d\u7684\u503c\uff1a<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><font color=\"#000000\" size=\"2\" face=\"NSimsun\"><code>if(Request.Cookies[&quot;Name&quot;]!=null)<br \/>\nLabel3.Text=Request.Cookies[&quot;Name&quot;].Value;<\/code><\/font><\/p>\n<div>\n<div class=\"article_c\">\n<p><font size=\"2\">\u3000\u3000\u663e\u7136\uff0c\u6b65\u9aa4\u8ddf\u4e0a\u4e00\u79cd\u65b9\u6cd5\u975e\u5e38\u76f8\u4f3c\uff0c\u53ea\u662f\u4f7f\u7528Request.Cookies\u4ee3\u66ff\u4e86Request.QueryString\u3002<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u6ce8\uff1a\u4e00\u4e9b\u6d4f\u89c8\u5668\u662f\u4e0d\u652f\u6301Cookies\u7684\u3002<\/font><\/p>\n<p><font size=\"2\">\u3000\u30003. Session \u53d8\u91cf<\/font><\/p>\n<p><font size=\"2\">\u3000 \u3000\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u770b\u5728\u670d\u52a1\u7aef\u7ef4\u6301\u7684Session\u53d8\u91cf\u3002 Session\u5728\u7528\u6237\u5411\u670d\u52a1\u7aef\u53d1\u51fa\u9996\u6b21\u8bf7\u6c42\u65f6\u88ab\u521b\u5efa\uff0c\u800c\u5728\u7528\u6237\u5173\u95ed\u6d4f\u89c8\u5668\u6216\u5f02\u5e38\u53d1\u751f\u65f6\u7ec8\u6b62\uff08\u5176\u5b9e\u8fd8\u6709\u8fc7\u671f\u7684\u60c5\u51b5\uff09\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u7528Session\u6765\u4f20\u503c\u7684 \u4f8b\u5b50\u3002 \u6211\u4eec\u53ef\u4ee5\u770b\u5230 Session \u4e3a\u7528\u6237\u521b\u5efa\u4e86&ldquo;Name&rdquo; \u952e\uff0c\u5e76\u628aTextBox\u7684\u503c\u8d4b\u7ed9\u5b83\u3002<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><font size=\"2\">\u3000\u3000Code<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">\/\/Session\u521b\u5efa<br \/>\nSession[&quot;Name&quot;]=txtName.Text;<br \/>\nResponse.Redirect(&quot;WebForm5.aspx&quot;);<br \/>\n\/\/\u4e0b\u9762\u7684\u4ee3\u7801\u663e\u793a\u5982\u4f55\u4eceSession\u4e2d\u53d6\u503c<br \/>\n\/\/\u4ee3\u7801\u653e\u5728\u5176\u5b83\u9875\u9762\u4e2d<br \/>\nif(Session[&quot;Name&quot;]!=null)<br \/>\nLabel3.Text=Session[&quot;Name&quot;].ToString();<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000\u30004. Application\u3000\u53d8\u91cf<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u6709\u4e9b\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u503c\u80fd\u591f\u5728\u6240\u6709\u7684\u9875\u9762\u4e2d\u8bbf\u95ee\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Application\u53d8\u91cf\u3002 \u5982\u4e0b\u5217\u4ee3\u7801\u6240\u793a\uff0c\u4e00\u65e6\u6211\u4eec\u521b\u5efa\u4e86Application\u53d8\u91cf\u5e76\u8d4b\u503c\uff0c\u5c31\u53ef\u4ee5\u5728\u7f51\u7ad9\uff08\u9879\u76ee\uff09\u7684\u6240\u6709\u9875\u9762\u4e2d\u83b7\u5f97\u5b83\u3002<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><font size=\"2\">\u3000\u3000Code<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">\/\/\u4e3aApplication\u53d8\u91cf\u8d4b\u503c<br \/>\nApplication[&quot;Name&quot;]=txtName.Text;<br \/>\nResponse.Redirect(&quot;WebForm5.aspx&quot;);<br \/>\n\/\/\u4eceApplication\u53d8\u91cf\u4e2d\u53d6\u51fa\u503c<br \/>\nif(Application[&quot;Name&quot;]!=null)<br \/>\nLabel3.Text=Application[&quot;Name&quot;].ToString();<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000\u30005. Server.Transfer\u65b9\u5f0f\uff08\u6216\u79f0HttpContext\u65b9\u5f0f\uff09<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 Server.Transfer\u65b9\u5f0f\uff08\u6216\u79f0HttpContext\u65b9\u5f0f\uff09\u5728\u9875\u9762\u4e4b\u95f4\u4f20\u9012\u53d8\u91cf\uff0c\u6b64\u65f6\uff0c\u8981\u4f20\u9012\u7684\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027\u6216\u65b9\u6cd5\u6765\u83b7\u5f97\uff0c\u4f7f\u7528\u5c5e\u6027\u5c06\u4f1a\u6bd4\u8f83\u5bb9\u6613\u4e00\u4e9b\u3002\u597d\uff0c\u8ba9\u6211\u4eec\u5728\u7b2c\u4e00\u4e2a\u9875\u9762\u4e2d\u6765\u5199\u4e00\u4e2a\u7528\u6765\u83b7\u5f97TextBox\u503c\u7684\u5c5e\u6027\uff1a<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><font size=\"2\">\u3000\u3000Code<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">publicstringGetName<br \/>\n{<br \/>\nget{returntxtName.Text;}<br \/>\n}<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000\u3000\u6211\u4eec\u9700\u8981\u4f7f\u7528Server.Transfer\u628a\u8fd9\u4e2a\u503c\u53d1\u9001\u5230\u53e6\u5916\u4e00\u4e2a\u9875\u9762\u4e2d\u53bb\uff0c\u8bf7\u6ce8\u610fServer.Transfer\u53ea\u662f\u53d1\u9001\u63a7\u4ef6\u5230\u4e00\u4e2a\u65b0\u7684\u9875\u9762\u53bb\uff0c\u800c\u5e76\u4e0d\u4f1a\u4f7f\u6d4f\u89c8\u5668\u91cd\u5b9a\u5411\u5230\u53e6\u4e00\u4e2a\u9875\u9762\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u6211\u4eec\u5728\u5730\u5740\u680f\u4e2d\u4ecd\u7136\u770b\u5230\u7684\u662f\u539f\u6765\u9875\u9762\u7684URL\u3002\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">Server.Transfer(&quot;WebForm5.aspx&quot;);<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000\u3000\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5230&quot;WebForm5.aspx&quot;\u770b\u770b\uff1a<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><font size=\"2\">\u3000\u3000Code<\/font><\/p>\n<p style=\"background-color: rgb(204,255,204)\"><code><font size=\"2\" face=\"NSimsun\">\/\/YoucandeclarethisGloballyorinanyeventyoulike<br \/>\nWebForm4w;<br \/>\n\/\/GetsthePage.ContextwhichisAssociatedwiththispage<br \/>\nw=(WebForm4)Context.Handler;<br \/>\n\/\/AssigntheLabelcontrolwiththeproperty&quot;GetName&quot;whichreturnsstring<br \/>\nLabel3.Text=w.GetName;<\/font><\/code><\/p>\n<p><font size=\"2\">\u3000\u3000\u7ed3\u675f\u8bed\uff1a<\/font><\/p>\n<p><font size=\"2\">\u3000\u3000\u5982\u6211\u4eec\u770b\u5230\u7684\u90a3\u6837\uff0c\u5404\u79cd\u4f20\u503c\u65b9\u5f0f\u90fd\u5404\u6709\u4f18\u52a3\uff0c\u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\u9009\u62e9\u9002\u5f53\u7684\u65b9\u5f0f\u662f\u5f88\u91cd\u8981\u7684\u3002<\/font><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET\u9875\u9762\u4f20\u503c\u6c47\u603b ASP.NET\u9875\u9762\u4f20\u503c\u6c47\u603b \u3000\u3000\u4ecb\u7ecd\uff1a \u3000 \u3000\u5728\u7f51\u9875\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u4e2d\uff0c\u9875\u9762\u4e4b\u95f4\u7684\u4f20 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-540","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=540"}],"version-history":[{"count":0,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}