我正在尝试在.Net v4中创建自定义日历用户控件。对于这个控件,我已经制作了一个具有onclick事件的自定义ASP表格单元格。以下课程:
public class ClickableTableCell : System.Web.UI.WebControls.TableCell, IPostBackEventHandler
{
private static readonly object TableCellClicked = new object();
public event EventHandler Click
{
add
{
base.Events.AddHandler(TableCellClicked, value);
}
remove
{
base.Events.RemoveHandler(TableCellClicked, value);
}
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
string argument = null;
if (this.ID != null)
{
argument = this.ID.ToString().Replace("/", "-");
}
Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, argument));
base.RenderBeginTag(writer);
}
public void RaisePostBackEvent(string eventArgument)
{
OnClick(new EventArgs());
}
protected void OnClick(EventArgs e)
{
EventHandler handler = (EventHandler)base.Events[TableCellClicked];
if (handler != null)
{
handler(this, e);
}
}
}
我的用户控件中有一个方法,它使用我的自定义表格单元格创建此表格。在用户控件的页面加载中触发此方法,以便始终填充表。始终将相同的ID分配给可单击的表格单元格,但是我设置的单击事件永远不会被触发。好吧,当我在ASCX文件中声明可点击的表格单元格时会被触发,如果我以编程方式添加它就不起作用,所以我认为我的表格单元格类没有任何问题。
protected void DrawMonth(DateTime monthToDraw, CultureInfo calCulture)
{
try
{
tblCal.Rows.Clear();
System.Globalization.Calendar calToDraw = calCulture.Calendar;
DayOfWeek firstDayOfWeek = calCulture.DateTimeFormat.FirstDayOfWeek;
//Add in day headings
TableHeaderRow dayHeadings = new TableHeaderRow();
int dayToStartFrom = (int)firstDayOfWeek;
for (int iDay = 0; iDay < 7; iDay++)
{
TableHeaderCell dayCell = new TableHeaderCell();
dayCell.ID = "tcDayHeader" + iDay;
dayCell.Text = calCulture.DateTimeFormat.DayNames[dayToStartFrom];
dayCell.Width = new Unit(14.28, UnitType.Percentage);
dayHeadings.Cells.Add(dayCell);
dayToStartFrom++;
if (dayToStartFrom > 6)
{
dayToStartFrom = 0;
}
}
tblCal.Rows.Add(dayHeadings);
//Add in dates
int startOffset = (int)calToDraw.GetDayOfWeek(new DateTime(monthToDraw.Year, monthToDraw.Month, 1).AddDays(-1));
int endOffset = (int)calToDraw.GetDayOfWeek(new DateTime(monthToDraw.Year, monthToDraw.Month, calToDraw.GetDaysInMonth(monthToDraw.Year, monthToDraw.Month)));
if (endOffset > 0)
{
endOffset = 7 - (int)calToDraw.GetDayOfWeek(new DateTime(monthToDraw.Year, monthToDraw.Month, calToDraw.GetDaysInMonth(monthToDraw.Year, monthToDraw.Month)));
}
int dateOn = 1;
int numOfWeeks = Helpers.DateHelper.GetNumOfWeeksInMonth(monthToDraw, calToDraw);
DateTime cellDate;
for (int iNumWeek = 0; iNumWeek <= numOfWeeks - 1; iNumWeek++)
{
TableRow weekRow = new TableRow();
weekRow.ID = "rowWeek" + iNumWeek.ToString();
for (int iNumDayInWeek = 0; iNumDayInWeek <= 6; iNumDayInWeek++)
{
ClickableTableCell dayCell = new ClickableTableCell();
if (iNumWeek == 0)
{
if (iNumDayInWeek < startOffset)
{
dayCell.CssClass = "OutOfBounds";
}
else
{
dayCell.Text = dateOn.ToString();
dateOn++;
}
}
else if (iNumWeek == numOfWeeks - 1)
{
if (iNumDayInWeek > 6 - endOffset)
{
dayCell.CssClass = "OutOfBounds";
}
else
{
dayCell.Text = dateOn.ToString();
dateOn++;
}
}
else
{
dayCell.Text = dateOn.ToString();
dateOn++;
}
dayCell.Width = new Unit(14.28, UnitType.Percentage);
if (monthToDraw.Year == DateTime.Now.Year && monthToDraw.Month == DateTime.Now.Month && (dateOn - 1) == DateTime.Now.Day)
{
dayCell.CssClass = "Today";
}
// Get calendar data
if (dayCell.CssClass != "OutOfBounds")
{
cellDate = monthToDraw.AddDays((monthToDraw.Day * -1) - 1 + dateOn).Date;
dayCell.ID = cellDate.ToString().Replace(' ', '-');
}
// add click event to the TableCell
dayCell.Click += new EventHandler(this.DayClicked);
dayCell.Attributes.Add("onmouseover", "this.style.cursor='pointer'");
// add cell to collection
weekRow.Cells.Add(dayCell);
}
tblCal.Rows.Add(weekRow);
}
lblCurrentMonth.Text = calCulture.DateTimeFormat.MonthNames[monthToDraw.Month - 1] + " " + monthToDraw.Year.ToString();
tblCal.CssClass = "AppointCal";
}
catch (Exception _exception)
{
throw new Exception(_exception.ToString());
}
}
有趣的是,如果我将此代码放在页面加载中,它将捕获点击。
if (Page.IsPostBack)
{
//It is a postback so check if it was by div click
string target = Request["__EVENTTARGET"];
if (target.Contains("ctl00$cphContent$amaAppointmentsCal$"))
{
//ClickableTableCell clickedCell = (ClickableTableCell)sender;
string id = Request["__EVENTARGUMENT"];
DrawDay(Convert.ToDateTime(id), new CultureInfo("en-GB"));
}
}
谁能看到我哪里出错了?任何帮助将不胜感激。
答案 0 :(得分:0)
表格和客户表格单元格的html输出是什么样的?