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

Dynamically Creating a Cascaded RadCombo User Control

1 Answer 40 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 22 Mar 2012, 09:44 AM
Hi all,

I've successfully created a casecaded radcombobox using the demo from here:

http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multiplecomboboxes/defaultcs.aspx 

I'm now trying to turn this into a user control and create it dynamically. The problem is the Page_Load JS function of the user control never fires. Also whenever the Page_Load of the aspx.cs fires IsPostback is always true. Can anyone help? 

User control code behind:
public partial class ctrlCascadedProcedureDropDown : System.Web.UI.UserControl
{
    public int ProcedureTypeSelectedValue { get; set; }
    public int ProcedureSelectedValue { get; set; }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadProcedureTypes();
 
            if (ProcedureTypeSelectedValue > 0)
                LoadProcedures(ProcedureTypeSelectedValue.ToString());
 
            rcbProcedureType.SelectedValue = ProcedureTypeSelectedValue.ToString();
            rcbProcedure.SelectedValue = ProcedureSelectedValue.ToString();
        }
        else if (!Page.IsCallback)
        {
            if (rcbProcedureType != null)
                LoadProcedures(rcbProcedureType.SelectedValue);
        }
    }
 
    protected void rcbProcedureType_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadProcedureTypes();
    }
 
    protected void rcbProcedure_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadProcedures(e.Text);
    }
 
    protected void LoadProcedureTypes()
    {
        TARNDB.OpsProcedureType opsProcedureType = new TARNDB.OpsProcedureType();
        rcbProcedureType.DataSource = opsProcedureType.GetProcedureTypes().OrderBy(x => x.ProcedureType);;
        rcbProcedureType.DataValueField = "ID";
        rcbProcedureType.DataTextField = "ProcedureType";
        rcbProcedureType.DataBind();
 
        rcbProcedureType.Items.Insert(0, new RadComboBoxItem("")); 
    }
 
    protected void LoadProcedures(string proceduretypeid)
    {
        int _proceduretypeid;
        if (int.TryParse(proceduretypeid, out _proceduretypeid))
        {
            TARNDB.OpsProcedure opsProcedure = new TARNDB.OpsProcedure();
            rcbProcedure.DataSource = opsProcedure.GetProcedures(_proceduretypeid).OrderBy(x => x.ProcedureName); ;
            rcbProcedure.DataValueField = "ID";
            rcbProcedure.DataTextField = "ProcedureName";
            rcbProcedure.DataBind();
        }
    }
}

User control mark-up:
<div>
<telerik:RadComboBox ID="rcbProcedureType" runat="server"
    OnClientSelectedIndexChanging="LoadProcedures"
    OnItemsRequested="rcbProcedureType_ItemsRequested" />
<telerik:RadComboBox ID="rcbProcedure" runat="server" Width="250px"
    OnClientItemsRequested="ItemsLoaded"
    OnItemsRequested="rcbProcedure_ItemsRequested" />
</div>
 
<script type="text/javascript">
 
var rcbProcedure;
var rcbProcedureType;
 
function pageLoad() {
    rcbProcedure = $find("<%= rcbProcedure.ClientID %>");
    rcbProcedureType = $find("<%= rcbProcedureType.ClientID %>");
 
    alert("in");
}
 
function LoadProcedures(sender, eventArgs) {
    var item = eventArgs.get_item();
    rcbProcedure.set_text("Loading...");
 
    // if a procedure type is selected
    if (item.get_index() > 0) {
        // this will fire the ItemsRequested event of the
        // procedures combobox passing the procedureTypeID as a parameter
        rcbProcedure.requestItems(item.get_value(), false);
    }
    else {
        // the -Select a continent- item was chosen
        rcbProcedure.set_text(" ");
        rcbProcedure.clearItems();
    }
}
 
function ItemsLoaded(sender, eventArgs) {
    if (sender.get_items().get_count() > 0) {           
        // pre-select the first item
        sender.set_text(sender.get_items().getItem(0).get_text());
        sender.get_items().getItem(0).highlight();
    }
    //sender.showDropDown();
}
 
</script>

Page using the user control code behind:
public partial class Operations : BasePage
{
    public List<DynamicControl> DynamicControls
    {
        get
        {
            return (List<DynamicControl>)Session["_DynamicControls"];
        }
        set
        {
            Session["_DynamicControls"] = value;
        }
    }
 
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DynamicControls = null;
        }
        else
        {
            if (DynamicControls != null && DynamicControls.Count > 0)
            {
                foreach (DynamicControl control in DynamicControls)
                {
                    ctrlCascadedProcedureDropDown cpd = new ctrlCascadedProcedureDropDown();
                    cpd.ID = control.ID;
 
                    switch (control.PlaceHolder.ID)
                    {
                        case "phProcedure":
                            phProcedure.Controls.Add(cpd);
                            phProcedure.Controls.Add(new Literal() { Text = "<BR />" });
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
 
    protected void btnAddProcedure_Click(object sender, EventArgs e)
    {
        AddDynamicProcedureControl("Procedure", phProcedure);
    }
 
    private void AddDynamicProcedureControl(string name, PlaceHolder placeHolder, int procedureTypeSelectedValue = 0,
        int procedureSelectedValue = 0)
    {
        Control control = LoadControl("~/Controls/ctrlCascadedProcedureDropDown.ascx");
        ctrlCascadedProcedureDropDown cpd = (ctrlCascadedProcedureDropDown)control;
 
        int controlCount = (DynamicControls == null ? 0 : DynamicControls.Count);
        string ID = string.Concat(name, controlCount.ToString());
        cpd.ID = ID;
        cpd.ProcedureTypeSelectedValue = procedureTypeSelectedValue;
        cpd.ProcedureSelectedValue = procedureSelectedValue;
 
        DynamicControl dc = new DynamicControl() { ID = ID, PlaceHolder = placeHolder };
        if (DynamicControls == null)
            DynamicControls = new List<DynamicControl>();
        DynamicControls.Add(dc);
 
        placeHolder.Controls.Add(cpd);
        placeHolder.Controls.Add(new Literal() { Text = "<BR />" });
    }
 
    private List<int> GetDynamicList(PlaceHolder placeHolder)
    {
        List<int> list = null;
        list = new List<int>();
        foreach (Control control in placeHolder.Controls)
        {
            if (control is GenericDropDown)
            {
                int id;
                if (int.TryParse(((GenericDropDown)control).SelectedValue, out id) && id > 0)
                    list.Add(id);
            }
        }
 
        return list;
    }
}

1 Answer, 1 is accepted

Sort by
0
Paul
Top achievements
Rank 1
answered on 26 Mar 2012, 08:54 AM
I wasn't using LoadControl in the Page_Init.
Tags
ComboBox
Asked by
Paul
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
Share this question
or