This is a migrated thread and some comments may be shown as answers.

Saving Data In UserControl When Tab Is Clicked On Dynamic RadTabStrip

2 Answers 146 Views
TabStrip
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 21 Aug 2009, 08:37 AM
Hi, I have the following scenario/code and I'm looking for a solution to this problem or a different way of doing it entirely!

I need a page with a number of tabs, each tab should display a question (Label) and an answer (RadEditor). The number of questions can vary so I need a way of doing this dynamically. Currently I have solved this problem by using a user control with the Label and RadEditor and loading this dynamically when (a) the page is loaded and !isPostBack (b) the radTab tabClick event fires.

My problem now is that if a user were to edit the text in the radEditor for Question 1 (for example) then clicks the tab for Question 2, the modified answer is lost. I need a way of either saving this answer to the database when the tabstrip is clicked or at least storing it as a session variable. This would be straight forward if only I could find a way to access the radEditor of Question 1 once the tab for Question two has been clicked!

When the tab is clicked the Page_Load method fires first then the tabClick event fires, so I have been using the following code:

When the page is first loaded (!isPostback):
ASP.Question question = new ASP.Question();  
question.LoadControl("./Question.ascx");  
question.ID = "question1";  
question.questionNumber = 1;  
question.questionTable = questionTable;  
 
RadMultiPage1.FindControl("RadPageView1").Controls.Add(question);  
Session["Current Question"] = 1; 

on PostBack:
int currentQuestion = Convert.ToInt32(Session["Current Question"]);  
Type type = sender.GetType();  
ASP.innovation_project_aspx myPage = sender as ASP.innovation_project_aspx;  
RadMultiPage rmp = myPage.FindControl("RadMultiPage1"as RadMultiPage;  
RadPageView rpv = rmp.FindControl("RadPageView" + currentQuestion) as RadPageView;  
ASP.Question aspq = rpv.FindControl("question1"as ASP.Question;  
RadEditor re = aspq.FindControl("RadEditor1"as RadEditor;  
 
I have tried various similar ways of doing this and they always fail as when debugging, the RadMultipage, rmp, is always null even though the Type, type, is an ASP.innovation_project_aspx as expected. I think I could be doing this totally wrong but my mind is now locked on this method!

Any solutions or alternate ideas would be greatly appreciated, I can try and explain further or post more code if required. Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Greg
Top achievements
Rank 1
answered on 21 Aug 2009, 11:34 AM
Ok, I've managed to get slightly further and I think I know what half the problem is but I'm still unsure how to get this working. The line

RadMultiPage rmp = myPage.FindControl("RadMultiPage1") as RadMultiPage;   
 should be
RadMultiPage rmp = myPage.RadMultiPage1;   

but I still can't access the ASP.Question object which at this point is null.

 

I think this is more a basic question regarding the flow of control/execution through the page when the tabStrip is clicked. Below is the  code for my current setup:

using System;  
using System.Collections;  
using System.Configuration;  
using System.Data;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using Telerik.Web.UI;  
 
public partial class Innovation_Project : System.Web.UI.Page  
{  
    string questionTable;  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
        RadAjaxManager AjaxManager = (RadAjaxManager)this.Master.Master.FindControl("RadAjaxManager1");  
        AjaxManager.AjaxSettings.AddAjaxSetting(RadTabStrip1, RadMultiPage1);  
        AjaxManager.AjaxSettings.AddAjaxSetting(RadTabStrip1, RadTabStrip1);  
 
        if (Request.QueryString.Count > 0)  
        {  
            database innovationDB = new database("*******""******""******""******");  
            int status = Convert.ToInt32(innovationDB.ReturnString("SELECT Status FROM Project WHERE ID ='" + Request.QueryString[0] + "' "));  
            TextBox1.Text = innovationDB.ReturnString("SELECT Name FROM Project WHERE ID ='" + Request.QueryString[0] + "' ");  
            TextBox2.Text = Request.QueryString[0];  
            DataSet dsQuestions = new DataSet();
            #region Set questionTable string  
            switch (status)  
            {  
                case 1:  
                    {  
                        questionTable = "Questions_Stage1";  
                        break;  
                    }  
                case 2:  
                    {  
                        questionTable = "Questions_Stage2";  
                        break;  
                    }  
                case 3:  
                    {  
                        questionTable = "Questions_Stage3";  
                        break;  
                    }  
                case 4:  
                    {  
                        questionTable = "Questions_Stage4";  
                        break;  
                    }  
                case 5:  
                    {  
                        questionTable = "Questions_Stage5";  
                        break;  
                    }  
                case 6:  
                    {  
                        questionTable = "Questions_Stage6";  
                        break;  
                    }  
                case 7:  
                    {  
                        questionTable = "Questions_Stage7";  
                        break;  
                    }  
                case 8:  
                    {  
                        questionTable = "Questions_Stage8";  
                        break;  
                    }  
                case 9:  
                    {  
                        questionTable = "Questions_Stage9";  
                        break;  
                    }  
                case 10:  
                    {  
                        questionTable = "Questions_Stage10";  
                        break;  
                    }  
                default:  
                    {  
                        break;  
                    }  
            }
            #endregion  
 
            if (!IsPostBack)  
            {  
                dsQuestions = innovationDB.ReturnDataSet("SELECT * FROM " + questionTable);  
                int page = 1;  
                foreach (DataRow row in dsQuestions.Tables[0].Rows)  
                {  
                    RadPageView rpv = new RadPageView();  
                    rpv.ID = "RadPageView" + page;  
 
                    RadMultiPage1.Controls.Add(rpv);  
                    page++;  
                }  
 
                ASP.Question question = new ASP.Question();  
                question.LoadControl("./Question.ascx");  
                question.ID = "question1";  
                question.questionNumber = 1;  
                question.questionTable = questionTable;  
 
                RadMultiPage1.FindControl("RadPageView1").Controls.Add(question);  
                Session["Current Question"] = 1;  
 
                /*TESTING
                int currentQuestion = Convert.ToInt32(Session["Current Question"]);
                RadMultiPage rmp = this.RadMultiPage1;
                RadPageView rpv1 = rmp.FindControl("RadPageView" + currentQuestion) as RadPageView;
                ASP.Question aspq = rpv1.FindControl("question1") as ASP.Question;
                RadEditor re = aspq.FindControl("RadEditor1") as RadEditor;
                End TESTING*/ 
 
              
           }  
            else 
            {            
                int currentQuestion = Convert.ToInt32(Session["Current Question"]);  
                ASP.innovation_project_aspx myPage = sender as ASP.innovation_project_aspx;  
                RadMultiPage rmp = myPage.RadMultiPage1;  
                RadPageView rpv = rmp.FindControl("RadPageView"+currentQuestion) as RadPageView;  
                //works to here  
 
                ASP.Question aspq = rpv.FindControl("question1"as ASP.Question;//this is null, not sure why  
                RadEditor re = aspq.FindControl("RadEditor1"as RadEditor;  
                  
                //save the content of re  
            }  
        }  
    }  
 
    protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)  
    {  
        ASP.Question question = new ASP.Question();  
        question.LoadControl("./Question.ascx");  
        question.ID = "question"+e.Tab.Index + 1;  
        question.questionNumber = e.Tab.Index + 1;  
        question.questionTable = questionTable;  
 
        RadMultiPage1.FindControl("RadPageView" + (e.Tab.Index + 1)).Controls.Add(question);  
        Session["Current Question"] = e.Tab.Index+1;  
 
    }  
 

The TESTING section commented out works fine so I know the code is ok. I think the reason it's failing is due to the page reloadnig or something similar - it seams that the question1 user control is no longer in the RadPageView1 controls but I'm not sure why. Can anyone explain this or come up with a solution? Thanks again.
    

0
Greg
Top achievements
Rank 1
answered on 26 Aug 2009, 09:13 AM
If anyone's interested, see here for the solution:

http://demos.telerik.com/aspnet-ajax/tabstrip/examples/applicationscenarios/dynamicpageview/defaultcs.aspx

I've not tried it as I found an alternate method but looks like it should do the job.
Tags
TabStrip
Asked by
Greg
Top achievements
Rank 1
Answers by
Greg
Top achievements
Rank 1
Share this question
or