在DataGrid中嵌套Calendar控件
当在一个表示时间的文本框中输入时间,可利用模版项中潜入Calendar控件实现。
首先建立DataGridCalendar.aspx
<%@ Page language="c#" Codebehind="DataGridCalendar.aspx.cs" AutoEventWireup="false" Inherits="aspnet.sqlConn.DataGridCalendar" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridCalendar</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<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>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 240px; POSITION: absolute; TOP: 128px"
runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="StringColumn" HeaderText="数据列"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="时间列">
<ItemTemplate>
<asp:TextBox id="TextBox2" runat="server" Text='<%# DateTime.Parse(DataBinder.Eval(Container, "DataItem.DateColumn").ToString()).ToShortDateString() %>'>
</asp:TextBox>
<asp:Button id="Button1" runat="server" Text="编辑值"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>
在后台的代码:
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;
namespace aspnet.sqlConn
{
/// <summary>
/// DataGridCalendar 的摘要说明。
/// </summary>
public class DataGridCalendar : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected DataTable dt;
private void Page_Load(object sender, System.EventArgs e)
{
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();
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
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 + "');");
}
}
}
}
在建立一个页面CalendarDialog.aspx
<%@ Page language="c#" Codebehind="CalendarDialog.aspx.cs" AutoEventWireup="false" Inherits="aspnet.sqlConn.CalendarDialog" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CalendarDialog</title>
<base target="_self">
<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 returnToMainForm() {
window.returnValue = window.Form1.selectedDate.value;
window.close();
}
</script>
</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="确定" onclick="returnToMainForm();">
<INPUT style="Z-INDEX: 103; LEFT: 176px; WIDTH: 96px; POSITION: absolute; TOP: 224px; HEIGHT: 24px"
type="button" value="取消" onclick="window.close();">
</form>
</body>
</HTML>
后台代码:
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;
namespace aspnet.sqlConn
{
/// <summary>
/// CalendarDialog 的摘要说明。
/// </summary>
public class CalendarDialog : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Calendar Calendar1;
private 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);
}
Web 窗体设计器生成的代码
private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
RegisterHiddenField("selectedDate",this.Calendar1.SelectedDate.ToShortDateString());
}
}
}
首先建立DataGridCalendar.aspx
在后台的代码:
在建立一个页面CalendarDialog.aspx
后台代码:
浙公网安备 33010602011771号