惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - live-evil

写给那些ASP.NET程序员:网站中的安全问题 FCKEditor编辑器安全配置 Ajax 安全工具 微软近日发布了三个免费SQL INJECTION检测工具 Enterprise Library 写给设计人的10个jQuery特效 - live-evil - 博客园 SQL注入攻击-来自微软安全博客的建议 用Dos命令进行加锁 防止病毒格式化硬盘 MembershipUser.IsOnline属性 FCKEditor for .NET. Nmap Techniques 希望所有的人都平安 端口复用小示例 DataReader Deom2_NextResult DataReader Demo. RssPageDemo. DataTable Demo. RSS技术实现(asp.net) 'sys' is undefined 错误.
ASP.NET页面传值的方法
live-evil · 2008-05-04 · via 博客园 - live-evil

From:Refresh-air

在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用。但是要全面的回答ASP.NET中页面传值的方式,估计往往很难全面。
一.
使用QueryString变量
    QueryString是一种非常简单也是使用比较多的一种传值方式,但是它将传递的值显示在浏览器的地址栏中,如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。

         Response.Redirect( "target.aspx?param1=hello&param2=hi ")
        接收页面:   string   str   =   Request.QueryString["param1"]; 
                               string   str1   =  Request.QueryString["param2];
二.使用Cookie对象变量(Cookie是存放在客户端的)
       设置Cookie:   HttpCookie cookie_name = new HttpCookie("name");
                         cookie_name.Value = Label1.Text;
                         Reponse.AppendCookie(cookie_name);
   
          获取Cookie:
                       string name= Request.Cookie["name"].Value.ToString();
 
三. 使用Session变量(session是存放在服务器端的)
  设置Session:      Session["name"] ="hello";
        获取Session:        string name = Session["name"].ToString();
四.使用Application 对象变量
  Application对象的作用范围是整个全局,也就是说对所有用户都有效。此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。 
        设置Application :    Application["name"] = ="hello";
        获取Application :     string   name = Application["name"].ToString();
五. PostBackUrl()方法

default.aspx页面:


1 <asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage" PostBackUrl="~/Default2.aspx" />
2

  default2.aspx页面:


1 if (PreviousPage != null)
2        {
3            TextBox textBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
4           Response.write(textBox1.Text );
5        }


六.使用Server.Transfer方法
    这个才可以说是 面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方 法是完全面象对象的,简洁有效。下面这个代码是展示在需要很多个参数的时候,使用的方法,如果参数比较少就没必要使用这个方法了.
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作!

1、先定义一个类,用该类放置所有查询参数:


/// <summary>
/// QueryParams 的摘要说明
/// </summary>

public class QueryParams

  
private   string   firstName; 
        
private   string   lastname;
        
private   int    age;
      

         
public string Firstname 
        
{
            
get return this.firstname; }
            
set this.firstname = value; } 
        }
 
        
public string LastName 
        
{
            
get return this.lastname; }
            
set this.lastname = value; } 
        }

        
public string Age
        
{
            
get return this.age; }
            
set this.age = value; }
        }
 
 
}



2、接口定义: 


///   <summary > 
    
///   定义查询接口。 
    
///   </summary > 

    public interface IQueryParams
    
{
        
///   <summary > 
        
///   参数 
        
///   </summary > 

        QueryParams Parameters get;}
    }
 


     3、查询页面继承IQueryParams接口(QueryPage.aspx):
QueryPage.aspx


<form id="form1" runat="server">
    
<div>
        
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
         
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
        
<asp:Button ID="btnEnter" runat="server" Text="Button" OnClick="btnEnter_Click" /></div>
    
</form>


QueryPage.aspx.cs


public partial class QueryPage : System.Web.UI.Page, IQueryParams 
{
    
private QueryParams queryParams;
   
        
public   QueryParams   Parameters 
        

            
get 
            

                 
return   queryParams; 
            }
 
        }
 
       
        
public   void   btnEnter_Click(object   sender,   System.EventArgs   e) 
        

            
//赋值 
            queryParams   =   new   QueryParams();
            queryParams.FirstnName 
= this.txtFirstName.Text;
            queryParams.Lastname 
= this.txtLastName.Text;
            queryParams.Age 
= this.txtAge.Text;
            Server.Transfer( 
"ResultPage.aspx "); 
        }


    
protected void Page_Load(object sender, EventArgs e)
    
{  }
}

4、接收页面(ResultPage.aspx):
ResultPage.aspx.cs
public partial class ResultPage : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        QueryParams queryParams 
= new QueryParams();
        IQueryParams queryInterface;
        
//实现该接口的页面 
        if (Context.Handler is IQueryParams)
        
{
            queryInterface 
= (IQueryParams)Context.Handler;
            queryParams 
= queryInterface.Parameters;
        }


        Response.Write(
"FirstName: ");
        Response.Write(queryParams.FirstName);
        Response.Write(
" <br/ >Lastname: ");
        Response.Write(queryParams.LastName); 
        Response.Write(
" <br/ >Age: ");
        Response.Write(queryParams.Age); 

    }

}