Wednesday, March 21, 2012

LoadControl and UpdatePanel

hi all,

I am having problems with server controls placed inside an UpdatePanel. they are placed in an acsx (web control) and I am loading them (following instructions encoded in an XML) using LoadControl. but, the inside controls are null.

I am quite sure I've got the page lifecycle messed up somehow. here's a detailed description of what I am doing:

I have got a web control named Canvas (as it is supposed to hold other controls, as well as other Canvases). it has got an UpdatePanel with a PlaceHolder in the ContentTemplate.

I have embedded one Canvas (obviously called Root) in my aspx page, which has the ScriptManager in it.

in the Page_Load event of the original aspx page (I tried Page_PreInit as well, doesn't work) I am dynamically creating other Canvases (by LoadControl) and adding it in my PlaceHolder. I have a method that takes an XML and I am calling that method. it works fine for the root.

however, after I have loaded my control, I call the XML accepting method (with a child XML node) of the newly created control. this is where it gets stuck with a null exception. so it's right after the LoadControl call.

I hope I haven't made a trivial mistake. I removed my UpdatePanel and it works just fine. so it can't be terribly wrong! I am using the January CTP.

cheers.

help! somebody? anybody?

I had the same issue and I just resolved it. You need to add an ID to your control... then it will persist on postbacks. That's all you do (and always create the control in the page_load event). Here's my code and it works like a champ and I'm using MS's AJAX panels as well.

i'm placing a unique control on the page based on the tab they are on but you could just add a single control here as well. Don't worry about the LoadData, those are just methods that load my data into the controls.

Good luck!

protected void Page_Load(object sender, EventArgs e) { LoadPageViewData(radCategoryTabStrip.SelectedIndex); }
private void LoadPageViewData(int index) {//-- clear out the controls pageDynamic.Controls.Clear();string guid = String.Empty;if (lblDetailData.Attributes["GUID"] !=null) { guid = lblDetailData.Attributes["GUID"].ToString(); }switch (index) {case 0://-- create the control CustomControls_Group_Edit oCtrlGroupItemEdit = (CustomControls_Group_Edit)LoadControl("~/CustomControls/Group/Edit.ascx");//new CustomControls_Group_Edit(); //-- wire up the event handler, this way we'll get notified when the control //-- save's it's data and we can update the tree on this page. oCtrlGroupItemEdit.GroupItemSaved +=new EventHandler(itemSaved);//-- give it an id, that way the page_load event will know to rebind it //-- to the same instance on postbacks and persist the data //-- (read an article about it and it works) oCtrlGroupItemEdit.ID ="GroupEdit1";//-- add the control to the page before we invoke the first method multiPage.PageViews[0].Controls.Add(oCtrlGroupItemEdit);//-- load the data oCtrlGroupItemEdit.LoadData(guid);break;case 1://-- not doing any custom events for this one, so just load the control CustomControls_Groupings_ProductList oCtrlProductList = (CustomControls_Groupings_ProductList)LoadControl("~/CustomControls/Groupings/ProductList.ascx");//-- add the control the page before we invoke the first method oCtrlProductList.ID ="ProductList1"; pageDynamic.Controls.Add(oCtrlProductList);//-- this must be called after it's added to the control collection (not before or it will fail) oCtrlProductList.LoadData(guid);break;case 2:break;case 3:break; }//-- make sure this control is selected pageDynamic.Selected =true;//-- either works: multiPage.PageViews[0].Selected = true; //-- update the ajax panel updatePanelCategoryInfo.Update(); }

No comments:

Post a Comment