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

SelectedIndex not working with Inherited RadComboBox

5 Answers 264 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Chris Mitchell
Top achievements
Rank 1
Chris Mitchell asked on 25 May 2011, 12:08 AM
Hi,

I have a series of user control that inherit from RadComboBox. They are used throughout the app, so this is primarily for the purpose of consistency and code re-use. However, I cannot seem to get my default 'Select a <xyz>' item to be the selected item upon the first page load. They show up in the drop-down but the text area of the combo box is blank until Postback, at which time it shows the 'Select a <xyz>' item correctly (assuming the user didn't pick anything).

Everything else seems to be working fine. Frankly, though, my OOP chops are a bit rusty and so I am wondering if I am doing this completely wrong.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OSIRISModel;
 
namespace Telerik.Web.UI.OSIRIS
{
  /// <summary>Summary description for OS_JobStatusComboBox</summary>
  public class OS_JobStatusComboBox : RadComboBox
  {
    #region Constructor
    public OS_JobStatusComboBox()
    {
      //
      // TODO: Add constructor logic here
      //
    }
    #endregion
 
    #region Control Events
    protected override void OnLoad(EventArgs e)
    {
      if (!Page.IsPostBack)
      {
        OSIRISEntities context = ((OSIRISPage)Page).OSIRISContext;
 
        List<JobStatu> items = context.JobStatus.ToList<JobStatu>();
        this.DataTextField = "Description";
        this.DataValueField = "ID";
        this.DataSource = items;
        this.RegisterWithScriptManager = false;
 
        this.AppendDataBoundItems = true;
        this.Items.Insert(0, new RadComboBoxItem("Select a Job Status", ""));
        this.SelectedIndex = 0;
      }
    }
 
    // This is to prevent double-binding in cases where this control is used inside another control that causes Child Databinding (such as Formview)
    private bool Bound = false;
    protected override void OnDataBound(EventArgs e) { Bound = true; }
    protected override void OnPreRender(EventArgs e) { if (Bound == false) { this.DataBind(); } }
 
    #endregion
  }
}


Little help?

5 Answers, 1 is accepted

Sort by
0
Helen
Telerik team
answered on 26 May 2011, 03:02 PM
Hello Chris,

What is the exact version of the Telerik.Web.UI that you use?
The code below works in a simple .aspx page:
//aspx:
<telerik:RadComboBox ID="RadComboBox2" runat="server" Width="250px" Height="400px">
</telerik:RadComboBox>

cs:
protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
           BindToTable(RadComboBox2);
 
           RadComboBox2.AppendDataBoundItems = true;
           RadComboBox2.Items.Insert(0, new RadComboBoxItem("Select a Job Status", ""));
           RadComboBox2.SelectedIndex = 0;
        
       }
   }
 
   protected void BindToTable(RadComboBox combo)
   {
       DataTable table = new DataTable();
 
       table.Columns.Add("One");
       table.Columns.Add("Two");
       table.Columns.Add("Three");
 
       table.Rows.Add(new string[] { "One", "One", "One" });
       table.Rows.Add(new string[] { "Two", "Two", "Two" });
       table.Rows.Add(new string[] { "Three", "Three", "Three" });
 
 
       combo.DataSource = table;
       combo.DataTextField = "One";
       combo.DataValueField = "One";
       combo.DataBind();
   }


Regards,
Helen
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Chris Mitchell
Top achievements
Rank 1
answered on 26 May 2011, 08:27 PM
I'm using the 3rd quarter 2010 tools..

However, the example you posted doesn't really apply to my scenario. When used on their own like that, the controls work fine for me. The problem I am encountering only occurs when I create my own class that inherits from RCB
0
Kalina
Telerik team
answered on 28 May 2011, 12:12 PM
Hi Chris Mitchell,

By design the SelectedIndex property is not intended to be set explicitly by the developer - it is used by the control internally.
The approach that my colleague suggested you is correct.

I have inspected the code of your implementation and I saw that override the OnDataBound and OnPreRender control event handlers. However these handler methods have certain implementations in the base class, but you do not call them within the override methods. In this way you replace the logic in the base class and you can cause unpredictable control behaviour.

Please call the base event handlers and then add the additional logic that you want to execute in this way:
protected override void OnDataBound(EventArgs e)
{
    base.OnDataBound(e);
    Bound = true;
}
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    if (Bound == false)
    {
         this.DataBind();
    }
}

More details you can find at the sample attached.

All the best,
Kalina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Chris Mitchell
Top achievements
Rank 1
answered on 28 May 2011, 10:59 PM
Point taken on the overrides; I forgot to add the base.OnDataBound and base.OnPreRender..

However, I am confused. You say that SelectedIndex isn't intended to be set explicitly by the developer and that your colleague's suggestion is right.. But that is exactly what she does in her Page_Load?
0
Kalina
Telerik team
answered on 01 Jun 2011, 04:15 PM
Hi Chris Mitchell,

Maybe I've been not enough clear in my answer – excuse me for the confusion caused.
The example that you mention is based on your code and it can be used, as well as the one that I've provided to you.
Simply, I am suggesting you to set the selected item, because this approach seems to be more easy and elegant.  However you can continue using the SelectedIndex property if this is more suitable to you.

All the best,
Kalina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
ComboBox
Asked by
Chris Mitchell
Top achievements
Rank 1
Answers by
Helen
Telerik team
Chris Mitchell
Top achievements
Rank 1
Kalina
Telerik team
Share this question
or