How to implement popup Calendar dialog box with DataGrid
2004-12-13 08:56
495 查看
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>
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 + "');");
}
}
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;
}
=======================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>
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 + "');");
}
}
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;
}
相关文章推荐
- How to implement popup Calendar dialog box with DataGrid
- How to create a resizeable dialog box with scroll bars in Visual C++
- How to Customize Common Open File Dialog Box Places Bar in Windows
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-2)
- How to implement the similar mechanism with Java final in c++?
- how to use adb and gdbserver with VirtualBox - KVM (qemu)
- How to cover an IE windowed control (Select Box, ActiveX Object, etc.) with a DHTML layer.
- How to drag a dialog box derived from DHtmlDialog ?
- How to load a dll resource dialog box at an application Dialog box (怎么在一个应用程序对话框动态加载DLL里的资源对话框)
- how to show a dialog box that prompts you to either "Open it" or "Save it to disk"again?
- [XAF] How to represent an enumeration property via a drop-down box with check boxes
- how to create a window ot dialog with fixed size?
- How to implement COMET with PHP
- How to play '.rm' files with RhythmBox in Ubutu?
- How to install OS X Mountain Lion in Virtualbox with iAtkos
- How to make our assemblies appeared in Add Reference dialog box
- How to make a combo box with fulltext search autocomplete support?
- How to integrate the latest lispbox with clisp.
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-1)
- How to implement multiple constructor with different parameters in Scala