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

LoadOnDemand pass variable

4 Answers 83 Views
TabStrip
This is a migrated thread and some comments may be shown as answers.
Atlas
Top achievements
Rank 1
Atlas asked on 20 Aug 2009, 10:12 PM
I am using the application scenario Load On Demand example for the tab strip, and I was wondering how I could pass a variable to the user control. I will have a numeric value (UnitNumber) between 1 and 4, for each number I will need to create a tab. So if the number is three, I need to create 3 tabs. I am going to use the same ascx page, and I want it to know which tab it is: 1, 2, or 3 for example.

I created unit.ascx, and it contains the following:
    private string _unitNumber;  
      
    public string UnitNumber  
    {  
        get{return _unitNumber; }  
        set{ _unitNumber = value; }  
    }  
 
the aspx page looks like your example:
        protected void RadMultiPage1_PageViewCreated(object sender, RadMultiPageEventArgs e)  
        {  
            string userControlName = e.PageView.ID.Replace(" ", "") + ".ascx";  
 
            Control userControl = Page.LoadControl(userControlName);  
 
            userControl.ID = e.PageView.ID + "_userControl";  
            e.PageView.Controls.Add(userControl);  
        }  
 
so, in the code above, how do I add something like: userControl.UnitNumber = 2

4 Answers, 1 is accepted

Sort by
0
Greg
Top achievements
Rank 1
answered on 21 Aug 2009, 08:05 AM
Funnily enough I'm working on something very similar and have found a way around that part of the problem (I'm now at the next hurdle and I'm about to create a thread for that!), it may not be the best way though. I used this article:

http://msdn.microsoft.com/en-us/library/c0az2h86.aspx

And got it working quite easily. If you are still having problems post again and I'll try and stick some code up.

0
kachy
Top achievements
Rank 1
answered on 26 Aug 2009, 07:40 PM
Greg,

Can you please the code for the same, as I want to do the same thing?

Thanks.
0
Greg
Top achievements
Rank 1
answered on 27 Aug 2009, 07:44 AM

Here is the code, I've edited out all the irrelevant stuff so it probablly won't work, but you should be able to see how to do it.

Project.aspx.cs:

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 (!IsPostBack)  
        {  
            ASP.Question question = new ASP.Question();  
            question.LoadControl("./Question.ascx");  
            question.ID = "question1";  
            question.questionNumber = 1;  
 
            RadMultiPage1.FindControl("RadPageView1").Controls.Add(question);  
        }  
    }  
 
    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);  
    }  
 
}  
 

Project.aspx:
<%@ Reference Control="Question.ascx" %> 
must contain this line with the name of your user control file.

Question.aspx.cs:
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 System.Windows.Forms;  
 
public partial class Innovation_Question : System.Web.UI.UserControl  
{  
    public int questionNumber;  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
        //do stuff with questionNumber  
    }  
 

Question.aspx:

<%@ Control ClassName="Question" Language="C#" AutoEventWireup="true" CodeFile="Question.ascx.cs" Inherits="Innovation_Question" %> 
must contain the ClassName="Question" assignment seen above.


I have two warnings:
1. This is quite possibly not the best way of doing this but it does seem to work.
2. I had major problems trying to access the variables of different user controls so I ended up not using this method. See here for a description of and solution to that problem (I never tried the solution so don't know if it definately works!).

Hope that helps!
0
kachy
Top achievements
Rank 1
answered on 27 Aug 2009, 12:35 PM
Thanks Greg, I will give this a try.
Tags
TabStrip
Asked by
Atlas
Top achievements
Rank 1
Answers by
Greg
Top achievements
Rank 1
kachy
Top achievements
Rank 1
Share this question
or