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

推荐订阅源

T
Threatpost
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security Affairs
N
News and Events Feed by Topic
T
Tenable Blog
P
Proofpoint News Feed
W
WeLiveSecurity
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
Help Net Security
Help Net Security
I
Intezer
T
Threat Research - Cisco Blogs
S
Secure Thoughts
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
Google Online Security Blog
Google Online Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Project Zero
Project Zero
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tor Project blog
N
News | PayPal Newsroom
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
Arctic Wolf
Forbes - Security
Forbes - Security
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Latest
Security Latest
P
Palo Alto Networks Blog
S
Schneier on Security
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园_首页
T
Troy Hunt's Blog
Latest news
Latest news
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
L
LINUX DO - 热门话题
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
V
Visual Studio Blog
H
Hacker News: Front Page

博客园 - JasonBie

使用NPOI编辑Excel C# datagridview 快速导出数据到Excel Outlook2016 不能自动配置企业Exchange的解决办法 Linq实现left join左连接 电脑端微信语音像机器人解决办法 解决sql server collation conflict Asp.net APP 重置密码的方式 jQuery dataTables 列不对齐的原因 JavaScript 获得客户端IP Entity Framework Linq 动态组合where条件 查询SQLSERVER执行过的SQL记录 Asp.net Web API 返回Json对象的两种方式 Read Excel file from C# JavaScript测试工具比较: QUnit, Jasmine, and Mocha Asp.net Form验证后造成URL参数重复的问题 MVC删除数据的方法 Session State Cookie View State
Transferring Information Between Pages
JasonBie · 2012-04-12 · via 博客园 - JasonBie

Transferring Information Between Pages

Corss-Page Posting

Here’s an example—a page named CrossPage1.aspx that  defines a form with two text boxes and a button. When the button is clicked, it posts to a page named CrossPage2.aspx. 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CrossPage1.aspx.cs" Inherits="CrossPage1" %> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>CrossPage1</title> 
</head> 
<body> 
    <form id="form1" runat="server" > 
      <div> 
        First Name: 
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> 
        <br /> 
        Last Name: 
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> 
        <br /> 
        <asp:Button runat="server" ID="cmdPost"  
          PostBackUrl
="CrossPage2.aspx" Text="Cross-Page Postback" /><br /> 
      </div> 
    </form> 
</body> 
</html>

public partial class CrossPage2 : System.Web.UI.Page 

    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (PreviousPage != null) 
        { 
            lblInfo.Text = "You came from a page titled " + 
            PreviousPage.Title; 
        } 
    } 
}

Getting More Information from the Source Page

protected void Page_Load(object sender, EventArgs e) 

    CrossPage1 prevPage = PreviousPage as CrossPage1; 
    if (prevPage != null
    { 
        // (Read some information from the previous page.) 
    } 
}

In a projectless website, Visual St udio may flag this as an error, indi cating that it does not have the type information for the source page class (in this exampl e, that’s CrossPage1). However, once you compile the website, the error will disappear.

You can solve the problem like this:

Add this code in CrossPage2.aspx

<%@ PreviousPageType VirtualPath="~/CrossPage1.aspx" %> 

Now, the PreviousPage property will automatically use the CrossPage1 type. That allows you to skip 

the casting code and go straight to work using the previous page object, like this: 

protected void Page_Load(object sender, EventArgs e) 

    if (PreviousPage != null
    { 
        // (Read some information from the previous page.) 
    } 

A better choice is to define specific, limited methods or properties that extract just the information you need.

public partial class CrossPage1 : System.Web.UI.Page 

    public string FullName 
    { 
        get { return txtFirstName.Text + " " + txtLastName.Text; } 
    } 
}

Here’s how you can rewrite the code in CrossPage2.aspx to display the information from CrossPage1.aspx: 

protected void Page_Load(object sender, EventArgs e) 

    if (PreviousPage != null
    { 
        lblInfo.Text = "You came from a page titled " + 
            PreviousPage.Title + "<br />"
 
        CrossPage1 prevPage = PreviousPage as CrossPage1; 
        if (prevPage != null
        { 
            lblInfo.Text += "You typed in this: " + prevPage.FullName; 
        } 
    } 

The Query String

Here’s an example: 

http://www.google.ca/search?q=organic+gardening

Here’s an example that uses this approach with the Response.Redirect() method: 

// Go to newpage.aspx. Submit a single query string argument 
// named recordID, and set to 10. 
Response.Redirect("newpage.aspx?recordID=10"); 

You can send multiple parameters as long as they’re separated with an ampersand (&): 

// Go to newpage.aspx. Submit two query string arguments: 
// recordID (10) and mode (full). 
Response.Redirect("newpage.aspx?recordID=10&mode=full"); 

The receiving page has an easier time working with the query string. It can receive the values from 

the QueryString dictionary collection exposed by the built-in Request object: 

string ID = Request.QueryString["recordID"]; 

Note that information is always retrieved as a string, which can then  be converted to another simple data type. Values in the QueryString collection are indexed by the variable name. If you attempt to retrieve a value that isn’t present in the query string, you’ll get a null reference. 

URL Encoding

To perform URL encoding, you use the UrlEncode() and UrlDecode() methods of the HttpServerUtility class. As you learned in Chapter  5, an HttpServerUtility object is made available to your code in every web form through the Page.Server property. The following code uses the UrlEncode() method to rewrite the previous example, so it works with product names that contain special characters: 

string url = "QueryStringRecipient.aspx?"
url += "Item=" +  Server.UrlEncode(lstItems.SelectedItem.Text) + "&"
url += "Mode=" + chkDetails.Checked.ToString(); 
Response.Redirect(url);

Notice that it’s important not to encode everything. In this example, you can’t encode the & character that joins the two query  string values, because it truly  is  a special character. 

You can use the UrlDecode() method to return a URL- encoded string to its initial value. However, you don’t need to take this step with the query s tring. That’s because ASP.NET automatically decodes your values when you access them through the Reque st.QueryString collection.