Strongly typed method.
1. First in the Source PageCodeBlock, declare a public attribute;
2. Add the <% previouspagetype %> command in the rendering block of the target page, and specify the virtualpath attribute.
3. Use previouspage to directly call the Public attributes of the Source Page code block.
Example:
Source Page
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "typed. aspx. cs" inherits = "typed" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> strongly typed method </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label1" runat = "server" associatedcontrolid = "textbox1" text = "name:"> </ASP: Label>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" onclick = "button#click" postbackurl = "~ /Testtyped. aspx"
TEXT = "complete"/> </div>
</Form>
</Body>
</Html>
Public partial class typed: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Public String Username
{
Get
{
Return this. textbox1.text. tostring ();
}
}
Protected void button#click (Object sender, eventargs E)
{
}
}
Host page
Protected void page_load (Object sender, eventargs E)
{
If (previouspage! = NULL)
{
Response. Write (string. Format ("Welcome {0}. Use the strong method to send data across pages! ", Previouspage. username ));
}
}
Weak type method:
1. In the code block of the target page, use the findcontrol method of previouspage to obtain the corresponding control and then convert it;
2. Call the converted control property.
Example:
Source Page
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "untyped. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> weak type method </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label1" runat = "server" text = "name:" associatedcontrolid = "textbox1"> </ASP: Label>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "complete" postbackurl = "~ /Testuntyped. aspx "onclick =" button1_click "/> </div>
</Form>
</Body>
</Html>
Protected void page_load (Object sender, eventargs E)
{
If (previouspage! = NULL)
{
Textbox txtname = (textbox) previouspage. findcontrol ("textbox1 ");
Response. Write (string. Format ("Welcome {0} use the weak type method for sending data across pages! ", Txtname. Text. tostring ()));
}
}
Host page
protected void page_load (Object sender, eventargs e)
{< br> If (previouspage! = NULL)
{< br> response. Write (string. Format ("Welcome {0} to use the Forced method to send data across pages! ", Previouspage. username);
}< BR >}