关于数据返回给调用页面

发表于:2007-06-30来源:作者:点击数: 标签:
我们经常需要将数据返回给调用页面,下面是基本实现方式。 假设,有两个页子selec td ate.aspx,calendar.aspx. 需要在selectdate.aspx做个连接,点击后打开calendar.aspx,然后在calendar.aspx中选择日期,将选择的日期返回给 selectdate.aspx(呵呵,不知道
我们经常需要将数据返回给调用页面,下面是基本实现方式。
假设,有两个页子selectdate.aspx,calendar.aspx.
需要在selectdate.aspx做个连接,点击后打开calendar.aspx,然后在calendar.aspx中选择日期,将选择的日期返回给
selectdate.aspx(呵呵,不知道我说明白没有)
算了,看看代码吧,自然就明白了。
对了,还需要个文件用来装JAVASCRIPT的script.js
===============================================================================================
selectdate.aspx
----------------------------------------
<%@ Page language="c#" Codebehind="detail.aspx.cs" AutoEventWireup="false" Inherits="calendar.detail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>detail</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript" src="script.js"></script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="CompletionDate" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 72px"
runat="server"></asp:TextBox>
<A href="javascript:OpenCalendar(@#CompletionDate@#, true)"><IMG style="Z-INDEX: 102; LEFT: 296px; WIDTH: 24px; POSITION: absolute; TOP: 72px; HEIGHT: 16px"
height="16" alt="" src="img/calendar.gif" width="24"></A>
</form>
</body>
</HTML>

--------------------------------------------------------------------
selectdate.aspx.cs
----------------------------------------------------------------------
sing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace calendar
{
/// <summary>
/// detail 的摘要说明。
/// </summary>
public class detail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox CompletionDate;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}




===========================================================================
calendar.aspx
=========================================================================

<%@ Page language="c#" Codebehind="calendar.aspx.cs" AutoEventWireup="false" Inherits="calendar.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function CloseWindow()
{
self.close();
}
</script>
</HEAD>
<body>
<form id="Calendar" method="post" runat="server">
<asp:Calendar id="Cal" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 16px" runat="server"
BorderWidth="1px" BackColor="#FFFFCC" Width="220px" DayNameFormat="FirstLetter" ForeColor="#663399"
Height="200px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#FFCC66" ShowGridLines="True">
<TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle>
<SelectorStyle BackColor="#FFCC66"></SelectorStyle>
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle>
<DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle>
<SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle>
<TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle>
<OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle>
</asp:Calendar>
<asp:Button id="OKButton" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 224px" runat="server"
Text="O k"></asp:Button>
<a href="javascript:CloseWindow()">
<asp:Button id="CancelButton" style="Z-INDEX: 103; LEFT: 136px; POSITION: absolute; TOP: 224px"
runat="server" Text="Cancel"></asp:Button>
</a>
<asp:Label id="lblDate" style="Z-INDEX: 104; LEFT: 216px; POSITION: absolute; TOP: 232px" runat="server"></asp:Label>
<asp:DropDownList id="MonthSelect" style="Z-INDEX: 105; LEFT: 96px; POSITION: absolute; TOP: 256px"
runat="server" AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList id="YearSelect" style="Z-INDEX: 106; LEFT: 248px; POSITION: absolute; TOP: 256px"
runat="server" AutoPostBack="True"></asp:DropDownList>
<input id="datechosen" type="hidden" name="datechosen" runat="server"> //这个很重要
</form>
</body>
</HTML>

---------------------------------------------------------------------------------------------------------------------------
calendar.aspx.cs
--------------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace calendar
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Calendar Cal;
protected System.Web.UI.WebControls.Button OKButton;
protected System.Web.UI.WebControls.Button CancelButton;
protected System.Web.UI.WebControls.Label lblDate;
protected System.Web.UI.WebControls.DropDownList MonthSelect;
protected System.Web.UI.HtmlControls.HtmlInputHidden datechosen;
protected System.Web.UI.WebControls.DropDownList YearSelect;

private void Page_Load(object sender, System.EventArgs e)
{

if (!Page.IsPostBack)
{
string selected = Request.QueryString["selected"];
string id = Request.QueryString["id"];
string form = Request.QueryString["formname"];
string postBack = Request.QueryString["postBack"];

Cal.FirstDayOfWeek = (System.Web.UI.WebControls.FirstDayOfWeek)1;//(System.Web.UI.WebControls.FirstDayOfWeek)Convert.ToInt32(ConfigurationSettings.AppSettings[Global.CfgKeyFirstDayOfWeek]);

try
{
Cal.SelectedDate = Cal.VisibleDate = Convert.ToDateTime(selected);
}
catch
{
Cal.SelectedDate = Cal.VisibleDate = DateTime.Today;
}

FillCalendarChoices();
SelectCorrectValues();

OKButton.Attributes.Add("onClick", "window.opener.SetDate(@#" + form + "@#,@#" + id + "@#, document.Calendar.datechosen.value," + postBack + ");");
CancelButton.Attributes.Add("onClick", "CloseWindow()");
}// 在此处放置用户代码以初始化页面
}
private void FillCalendarChoices()
{
DateTime thisdate = new DateTime(DateTime.Today.Year,1,1);
for (int x=0; x<12; x++)
{
ListItem li = new ListItem(thisdate.ToString("MMMM"), thisdate.Month.ToString());
MonthSelect.Items.Add(li);
thisdate = thisdate.AddMonths(1);
}

for (int y=1994; y <= thisdate.Year; y++)
{
YearSelect.Items.Add(y.ToString());
}
}

private void SelectCorrectValues()
{
lblDate.Text = Cal.SelectedDate.ToShortDateString();
datechosen.Value = lblDate.Text;
MonthSelect.SelectedIndex = MonthSelect.Items.IndexOf(MonthSelect.Items.FindByValue(Cal.SelectedDate.Month.ToString()));
YearSelect.SelectedIndex = YearSelect.Items.IndexOf(YearSelect.Items.FindByValue(Cal.SelectedDate.Year.ToString()));
}
private void Cal_SelectionChanged(object sender, System.EventArgs e)
{
Cal.VisibleDate = Cal.SelectedDate;
SelectCorrectValues();
}

private void MonthSelect_SelectedIndexChanged(object sender, System.EventArgs e)
{
Cal.SelectedDate = Cal.VisibleDate
= new DateTime(Convert.ToInt32(YearSelect.SelectedItem.Value),
Convert.ToInt32(MonthSelect.SelectedItem.Value), 1);;
SelectCorrectValues();
}

private void YearSelect_SelectedIndexChanged(object sender, System.EventArgs e)
{
Cal.SelectedDate = Cal.VisibleDate
= new DateTime(Convert.ToInt32(YearSelect.SelectedItem.Value),
Convert.ToInt32(MonthSelect.SelectedItem.Value), 1);;
SelectCorrectValues();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Cal.SelectionChanged += new System.EventHandler(this.Cal_SelectionChanged);
//this.OKButton.Click += new System.EventHandler(this.OKButton_Click);
//this.CancelButton.Click += new System.EventHandler(this.CancelButton_Click);
this.MonthSelect.SelectedIndexChanged += new System.EventHandler(this.MonthSelect_SelectedIndexChanged);
this.YearSelect.SelectedIndexChanged += new System.EventHandler(this.YearSelect_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
this.ID = "Calendar";

}
#endregion

private void CancelButton_Click(object sender, System.EventArgs e)
{

}

private void OKButton_Click(object sender, System.EventArgs e)
{

}
}
}


==========================================================================================
script.js
===================================================================================
var popUp;

function OpenCalendar(idname, postBack)
{
popUp = window.open(@#Calendar.aspx?formname=@# + document.forms[0].name +
@#&id=@# + idname + @#&selected=@# + document.forms[0].elements[idname].value + @#&postBack=@# + postBack,
@#popupcal@#,
@#width=285,height=420,left=200,top=250@#);
}

function SetDate(formName, id, newDate, postBack)
{
eval(@#var theform = document.@# + formName + @#;@#);
popUp.close();
theform.elements[id].value = newDate;
if (postBack)
__doPostBack(id,@#@#);
}
=============================================================================


原文转自:http://www.ltesting.net