Wednesday, October 24, 2007

Event does not fire unless control is created in memory

I stumbled across a very interesting problem in ASP.net. Say you have one Repeater and you have put a LinkButton for a cell in every row. The LinkButton has a eventHandler for its onClick method. The interesting thing is that, the click event will not be fired unless the linkbutton inside the repeater control is recreated.

It means that unless you repopulate the repeater which will create a linkButton in every row, the click event will not be evaluated. This is happening because, unless the server controls (here the linkbutton) is created in the memory, the click event cannot be tied to the control.

To test this, just populate a repeater control with linkbutton in the load event only when the page is loaded for the first time as in the following statment

if (!isPostback)
{

//Fill the repeater
}


//Write event handler to handle click event of the linkbutton

The above will cause the repeater control to appear on the page. After this, click the link button. You will see that the Click Event handler is not fired because the data has not been bound to the repeater and therefore no linkbutton exist.

No comments: