1

我已经使用扩展类实现了可点击的表格单元格。但是,当我在主类中调用事件处理程序/onclick 方法时,事件处理程序不起作用。

扩展的可点击表格单元类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Member
{
    public class TableCellClick: TableCell, IPostBackEventHandler, INamingContainer
    {
        private static readonly object click_event = new object();

        // public handles for adding and removing functions to be called on the click event
        public event EventHandler Click
        {
            add
            {
                Events.AddHandler(click_event, value);
            }
            remove
            {
                Events.RemoveHandler(click_event, value);
            }
        }

        // define parent function that will be called when the container is clicked
        protected void OnClick(EventArgs e)
        {
            EventHandler h = Events[click_event] as EventHandler;
            if (h != null)
            {
                h(this, e);
            }
        }

        // specify the "post back event reference" or id of the click event
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, 
                                Page.ClientScript.GetPostBackEventReference(this, "custom_click"));
        }

        // link the custom click id to the click function
        void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            if(eventArgument == "custom_click")
            {
                OnClick(EventArgs.Empty);
            }
        }
    }
}

我的 Main 类中的方法:

        memPanel.Visible = true;

        //Set a table width.
        memTable.Width = Unit.Percentage(40.00);
        //Create a new row for adding a table heading.
        TableRow tableHeading = new TableRow();

        //Create and add the cells that contain the msno column heading text.
        TableHeaderCell msnoHeading = new TableHeaderCell();
        msnoHeading.Text = "M'Ship No.";
        msnoHeading.HorizontalAlign = HorizontalAlign.Left;
        tableHeading.Cells.Add(msnoHeading);

        //Create and add the cells that contain the Name column heading text.
        TableHeaderCell nameHeading = new TableHeaderCell();
        nameHeading.Text = "Name";
        nameHeading.HorizontalAlign = HorizontalAlign.Left;
        tableHeading.Cells.Add(nameHeading);

        memTable.Rows.Add(tableHeading);

        while (reader.Read())
        {
            TableRow detailsRow = new TableRow();
            TableCellClick msnoCell = new TableCellClick();
            msnoCell.Text = reader["MSNo"].ToString();
            msnoCell.Click += cellClick;
            detailsRow.Cells.Add(msnoCell);

            TableCellClick nameCell = new TableCellClick();
            nameCell.Text = reader["fName"].ToString();
            nameCell.Click += cellClick;

            memTable.Rows.Add(detailsRow);

        }

点击方法:

protected void cellClick(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("cell click");
    memPanel.Visible = false;
    basicInfo.Visible = true;
}

有谁知道哪里出错了?

4

2 回答 2

0

请试试这个::

msnoCell.Click += new System.EventHandler(cellClick);
于 2012-08-21T08:52:18.443 回答
0

之所以没有调用它是因为每次单击按钮时的回发方法。
我的单元格不是在 pageload 方法中创建的,这就是为什么单击单元格按钮时没有调用它的原因。

于 2012-08-28T06:31:38.200 回答