将EventHandler添加到LinkBut​​tons

时间:2014-08-08 14:20:14

标签: c# .net event-handling

页面中有2个链接按钮,似乎添加了事件处理程序,但日志没有被触发。

    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (var btn in Master.FindControl("ContentPlaceHolder1").Controls.OfType<LinkButton>())
        {
            btn.Click += new EventHandler(log);
        }
    }

    void log(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        //somecode
    }

1 个答案:

答案 0 :(得分:0)

答案是;

protected void Page_Load(object sender, EventArgs e)
    {
        foreach (var btn in Master.FindControl("ContentPlaceHolder1").Controls.OfType<LinkButton>())
        {
            btn.Click += log;
        }
    }

    void log(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        //somecode
    }