重拾ASP.NET页面间传值

1.Use Query String

:以前总是忽略了Server.UrlEncode()  AND Server.UrlDecode() 切记,切记!

(1)Source Page

1 protected void QueryStringButton_Click(object sender, EventArgs e)
2 {
3 Response.Redirect("QueryStringPage.aspx?Data=" + Server.UrlEncode(DataToSendTextBox.Text));
4 }

(2)Target Page

1  <form id="form1" runat="server">
2 <div>
3 <h1>Target Page - Query String</h1>
4 The data received from the source page is: <%=Server.UrlDecode(Request.QueryString["Data"]) %><br /><br />
5 </div>
7 </form>

2.Use Session

(1)Source Page

 

1  protected void SessionStateButton_Click(object sender, EventArgs e)
2 {
3 Session["Data"] = DataToSendTextBox.Text;
4 Response.Redirect("SessionStatePage.aspx");
5 }

(2)Target page

 

 <div>
<h1>Target Page - Session State</h1>
The data received from the source page is: <%=Session["Data"] %><br /><br />
</div>
</form>

3.Use HttpPost

说明:印象中这是第一次见到这种用法,这次该记住了。

(1)Source Page

 <asp:Button ID="HttpPostButton" runat="server" Text="Use HttpPost" 
PostBackUrl
="~/HttpPostPage.aspx"/><br /><br />

(2)Target page

<h1>Target Page - HttpPost</h1>
The data received from the source page is: <%=Request.Form["DataToSendTextBox"] %><br /><br />
<fieldset>
<legend>All form data from the sending page</legend>
<asp:Label ID="ReceivedDataLabel" runat="server" Text="Label"></asp:Label>
</fieldset>
<br />

 

  protected void Page_Load(object sender, EventArgs e)
{
System.Text.StringBuilder displayValues =
new System.Text.StringBuilder();
System.Collections.Specialized.NameValueCollection
postedValues = Request.Form;
String nextKey;
for (int i = 0; i < postedValues.AllKeys.Length; i++)
{
nextKey = postedValues.AllKeys[i];
if (nextKey.Substring(0, 2) != "__")
{
displayValues.Append("<br>");
displayValues.Append(nextKey);
displayValues.Append(" = ");
displayValues.Append(postedValues[i]);
}
}
ReceivedDataLabel.Text = displayValues.ToString();

}

4.Use Public Properties

(1)Source Page

 说明:这个方法也是很少用,不过在某些时候还是用得到的。

 public string DataToSend
{
get
{
return DataToSendTextBox.Text;
}
}

protected void PublicPropertiesButton_Click(object sender, EventArgs e)
{
Server.Transfer("PublicPropertiesPage.aspx");
}

(2)target page

 

<div>
<h1>Target Page - Public Properties</h1>
The data received from the source page is: <%=PreviousPage.DataToSend %><br /><br />
<asp:hyperlink ID="Hyperlink1" runat="server" NavigateUrl="~/Default.aspx">Return to Source Page</asp:hyperlink>
</div>

5.Use Control Infomation

(1)Source Page

 说明:以前只有在应用母版页时才去FindControl(),长眼见了。

<asp:TextBox ID="DataToSendTextBox" runat="server" Text="Hello World!"></asp:TextBox><br /><br />


 protected void ControlInfoButton_Click(object sender, EventArgs e)
{
Server.Transfer("ControlInfoPage.aspx");
}

(2)Target page

 

<h1>Target Page - Control Info</h1>
The data received from the source page is:
<asp:Label ID="DataReceivedLabel" runat="server" Text="Label"></asp:Label><br /><br />

 

protected void Page_Load(object sender, EventArgs e)
{
var textbox = PreviousPage.FindControl("DataToSendTextbox") as TextBox;
if (textbox != null)
{
DataReceivedLabel.Text = textbox.Text;
}

}



反思:似乎作者没有把Cookies放到这一类来,why?


 

你可能感兴趣的:(asp.net)