Monday, 2 July 2007

Page_Render and Custom composite web controls

I created web custom composite controls which contain CheckBoxes.

In order to make it display after PostBack, its building-function has to be in Page_Init() or Page_Load() before calling LoadViewState of the Page.

As the custom control has a checkbox and it needs to be in the same condition before the postback (for example, if the checkbox is ticked, then after postback, it should be ticked too), I was putting the code for the checkbox selection in Page_PreRender().

As the PreRender is called last time before the display, I thought it would refill the checkboxes correctly. However, the checkboxes which were checked before the postback were unchecked after the postback.

I realised later it was because the PreRender or Render is called after LoadViewState of the Page and thus it cannot find the custom controls in the ViewState of that Page.

Thus, I changed it back into Page_Load and now it works properly.

Another thing is, when you want your custom controls to be in ViewState, do not put its built code into “if (!IsPostBack){ }”.

Learn another lesson again J

No comments: