Dynamic Image button Click not firing

Hi Experts

The following method I'm calling whenever I need to create a dynamic Image Button. However, my question is what's the difference between the first and second code:
protected Control addCtrlCell(string strCtrl, string strRank)
    {
        ImageButton btnImge = new ImageButton();

        btnImge.ID = "imb" + strCtrl + "_" + strRank;
        btnImge.ImageUrl = "~/tbl_" + strCtrl + ".png";
        btnImge.Click += doWhatever;
        btnImge.ToolTip = strCtrl;
        return btnImge;
    }

Open in new window

protected Control addCtrlCell(string strCtrl, string strRank)
    {
        using (ImageButton btnImge = new ImageButton())
        {
            btnImge.ID = "imb" + strCtrl + "_" + strRank;
            btnImge.ImageUrl = "~/tbl_" + strCtrl + ".png";
            btnImge.Click += doWhatever;
            btnImge.ToolTip = strCtrl;
            return btnImge;
        }
    }

Open in new window

It's really confusing me, since in the first code, the Image Button will fire the event when clicked, in the second code it doesn't. Kindly explain the difference.
Faraj1969System AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
In the first method it creates an ImageButton and then returns that button to the caller of the function. In the second function it creates an ImageButton in the using block. In the using block it initializes the control and returns that button to the caller but the last thing it does before leaving the using block is to call the Dispose method on the ImageButton. So in this case do not use a using block.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Faraj1969System AdministratorAuthor Commented:
aaahhhh, I totally forgot about that, my brain needs to be disposed.

Many thanks mate.
Fernando SotoRetiredCommented:
Not a problem Faraj1969, glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.