Saturday, March 24, 2012

AJAX and user controls

Hi

First of all, I use AJAX 1.0. I have bumped into following problem. I have aspx page (Default.aspx) with ScriptManager on it. It has header, left menu and content layout. On each of those I have UpdatePanel in which I have PlaceHolder. I use those placeholders to load uc controls.

What I want to acheive is to load default uc-s in every ph on Page Load which works ok. Menu ph will always have default uc loaded. When I click on menu item (which is in menu control on menu uc in menu ph), I want to change header uc and content uc -> load new uc-s in their ph-s. This also works ok but I doubt my method in acheiving this.

My page load code:

1using System;2using System.Data;3using System.Configuration;4using System.Web;5using System.Web.Security;6using System.Web.UI;7using System.Web.UI.WebControls;8using System.Web.UI.WebControls.WebParts;9using System.Web.UI.HtmlControls;1011public partialclass _Default : System.Web.UI.Page12{13#region UC ViewState14public string currentUCM15 {16get17 {18if (ViewState["currentUCM"] !=null)return ViewState["currentUCM"].ToString();19else return null;20 }21set { ViewState["currentUCM"] =value; }22 }23public string currentUCH24 {25get26 {27if (ViewState["currentUCH"] !=null)return ViewState["currentUCH"].ToString();28else return null;29 }30set { ViewState["currentUCH"] =value; }31 }32public string currentUCC33 {34get35 {36if (ViewState["currentUCC"] !=null)return ViewState["currentUCC"].ToString();37else return null;38 }39set { ViewState["currentUCC"] =value; }40 }41#endregion4243 protected void Page_Load(object sender, EventArgs e)44 {45// menu46if (currentUCM !=null)47 {48 phMenu.Controls.Add(LoadControl(currentUCM));49 }50else51 {52 currentUCM ="~/uc/uc_m1.ascx";53 phMenu.Controls.Add(LoadControl(currentUCM));54 }5556// head57if (currentUCH !=null)58 {59 phHead.Controls.Add(LoadControl(currentUCH));60 }61else62 {63 currentUCH ="~/uc/uc_h1.ascx";64 phHead.Controls.Add(LoadControl(currentUCH));65 }6667// content68if (currentUCC !=null)69 {70 phMainContent.Controls.Add(LoadControl(currentUCC));71 }72else73 {74 currentUCC ="~/uc/uc_c1.ascx";75 phMainContent.Controls.Add(LoadControl(currentUCC));76 }77 }78}
Code in my uc_m1.ascx (menu) is as follows:  
1using System;2using System.Data;3using System.Configuration;4using System.Collections;5using System.Web;6using System.Web.Security;7using System.Web.UI;8using System.Web.UI.WebControls;9using System.Web.UI.WebControls.WebParts;10using System.Web.UI.HtmlControls;1112public partialclass uc_uc_m1 : System.Web.UI.UserControl13{14protected void Page_Load(object sender, EventArgs e)15 {1617 }1819protected void mnuMain_MenuItemClick(object sender, MenuEventArgs e)20 {21string currentUCM ="~/uc/uc_m" + e.Item.Value +".ascx";22string currentUCH ="~/uc/uc_h" + e.Item.Value +".ascx";23string currentUCC ="~/uc/uc_c" + e.Item.Value +".ascx";2425// setting ViewState on page26 Page.GetType().GetProperty("currentUCM").SetValue(this.Page, currentUCM,null);27 Page.GetType().GetProperty("currentUCH").SetValue(this.Page, currentUCH,null);28 Page.GetType().GetProperty("currentUCC").SetValue(this.Page, currentUCC,null);2930 PlaceHolder phH = (PlaceHolder)this.Page.FindControl("phHead");31 phH.Controls.Clear();32 phH.Controls.Add(LoadControl(currentUCH));3334 PlaceHolder phC = (PlaceHolder)this.Page.FindControl("phMainContent");35 phC.Controls.Clear();36 phC.Controls.Add(LoadControl(currentUCC));3738// debug39 lblClicked.Text = (Convert.ToInt32(lblClicked.Text) + 1).ToString();40 }41}42
...here I set what gets loaded and override Page Load-s loaded controls, and then use reflection to set ViewState on Page through public property. I tried
to use ViewState directly, but didn't work. Can I update ViewState somehow?
Anyway, when I do this, I get "Failed to load viewstate...control tree doesn't match...".
Is there a way to go arround this? Another solution? Because I would like to use those similar to windows forms, just open form I need. And those uc I load
will have forms on them.
Thanx
If I remove reflection, leave ViewState as is and simply put another update panel in each UC, can I then refresh page only inside UC's update panel and refresh *global update panel based on condition?
Ok, I solved ViewState problem by simply using Session instead. It works better but not correct yet. Also there might be bug in AJAX, I have script error on page saying 0.cells is not object. Problem that remains is how to postback on uc control. My first postback doesnt work, only next ones do. I think it is because of event registration happening too late.

No comments:

Post a Comment