Recently, I got an error for Ninject.Web that it does not work properly with dynamic ASP.NET data. The problem is that when postback (for example, when inserting, deleting, editing an entry), the table is no longer displayed.
Some debugging showed that the problem was caused by the IHttpModule, which recursively iterates through all the controls on the page after its initialization. As soon as this module accesses the Controls get accessor FormView or GridView element, a problem arises. If this type of control is skipped, everything is in order. The following code shows the module:
public class NinjectHttpModule : DisposableObject, IHttpModule { private HttpApplication httpApplication; public void Init(HttpApplication context) { this.httpApplication = context; this.httpApplication.PreRequestHandlerExecute += this.OnPreRequestHandlerExecute; } private static void InjectUserControls(Control parent) { if (parent == null) { return; } foreach (Control control in parent.Controls) { if (control is UserControl) {
If this code is modified so that iteration through the child DataBoundControls controls is delayed in the DataBound event, everything is fine. It is displayed by the following code fragment:
private static void InjectUserControls(Control parent, bool skipDataBoundControls) { if (parent == null) { return; } if (skipDataBoundControls) { var dataBoundControl = parent as DataBoundControl; if (dataBoundControl != null) { dataBoundControl.DataBound += InjectDataBoundControl; return; } } foreach (Control control in parent.Controls) { if (control is UserControl) { KernelContainer.Inject(control); } InjectUserControls(control, skipDataBoundControls); } } private static void InjectDataBoundControl(object sender, EventArgs e) { var dataBoundControl = sender as DataBoundControl; if (dataBoundControl != null) { dataBoundControl.DataBound -= InjectDataBoundControl; InjectUserControls(dataBoundControl, false); } }
Since I am completely unfamiliar with System.Web.DynamicData, I would like to learn some things in order to better understand how to fix this error:
- Why is this problem occurring? I mean, this is easy access to access the Controls property.
- What side effects can the above changes have?
- Is there enough time to enter the controls after the data event?
- Do you think this is a valid bug fix for this problem?
source share