Wednesday, March 28, 2012

Changing dynamically a user control within an UpdatePanel

I have a page that (using ajax), will dynamically route someone through a bunch of survey questions. Each survey page is written as a user control as each page needs to have a lot of flexibility, etc to visually add/edit HTML, blah, blah, but that's not the problem. I'm having a problem with loading the question too late in the page life cycle, so our respondent will see the following...

Introduction, Q1 (where they answer), Q1 again (as their answers aren't remembered), Q2 (Q1 answers were remembered this time), Q2 (again, cause Q2 answers weren't remembered first time again)... and so on, etc.

I know what the problem is, but I'm drawing a blank on how I can execute what I want properly. I've attached the code for my controlling page below. Thank you for any advice anyone can offer!!!

<%@dotnet.itags.org. Page Language="C#" MasterPageFile="~/survey.master" Title="Surveys" %>
<%@dotnet.itags.org. Import Namespace="SurveyWebControls" %><script runat="server">
const string SurveyStart ="SurveyStart";

SurveyPageBase currentSurveyPage;

protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme ="SurveyDefault";
}

protected void Page_Load(object sender, EventArgs e)
{
lblErrorMessage.Text ="";

if (txtCurrentSurveyPage.Value =="")
LoadSurveyPage(SurveyStart);
else LoadSurveyPage(txtCurrentSurveyPage.Value); }protected void btnNext_Click(object sender, ImageClickEventArgs e)
{
if (currentSurveyPage.Validated)
LoadSurveyPage(currentSurveyPage.NextPage);
else lblErrorMessage.Text = currentSurveyPage.ErrorMessage; }private void LoadSurveyPage(string QId)
{
Survey.Controls.Clear();
currentSurveyPage = (SurveyPageBase)LoadControl(QId +".ascx");
Survey.Controls.Add(currentSurveyPage);
txtCurrentSurveyPage.Value = currentSurveyPage.QId;
btnNext.Visible = currentSurveyPage.ShowNext;
}

</script
<asp:Content ID="SurveyBody" ContentPlaceHolderID="SurveyBody" Runat="Server">
<asp:UpdatePanel ID="SurveyUpdatePanel" runat="server">
<ContentTemplate>
<asp:HiddenField runat="server" ID="txtCurrentSurveyPage" />
<asp:Label runat="server" ID="lblErrorMessage" SkinID="ErrorMessage" />
<asp:PlaceHolder ID="Survey" runat="server" />
<br /><br />
<asp:ImageButton ID="btnNext" SkinID="NextButton" runat="server" OnClick="btnNext_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Try to use the template of asp:Wizard to load?user?controls?dynamically.?Try?to?take?a?look?at?the?following?materials?for?details.
http://msdn2.microsoft.com/en-us/library/fs0za4w6(VS.80).aspx
http://aspnet.4guysfromrolla.com/articles/061406-1.aspx
http://support.microsoft.com/kb/180405
http://weblogs.asp.net/sushilasb/archive/2004/03/31/104939.aspx
http://aspalliance.com/766_Working_with_the_Wizard_Control_Using_Visual_Studio_2005

Wish the above can help you.
I had thought of that, but it would still have to be dynamic. Sometimes there are literally hundreds of pages where someone might only go through 5 of them, based on how they answer previous questions.

No comments:

Post a Comment