I've placeholder control inside gridvew which has dynamically added image controls during RowDataBound event handle. The controls are added fine and are visible when the page is rendered.
The problem is when the page is postback the image controls added inside the placeholder is not maintained. Somehow placeholder is not maintaining its viewstate. Here is code which I use. I could have added those images again in the RowCreated event handle
but the only problem is this event handle does not have DataItem object during postback. Please can someone suggest alternative solution.
dynamic controls would need to be created on every request.
E.g if you create them initially in a postback event such as button click, you'd need to recreate them on next Page loading so that they'd exist in the Controls collection. ViewState would work when you'd have dynamic control instantiation in order.
You should be using a DataGrid, DataList, Repeater or such. But if you really have to add the controls "manually", then you have to add them on every page request, and in the exact same order. If you do this, then the controls will load their ViewState as
well as the values posted from the form. This will make the new values available in the Text or Checked properties, and it will allow the controls to raise events when these values change.
check the thread about get dynamic controls and values on postback,
http://forums.asp.net/t/1609262.aspx
and also check this url about get dynamic controls and values on postback,
bhav27
Member
79 Points
107 Posts
Placeholder viewstate not maintained inside gridview
Aug 20, 2011 09:24 AM|LINK
Hi
I've placeholder control inside gridvew which has dynamically added image controls during RowDataBound event handle. The controls are added fine and are visible when the page is rendered.
The problem is when the page is postback the image controls added inside the placeholder is not maintained. Somehow placeholder is not maintaining its viewstate. Here is code which I use. I could have added those images again in the RowCreated event handle but the only problem is this event handle does not have DataItem object during postback. Please can someone suggest alternative solution.
protected void grdMenuItems_RowDataBound(object sender, GridViewRowEventArgs e) { CatalogMenu menuItem = e.Row.DataItem as CatalogMenu; if (menuItem != null && e.Row.RowType == DataControlRowType.DataRow) { PlaceHolder imgPlaceHolder = e.Row.Cells[1].FindControl("imgPlaceHolder") as PlaceHolder; RenderTasteImages(menuItem, imgPlaceHolder); } } private static void RenderTasteImages(CatalogMenu menuItem, PlaceHolder imgPlaceHolder) { if (imgPlaceHolder != null) { if (menuItem.VegDish) { Image imgVeg = new Image { ImageUrl = "~/images/veg.gif" }; imgVeg.ID = "imgVeg"; imgVeg.Height = 24; imgVeg.Width = 24; imgPlaceHolder.Controls.Add(imgVeg); } if (menuItem.Taste.TasteImage != null) { Image imgTaste = new Image { ImageUrl = menuItem.Taste.TasteImage }; imgTaste.Height = 24; imgTaste.Width = 24; imgPlaceHolder.Controls.Add(imgTaste); } } }gopalanmani
Star
7826 Points
1320 Posts
Re: Placeholder viewstate not maintained inside gridview
Aug 20, 2011 09:35 AM|LINK
Hi,
dynamic controls would need to be created on every request.
E.g if you create them initially in a postback event such as button click, you'd need to recreate them on next Page loading so that they'd exist in the Controls collection. ViewState would work when you'd have dynamic control instantiation in order.
You should be using a DataGrid, DataList, Repeater or such. But if you really have to add the controls "manually", then you have to add them on every page request, and in the exact same order. If you do this, then the controls will load their ViewState as well as the values posted from the form. This will make the new values available in the Text or Checked properties, and it will allow the controls to raise events when these values change.
check the thread about get dynamic controls and values on postback,
http://forums.asp.net/t/1609262.aspx
and also check this url about get dynamic controls and values on postback,
http://aspadvice.com/blogs/joteke/archive/2006/01/06/14568.aspx
Gopalan Mani
My Tech blog