RohitWagh
RohitWagh

Reputation: 2097

Not able to call User control button click event when loaded dynamically into another page

I am writing a project that load the list of products and then provide an option to delete them. ASP.NET C#

so have created a user control for this purpose.

The user control has product image, name , and has one button for delete. and this user control is loaded dynamically into a page lets say Products page.

but i have loaded an array of user control dynamically into Products page.

Below is the code for it.

            ProductInfo ib = (ProductInfo)LoadControl("ProductInfo.ascx");
            ProductName = "xyz"; 
            /**and so on*//
            spn_list.Controls.Add(ib);

and now a list is displayed with each product from database and each of them also has delete button.

but when i click on the delete button, it does not calls he event. none of them.

have also tried to use break Point but it does not reaches there. ?????

also tried This example

Button Click Event does not fire

but does not work

Upvotes: 2

Views: 1395

Answers (1)

Crab Bucket
Crab Bucket

Reputation: 6277

The user control must be loaded in the Page_Init method not the Page_Load. If it is done in Page_Load then it won't be added to ViewState so problems will occurr

Also wire the events on every page load not just the first load as is more usual.

if(!Page.PostBack) 
{     
    control.EventRaised += new EventHandler(EventResponse) 
}

is wrong - this event will disappear when the page reloads. Events wiring doesn't perisist on post back.

Upvotes: 3

Related Questions