The following two pages will demonstrate how to use client side "window.showModalWindow" in combination with ASP.Net server side PostBack.
=======================WebForm1.aspx=========================
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 48px" runat="server"
Width="512px" Height="256px" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="StringColumn" HeaderText="StringColumn"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="DateColumn">
<ItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%# DateTime.Parse(DataBinder.Eval(Container, "DataItem.DateColumn").ToString()).ToShortDateString() %>'>
</asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Edit Value"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
<script runat="server">
protected DataTable dt;
protected void Page_Load(object sender, System.EventArgs e)
{
DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
dt = new DataTable("TestTable");
dt.Columns.Add("StringColumn",typeof(string));
dt.Columns.Add("DateColumn",typeof(DateTime));
dt.Rows.Add(new object[]{"String 1",DateTime.Now});
dt.Rows.Add(new object[]{"String 2",DateTime.Now});
DataGrid1.DataSource = dt;
DataGrid1.DataBind();
}
protected void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//Eliminate the headers and footers.
if (e.Item.Cells[1].Controls.Count == 5)
{
TextBox tx = (TextBox)e.Item.Cells[1].Controls[1];
Button btn = (Button)e.Item.Cells[1].Controls[3];
btn.Attributes.Add("onclick","return buttonClick('" + tx.ClientID + "');");
}
}
</script>
<script language="javascript">
function buttonClick(senderTextBoxID) {
//debugger;
var i;
var senderTextBox;
for (i = 0; i < event.srcElement.parentElement.children.length; i++) {
if (event.srcElement.parentElement.children[i].id == senderTextBoxID) {
senderTextBox = event.srcElement.parentElement.children[i];
}
}
var returnValue;
returnValue = window.showModalDialog("CalendarDialog.aspx?selectedDate=" + senderTextBox.value);
//debugger;
if (returnValue != null) {
senderTextBox.value = returnValue.toString();
}
//Cancel the postback.
return false;
}
</script>
</body>
</HTML>
=======================CalendarDialog.aspx=========================
<%@ Page language="c#" AutoEventWireup="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CalendarDialog</title>
<base target=_self>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Calendar id="Calendar1" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 32px" runat="server"
Width="241px" Height="176px"></asp:Calendar>
<INPUT style="Z-INDEX: 102; LEFT: 32px; WIDTH: 96px; POSITION: absolute; TOP: 224px; HEIGHT: 24px"
type="button" value="OK" onclick="returnToMainForm();">
<INPUT style="Z-INDEX: 103; LEFT: 176px; WIDTH: 96px; POSITION: absolute; TOP: 224px; HEIGHT: 24px"
type="button" value="Cancel" onclick="window.close();">
</form>
<script runat=server>
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DateTime dt;
try
{
dt = DateTime.Parse(Request.QueryString["selectedDate"]);
}
catch
{
dt = DateTime.Now;
}
Calendar1.SelectedDate = dt;
RegisterHiddenField("selectedDate",dt.ToShortDateString());
}
Calendar1.SelectionChanged += new EventHandler(Calendar1_SelectionChanged);
}
protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
RegisterHiddenField("selectedDate",this.Calendar1.SelectedDate.ToShortDateString());
}
</script>
<script language=javascript>
function returnToMainForm() {
window.returnValue = window.Form1.selectedDate.value;
window.close();
}
</script>
</body>
</HTML>
本文地址:http://com.8s8s.com/it/it7734.htm