网页导航技术
1.使用HyperLink
2.Response.Redirect()
3.Server.Transfer()
4.跨网页公布(Cross-Page Posting)
跨网页公布Cross-Page Posting
访问来源网页中的控件:<%@ PreviousPageType....
Source:
Button的PostBackUrl属性
Traget:
TextBox txtUserName=(TextBox)PreviousPage.FindControl("txtUserName");
lblText.Text="your name is "+txtUserName.Text;
访问来源网页的公共属性值
PreviousPageTypeSource.aspx
PreviousPageTypeSource.aspx.cs
....
public partial class _4_PreviousPageTypeSource : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//这里一定要有,因为不管是Postback回本页面,还是通过Postbackurl指定Postback到另一个页面,Page_Load都会执行.(也就是说,首次载入页面时执行一次,当点击Button时再执行一次,然后Postback到Postbackurl指定的页面)
{
txtUserName.Text = "Everett";
}
}
public string UserName
{
get
{
return txtUserName.Text;
}
}
}
PreviousPageTypeSource.aspx
页面头部加上:
<%@ PreviousPageType VirtualPath="~/4/PreviousPageTypeSource.aspx" %>
注:一个page只能有一个PreviousPageType指示符
PreviousPageTypeSource.aspx.cs
public partial class _4_PreviousPageTypeTarget : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMsg.Text = "your name is:" + PreviousPage.UserName;
}
}
访问来源网页中的控件:<%@ Reference...
ReferenceSource.aspx
ReferenceSource.aspx.cs
public partial class _4_ReferenceSource : System.Web.UI.Page
{
public string city = "长春";
protected void Page_Load(object sender, EventArgs e)
{
}
public string UserName
{
get
{
return txtUserName.Text;
}
}
}
ReferenceSource1.aspx
ReferenceSource1.aspx.cs
public partial class _4_ReferenceSource1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txtUserName.Text = "Everett";
}
}
public string UserName
{
get
{
return txtUserName.Text;
}
}
}
ReferenceTarget.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReferenceTarget.aspx.cs" Inherits="_4_ReferenceTarget" %>
<%@ Reference VirtualPath="~/4/referencesource.aspx" %>
<%@ Reference VirtualPath="~/4/ReferenceSource1.aspx" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>
ReferenceTarget.aspx.cs
public partial class _4_ReferenceTarget : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Try//一定要进行这种判断,因为网页来源对于本网页来说是不可见的,因此当下面的代码产生错误时就可以不终止程序.
{
if (PreviousPage != null)
{
if (PreviousPage.IsCrossPagePostBack)
{
_4_ReferenceSource sourcePage = (_4_ReferenceSource)PreviousPage;
lblMsg.Text = "your name:" + sourcePage.UserName + "," + sourcePage.city + "," + sourcePage.User;
}
}
}catch{}
try
{
if (PreviousPage != null)
{
if (PreviousPage.IsCrossPagePostBack)
{
_4_ReferenceSource1 sourcePage = (_4_ReferenceSource1)PreviousPage;
lblMsg.Text = "your name is:" + sourcePage.UserName + ">" + sourcePage.Page;
}
}
}
catch { }
}
}
IsCrossPagePostBack vs. IsPostBack
跨网页公布不是真正的PostBack,且在跨网页公布导向目标网页后,IsPostBack属性会被设置为null.跨网页公布必须使用"IsCrossPagePostBack"这个属性来判断.
ClientScript
WebForm的DefaultButton和DefaultFocus
using System.Drawing;
public partial class _4_FocusDefault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMsg.Text =
"defaultbutton:" + form1.DefaultButton.ToString() + "<br>" +
form1.DefaultFocus.ToString();
for (int i = 0; i < form1.Controls.Count; i++)
{
if (form1.DefaultFocus == form1.Controls[i].ID)
{
switch (form1.Controls[i].GetType().Name)
{
case "TextBox":
((TextBox)form1.Controls[i]).BackColor = Color.LightPink;
break;
case "Button":
((Button)form1.Controls[i]).BackColor = Color.LightPink;
break;
}
}
if (form1.DefaultButton == form1.Controls[i].ID)
{
try
{
((Button)form1.Controls[i]).BackColor = Color.LightBlue;
}
catch { }
}
}
}
protected void btnOK_Click(object sender, EventArgs e)
{
lblMsg.Text = "your account:" + txtAccount.Text + "/" + txtPassword.Text;
txtAccount.Text = null;
txtPassword.Text = null;
}
}
Client Script动态注册
同目录下建立一个js文件
jsinclude.js
function showName(username)
{
alert('your name is:'+username);
}
RegisterClientScript.aspx
<form id="form1" runat="server">
<div>
input your name:<input id="txtUserName" style="width: 182px" type="text" />
<input id="btnSubmit" style="width: 116px" type="button" value="ok" onclick="showName(txtUserName.value);" />
<input id="Button1" type="button" value="hello" onclick="hiYou(txtUserName.value);" style="width: 93px"/>
</div>
</form>
RegisterClientScript.aspx.cs
public partial class _4Script_RegisterClientScriptInclude : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/包含js文件
if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "fsMessage"))
{
Page.ClientScript.RegisterClientScriptInclude("fsMessage", "jsinclude.js");
}
注册指令区块
string info = "";
info += "function hiYou(username)" +
"{alert('hi '+username+',i miss you!');}";
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "fsHello"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "fsHello", info, true);
}
用于注册只执行一次的代码
string showfirst = "";
showfirst += @"<script>alert('welcome to Everett/' s Home.');</script>";
if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "Welcome"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Welcome", showfirst);
}
}
}
Client-Callback ( Client异步Callback)
Callback.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="callback.aspx.cs" Inherits="_4Callback_callback" %>
<!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>无标题页</title>
<script type="text/javascript">
function DoSearch()
{
var txtName=document.getElementById('txtUserName');
CallServer(txtName.value,'');
}
function ReceiveFSData(txtUserInfo)
{
Results.innerText=txtUserInfo;
}
setInterval('DoSearch()',1000);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Name:<input id="txtUserName" type="text" /><br />
<span id="Results" ></span>
</div>
</form>
</body>
</html>
Callback.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class _4Callback_callback :
System.Web.UI.Page,
ICallbackEventHandler
{
protected string txtUserInfo;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string vipRef = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveFSData", "context");
string lastSent = "function CallServer(arg,context)" +
"{" +
vipRef +
"}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", lastSent, true);
}
}
public void RaiseCallbackEvent(string txtFirstName)
{
if (txtFirstName != null)
{
string connstr = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand("select EmployeeID,FirstName,City,Address from Employees where FirstName=@FirstName", conn);
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = txtFirstName;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtUserInfo = "No:" + dr["EmployeeID"] + " ," +
"Name:" + dr["FirstName"] + " ," +
"City:" + dr["City"] + " ," +
"Address:" + dr["Address"].ToString().Replace("/r/n", "") + " ," +
"[Server searching time:" + DateTime.Now.ToLongTimeString() + "]";
}
else
{
if (string.IsNullOrEmpty(txtFirstName))
{
txtUserInfo = "Please input your name";
}
else
{
txtUserInfo = "No Result";
}
}
cmd.Dispose();
dr.Dispose();
conn.Dispose();
}
}
public string GetCallbackResult()
{
return txtUserInfo;
}
}