Beginner's question - RadComboBox value problems

6 posts, 1 answers
  1. Boris
    Boris avatar
    289 posts
    Member since:
    Oct 2012

    Posted 18 Oct 2012 Link to this post

    I'm new to RadControls and I'm trying to rough out a simple page using a couple of RadComboBoxes, version 4.0.

    First of all I note that there seems to be a great deal of chatter on this forum about bugs in the boxes.

    However I wonder if my problem is related or whether I can't use these as ordinary combo boxes.

    The code is extremely simple.
    I bind it like this:
     cb.DataTextField = "Desc";
     cb.DataValueField = "Id";
     cb.DataBind();

    I then hit an ordinary Asp.Net button and try to get the selected values in the click event. 
    The data appears to have loaded correctly and the selected text is correct but I cannot get the correct selected value.
    I've tried both SelectedValue and SelectedItem.Value.  It's as if it's not setting at all.
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Oct 2012 Link to this post

    Hi Boris,

    RadComboBox performs a Post Back every time a different item is selected and this causes a new page load. When the Load stage of the page life cycle is reached the RadComboBox is rebound, and this action overwrites the previous selected values. You could consider using 2 approaches in order to  resolve this issue. The first one is to bind the control only upon initial load as shown below:

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
       RadComboBox1.DataSourceID = SqlDataSource1.ID;
       RadComboBox1.DataTextField = "name";
       RadComboBox1.DataValueField = "name";
       RadComboBox1.DataBind();
      }
    }

    The second approach is to perform data binding at the Init stage of the life cycle, because at that point the ViewState is still not applied.


    Thanks,
    Princy.
  3. Boris
    Boris avatar
    289 posts
    Member since:
    Oct 2012

    Posted 19 Oct 2012 Link to this post

    As I said, I'm new to this version of Rad. 

    The AutoPostBack on the control is set to False.  Are you referring to something else? 
  4. Nencho
    Admin
    Nencho avatar
    1871 posts

    Posted 19 Oct 2012 Link to this post

    Hi Boris,

    Could you provide us the implementation of the RadComboBox that you use at your end. In addition, could you clarify the version of the controls that you are using?

    Regards,
    Nencho
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  5. Boris
    Boris avatar
    289 posts
    Member since:
    Oct 2012

    Posted 22 Oct 2012 Link to this post

    Implementation?

    So far it's just a bare bones Asp.Net screen with a couple of combo boxes on it.

    if (!IsPostBack)
     {
         rcb1.DataSource = DataClass1.GetTable();
         rcb1.DataTextField = "Desc";
         rcb1.DataValueField = "Id";
         rcb1.DataBind();
     }
    The DataSource is a DataTable.

    The Telerik version is 2012.1.501.40,  runtime version: v4.0.30319.
  6. Nencho
    Admin
    Nencho avatar
    1871 posts

    Posted 23 Oct 2012 Link to this post

    Hi Boris,

    I have prepared sample page for you, implementing the desired functionality. In the provided sample, I use the OnClick event handler of the Button, in order to get the RadComboBox's SelectedValue.


    Kind regards,
    Nencho
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top