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

Rotator w/ dynamic controls cant get control data

1 Answer 46 Views
Rotator
This is a migrated thread and some comments may be shown as answers.
tmorford
Top achievements
Rank 1
tmorford asked on 05 Aug 2010, 08:31 PM
I made a survey tool with a rad rotator for an Ipad website, I am filling the rotator with RadioButtonlists and RadRating item dynamicly and the last part I add a button to say save survey. I loop though all the controls and I can not find any of the dynamic controls I even tried to use page.findcontrol and I can not find them, can anyone help out with this?

thanks!

Here is my code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using OSweb.Admin.DataTypes;
  
namespace OSweb.Controls
{
    public partial class DisplayQuestions : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RadRotator1.ItemTemplate = new RadRotatorTemplate();
                RadRotator1.DataSource = CreateRotatorData(Convert.ToInt32(Session["DealerId"].ToString()), Convert.ToInt32(Session["GroupId"].ToString()));
                RadRotator1.DataBind();
            }
            else
            {
                Button bt = (Button)Page.FindControl("ButtonSubmitSurvey");
                LabelTest.Text = bt.Text;
            }
        }
  
        private List<Questions> CreateRotatorData(Int32 DealerId, int GroupId)
        {
            List<Questions> lq = new List<Questions>();
            using (OSWEB_GenericRemittanceEntities OSWEBe = new OSWEB_GenericRemittanceEntities())
            {
                var qg = (from l in OSWEBe.QuestionGroups where (l.GroupID == GroupId) && (l.DealerId == DealerId) select l.GroupID).FirstOrDefault();
                if (qg != null)
                {
                    var q = (from l in OSWEBe.GroupOfQuestions where (l.GroupID == GroupId) select l);
                    foreach (GroupOfQuestion goq in q)
                    {
                        Questions qs = new Questions();
                        qs.QuestionText = goq.Question.QuestionText;
                        qs.QuestionType = goq.Question.QuestionType;
                        qs.QuestionId = Convert.ToInt32(goq.QuestionID);
                        lq.Add(qs);
                    }
                    Questions qsEnd = new Questions();
                    qsEnd.QuestionType = "End";
                    lq.Add(qsEnd);
                }
            }
            return lq;
        }
          
    }
  
    public class RadRotatorTemplate : ITemplate
    {
  
        public RadRotatorTemplate() { }
  
        public void InstantiateIn(Control container)
        {
            HtmlGenericControl wrapper = new HtmlGenericControl("div");
            wrapper.DataBinding += new EventHandler(wrapper_DataBinding);
            container.Controls.Add(wrapper);
        }
  
        void wrapper_DataBinding(object sender, EventArgs e)
        {
            HtmlGenericControl wrapper = sender as HtmlGenericControl;
            RadRotatorItem item = wrapper.NamingContainer as RadRotatorItem;
            wrapper.InnerHtml = string.Format("<strong>{0}</strong>", ((Questions)item.DataItem).QuestionText);
            switch (((Questions)item.DataItem).QuestionType)
            {
                case "YesNo":
                    {
                        RadioButtonList rbl = new RadioButtonList();
                        rbl.ID = ((Questions)item.DataItem).QuestionId.ToString();
                        ListItem liYes = new ListItem();
                        liYes.Text = "Yes";
                        rbl.Items.Add(liYes);
                        ListItem liNo = new ListItem();
                        liNo.Text = "No";
                        rbl.Items.Add(liNo);
                        wrapper.Controls.Add(rbl);
                        break;
                    }
                case "Rate":
                    {
                        RadRating rr = new RadRating();
                        rr.ID = ((Questions)item.DataItem).QuestionId.ToString();
                        rr.ItemCount = 10;
                        wrapper.Controls.Add(rr);
                        break;
                    }
                case "Multi":
                    {
                        Int32 qId = ((Questions)item.DataItem).QuestionId;
                        RadioButtonList rbl = new RadioButtonList();
                        rbl.ID = ((Questions)item.DataItem).QuestionId.ToString();
                        using (OSWEB_GenericRemittanceEntities OSWEBe = new OSWEB_GenericRemittanceEntities())
                        {
                            var answers = (from a in OSWEBe.Answers where a.QuestionID == qId select a);
                            foreach (Answer a in answers)
                            {
                                ListItem li = new ListItem();
                                li.Text = a.AnswerText;
                                li.Value = a.AnswerID.ToString();
                                rbl.Items.Add(li);
                            }
                            wrapper.Controls.Add(rbl);
                        }
                        break;
                    }
                case "End":
                    {
                        Button bt = new Button();
                        bt.Text = "Submit Survey";
                        bt.ID = "ButtonSubmitSurvey";
                        bt.Click += new System.EventHandler(bt_Click);
                        wrapper.Controls.Add(bt);
                        break;
                    }
            }
        }
        private void bt_Click(object Sender, EventArgs e)
        {
              
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Fiko
Telerik team
answered on 11 Aug 2010, 09:42 AM
Hi Tim,

I have used a part of the provided code and prepared a demo which shows the exact steps in order to achieve the desired result. The main change is that I use the RadRotator's OnItemClick event instead of the Button.Click one. In this case you can find the controls inside the clicked Rotator's item by using this approach:
protected void RadRotator1_ItemClick(object sender, RadRotatorEventArgs e)
{
    RadRating rating = e.Item.FindControl("RadRating1") as RadRating;
    decimal val = rating.Value;
    RadioButtonList radioButtons = e.Item.FindControl("RadioButtonList1") as RadioButtonList;
    Button button = e.Item.FindControl("ButtonSubmitSurvey") as Button;
}

The source code of the demo is attached to this mail.

I hope this helps.


Best wishes,
Fiko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Rotator
Asked by
tmorford
Top achievements
Rank 1
Answers by
Fiko
Telerik team
Share this question
or