我得到了这个扩展tablerow的代码,使它们可以点击。
namespace ClickableTableRow
{
public class ClickableTableRow : TableRow, IPostBackEventHandler
{
public ClickableTableRow()
: base()
{ }
private EventHandler _click;
public event EventHandler Click
{
add { _click += value; }
remove { _click -= value; }
}
protected virtual void FireClickEvent()
{
if (_click != null)
_click(this, new EventArgs());
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.ClientScript.GetPostBackEventReference(this, String.Empty));
base.RenderAttributes(writer);
}
public void RaisePostBackEvent(string eventArgument)
{
FireClickEvent();
}
}
如果我手动创建表,上面的代码运行良好,但是当我在后面的代码中创建表时,它似乎是触发回发但没有返回resuts。我可能做错了什么事。我的代码隐藏看起来像:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void row1_Click(object sender, EventArgs e)
{
Messagebox.Text = "message";
}
ClickableTableRow.ClickableTableRow row1;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Table Table1 = new Table();
TableCell cell1 = new TableCell();
row1= new ClickableTableRow.ClickableTableRow();
row1.Attributes.Add("onmouseover", "this.style.cursor='hand'");
cell1 = new TableCell();
cell1.Text = "llllll";
row1.Cells.Add(cell1);
Table1.Rows.Add(row1);
row1.Click += new EventHandler(row1_Click);
this.pnl2.Controls.Add(Table1);
}
}
答案 0 :(得分:0)
尝试将此代码移至Init或PreInit。这是应该添加动态控件的生命周期中的典型部分。