拡張クラスを使用してクリック可能なテーブルセルを実装しました。ただし、メインクラスでイベントハンドラー/ 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);
}
}
}
}
私のメインクラスのメソッド:
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);
}
onclickメソッド:
protected void cellClick(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("cell click");
memPanel.Visible = false;
basicInfo.Visible = true;
}
どこがうまくいかなかったのか誰か知っていますか?