zic10
zic10

Reputation: 2330

RadComboBox Binding Selected Value

I have a radcombobox and I'm trying to bind the selected value to data I'm pulling back from a table in my database.

  <telerik:RadComboBox ID="cboRole" runat="server"  DataValueField="UserType.ID" DataTextField="UserType.Value" Text='<%# Bind("UserType") %>' DataSourceID="odsCertifications"  >

                                    </telerik:RadComboBox>

Here is my data

   public partial class Certification
{
    public Certification()
    {
        this.Courses = new HashSet<CertificationCourse>();
    }

    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Duration { get; set; }
    public Nullable<System.DateTime> Expiration { get; set; }
    public bool IsActive { get; set; }
    public string CreatedBy { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public string UpdatedBy { get; set; }
    public Nullable<System.DateTime> UpdatedOn { get; set; }
    public Nullable<int> UserTypeID { get; set; }

    public virtual ICollection<CertificationCourse> Courses { get; set; }
    public virtual SystemCode UserType { get; set; }
}

The UserType has the attributes Value and ID upon which I'm trying to bind the DataTextFields and DataValueFields. However, I'm getting javascript exceptions when trying to bind this way. The javascript message if omitted so I can't fully see what the exact error message is.

Here is what the chrome js debugger is telling me is happening:

Uncaught Sys.WebForms.PageRequestManagerServerErrorException: 
Sys.WebForms.PageRequestManagerServerErrorException: Object of type
System.Data.Entity.DynamicProxies.Certification_D985D293565CB0BFD87FA8F8F44D70ACF4130D6E138A82D6E486544C53D7FE10 does ...<omitted>...y.

<asp:ObjectDataSource runat="server" ID="odsCertifications" SelectMethod="GetActiveCertifications" TypeName="SitefinityWebApp.Controllers.CertificationController"></asp:ObjectDataSource>


    public List<Certification> GetActiveCertifications()
    {
        using (var db = new Entities())
        {
            try
            {

                return db.Certifications.Include("UserType").Where(x => x.IsActive == true).OrderBy(x => x.Title).ToList();

            }

            catch (Exception ex)
            {
                ExceptionManager.LogException(this, ex);
                return null;
            }
        }
    }

Upvotes: 0

Views: 4200

Answers (3)

rdmptn
rdmptn

Reputation: 5611

To get the actual server exception, remove AJAX. What you get is a server exception captured by MS AJAX during a partial postback and send (truncated) to the client as a JavaScript error.

I also think you may need to expose the string property you need as a direct field, not as a sub-field of the field you pass to the combo's property

Upvotes: 0

serhads
serhads

Reputation: 462

What would you like to do exactly? I guess "odsCertifications" is not a list. So if you would like to show only binded user type value which is in certification object. You don't need to bind any source to combobox. Just add list item to combobox in cs. side like:

cbcRole.Items.Add(new RadComboboxItem(){Value=odsCertifications.UserType.ID, Text=odsCertifications.UserType.Value})   //RadComboboxItem and properties can be different I wrote only as example.

If you would like to populate your combobox with all user types and show binded user type value which is in certification object as selected item, You have to bind UserType list object to your combobox like:

<telerik:RadComboBox ID="cboRole" runat="server"  DataValueField="ID" DataTextField="Value" DataSourceID="userTypes" ></telerik:RadComboBox> 

DataSourceID="userTypes" here, userTypes is must be List object like

List<UserType> userTypes;

Than you can set selected item in cs. side like:

cboRole.SelectedValue=odsCertifications.UserType.ID;

Upvotes: 0

darkchico
darkchico

Reputation: 647

I don't have any knowledge in the RAD version of Telerik, but I think the problem comes from the fact that you are trying to bind a SystemCode object to the ComboBox, and it can only probably handles simple types like int / string.
I guess you should be binding with the UserTypeID instead, but I'm not sure it would work because it's Nullable.

Upvotes: 1

Related Questions