2011-11-21 83 views
2

我正在嘗試在.Net v4中創建自定義日曆用戶控件。對於這個控件,我做了一個自定義的ASP表格單元格,它有一個onclick事件。 Class下:在編程添加單元格時,單擊方法時不會觸發自定義表格單元格

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

請不要**使用'asp'標籤'asp.net'問題。 – Polynomial

+0

我的錯誤,對不起。 – Wizardskills

回答

0

表格和客戶表單元格的html輸出是什麼樣的?

+0

這是一個典型的單元格,它看起來像HTML源代碼。 – Wizardskills

相關問題