Sunday, June 13, 2010

Problem With UpdatePanel and FormView

I got to this problem the other day which I thought is good to share it.

In one of my ASP.NET application, I have a "Form View" which is bind to a database. For some reason, I needed to use Update Panel (Ajax) to implement click event without server post back. So I used it and it works fine but the problem was when I submit my Form View to save information, the controls that are inside of "Update Panel" would not updated on SQL database even though the update method called. 

After a little bit of research, I found out that is bug with Update Panel and Form View but there is a work around. to fix it. All you need to do is to use "Updating" event of Form View and set the value of controls that are inside of "Update Panel" by using e.InputParameter[0] (Take a look at sample below. notice that we cast e.InputParameter[0] as the object of entity/class that bound to form view   and that solve the issue. here is some sample code :

        protected void odsMatterDetail_Updating(object sender, ObjectDataSourceMethodEventArgs e)
            //update items that are inside Update panel AJax. because of Update Panel items not bound back to form
            YesNoControl YesNoControlHST = (YesNoControl)MatterDetailFormview.FindControl("YesNoControlHST");
            YesNoControl YesNoControlIsCanadian = (YesNoControl)MatterDetailFormview.FindControl("YesNoControlIsCanadian");
            YesNoControl YesNoControlIsCndLitigation = (YesNoControl)MatterDetailFormview.FindControl("YesNoControlIsCndLitigation");
            YesNoControl YesNoControlHasLitigationCommence = (YesNoControl)MatterDetailFormview.FindControl("YesNoControlHasLitigationCommence");
            YesNoControl YesNoControlHasRealPropertyInCnd = (YesNoControl)MatterDetailFormview.FindControl("YesNoControlHasRealPropertyInCnd");

            LookupListBoxControl LookupListBoxOfficeProvince = (LookupListBoxControl)MatterDetailFormview.FindControl("LookupListBoxOfficeProvince");
            LookupListBoxControl LookupListBoxLitigationProvince = 
            LookupListBoxControl LookupListBoxRealPropProvince = 

            (e.InputParameters[0] as MatterDetail).IsGSTApplied = YesNoControlHST.Value;
            (e.InputParameters[0] as MatterDetail).IsCanadianClient = YesNoControlIsCanadian.Value;
            (e.InputParameters[0] as MatterDetail).ClientOfficeProvinceCode = LookupListBoxOfficeProvince.Value;
            (e.InputParameters[0] as MatterDetail).IsCanadianLitigationMatter = YesNoControlIsCndLitigation.Value;
            (e.InputParameters[0] as MatterDetail).HasLitigationCommenced = YesNoControlHasLitigationCommence.Value;
            (e.InputParameters[0] as MatterDetail).LitigationProvinceCode = LookupListBoxLitigationProvince.Value;

            (e.InputParameters[0] as MatterDetail).HasRealPropertyInCanada = YesNoControlHasRealPropertyInCnd.Value;
            (e.InputParameters[0] as MatterDetail).RealPropertyProvinceCode = LookupListBoxRealPropProvince.Value;

            (e.InputParameters[0] as MatterDetail).ModifBy = Security.CurrentUserID;

Happy Coding