开发者

Event handler reference from utility class

I have this method that I'm currently putting in each page I make, I know there should be a good way to move it to a single place for ease of maintenance and simplicity. I'm just not sure how I should handle the event handler. The event handler needs to be on each page, so how would I pass in a reference to the page properly so I can reference the event handler?

private void InsertLinkButton(string text, string id, UpdatePanel updateSummary)
    {
        LinkButton link = new LinkButton();
        link.Text = text;
        l开发者_运维百科ink.Click += new EventHandler(link_Click);   <------
        link.CausesValidation = false;
        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = link.ID = "link" + id;
        trigger.EventName = "Click";
        Utils.Tag(link, placeHolderSummary);
        updateSummary.Triggers.Add(trigger);
    }


Why not just pass the event handler into your method as an argument?

private void InsertLinkButton(string text, string id, UpdatePanel updateSummary,
                              EventHandler clickHandler)
{
    LinkButton link = new LinkButton();
    link.Text = text;
    link.Click += clickhandler;
    ...
}

Call it with:

InsertLinkButton("text", "id", updatePanel, link_Click);

(Assuming link_Click is your method name.)


I am not very in to ASP.NET, but I have done something relevant at Windows forms. I had a FormBase in which I had all the common functionality and from the derived I did whatever I wanted. I hooked up events and other things like authentication.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜