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

RadComboBox Bound to string[] Not Selecting Specified Value

6 Answers 420 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
kevin floyd
Top achievements
Rank 1
kevin floyd asked on 25 May 2010, 05:18 PM
I have a RadGrid with an EditForm containing a RadComboBox.  The RadComboBox has a datasource set to a string[] (DataSource='<%# GetSampleValues() %>').  When creating and editing a record, the combobox displays the correct strings in the dropdown list.  When Editing an existing record, the selected string before expanding the combobox is not displaying the appropriate value.

<rad:RadComboBox ID="rcb_SampleValues" runat="server" AllowCustomText="false" 
   DataSource='<%# GetSampleValues() %>'  
   Text='<%# Bind("SampleValueString") %>' 
   Value='<%# Bind("SampleValueString") %>' 
   MaxLength="255"  
   Width="100" > 
</rad:RadComboBox> 


public enum SampleValues 
     SampleValue1, 
     SampleValue2 
}

public class Samples 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public SampleValues SampleValue { get; set; } 
     public string SampleValueString { get; set; } 
}


public void Page_Load(object sender, EventArgs e)
{
BindGridData();
}

public void BindDataGrid()
{
Samples[] samples = GetObjectsFromDataAccessLayer()
radGrid.DataSource = samples;
}
public static string[] GetSampleValues() 
     return Enum.GetNames(typeof(SampleValues)); 

I am binding the RadComboBox to an array of strings which are generated off of an Enum - but the same problem happens when I bind the RadCombobBox to a set of objects and set the DataTextMember and DataValueMember.  I have a sample application that I can send to someone that would like to see it.

Any ideas on what is not being done to make the comboBox NOT set to the intended value when the record is loaded?

6 Answers, 1 is accepted

Sort by
0
Veronica
Telerik team
answered on 26 May 2010, 09:41 AM
Hello kevin,

As I see the problem is that you are binding the Value of the RadComboBox to a non-unique value:

<rad:RadComboBox ID="rcb_SampleValues" runat="server" AllowCustomText="false"  
   DataSource='<%# GetSampleValues() %>'   
   Text='<%# Bind("SampleValueString") %>'  
   Value='<%# Bind("SampleValueString") %>'  
   MaxLength="255"   
   Width="100" >  
</rad:RadComboBox>  

Please take a look at this help topic when RadComboBox selects a different item.

If you still have some questions please send us the full code and we can help you.

Kind regards,
Veronica Milcheva
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.
0
kevin floyd
Top achievements
Rank 1
answered on 26 May 2010, 02:40 PM
The RadComboBox has unique text entries and value entries.  I can remove the Text= or Value= from the aspx file, and still will not get the appropriate record selected when editing an existing record.
0
Veronica
Telerik team
answered on 27 May 2010, 11:30 AM
Hello kevin floyd,

As I see on a  first sight there is nothing wrong in your code. At least from the part I see.

Could you please send me the full code so I can inspect it and help you?

Thank you.

Kind regards,
Veronica Milcheva
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.
0
kevin floyd
Top achievements
Rank 1
answered on 27 May 2010, 02:28 PM
Ok...  I have made a sample web application that exhibits the same behavior I am seeing in my live application.  There are 3 files: Objects.cs, Default.aspx, Default.aspx.cs.

Objects.cs:
namespace StringsComboBoxExample 
    public enum SampleValues 
    { 
        SampleValue1, 
        SampleValue2 
    } 
 
    public class Samples 
    { 
        public int ID { get; set; } 
        public string Name { get; set; } 
        public SampleValues SampleValue { get; set; } 
        public string SampleValueString { get; set; } 
    } 

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="StringsComboBoxExample._Default" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<%@ Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %> 
<%@ Register Assembly="RadComboBox.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title></title
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <rad:RadGrid ID="radGrid" runat="server" GridLines="None" PageSize="20" AllowSorting="true"  
            AutoGenerateColumns="false" EnableAJAX="true" ShowStatusBar="false" Width="1000"
            <PagerStyle Mode="NextPrevAndNumeric" /> 
            <MasterTableView CommandItemDisplay="Bottom" GridLines="None" DataKeyNames="ID"
                <Columns> 
                    <rad:GridBoundColumn Display="true" UniqueName="Name" DataField="Name" HeaderText="Name" /> 
                    <rad:GridBoundColumn Display="true" UniqueName="SampleValueString" DataField="SampleValueString" HeaderText="SampleValueString" /> 
                 
                    <rad:GridEditCommandColumn /> 
                </Columns> 
                <EditFormSettings EditFormType="Template"
                    <FormTemplate> 
                        <div> 
                            <table width="100%"
                                <tr> 
                                    <td align="left"
                                        <table> 
                                        <tr> 
                                            <td align="right"> Name:</td> 
                                            <td><asp:TextBox ID="tbx_Name" runat="server" ValidationGroup="newField" 
                                                    MaxLength="50" Height="15" Width="100" Text='<%# Bind("Name") %>' /> 
                                                <asp:RequiredFieldValidator ID="rfv_NameRequired" runat="server" ErrorMessage="*" Text="*"  
                                                    SetFocusOnError="true" ControlToValidate="tbx_Name" ValidationGroup="newField" /> 
                                            </td> 
                                            <td align="right"> Sample Value: </td> 
                                            <td> 
                                              <rad:RadComboBox ID="rcb_SampleValues" runat="server" AllowCustomText="false" 
                                                    DataSource='<%# GetSampleValues() %>' MaxLength="255" Text='<%# Bind("SampleValueString") %>' 
                                                    Width="100" ToolTip="Please select an Sample Values." ValidationGroup="newField"
                                              </rad:RadComboBox> 
                                              <asp:RequiredFieldValidator ID="rfv_SampleValueValidator" runat="server" ErrorMessage="*"  
                                                    SetFocusOnError="true" ControlToValidate="rcb_SampleValues" ValidationGroup="newField" /> 
                                            </td> 
                                        </tr> 
                                        </table> 
                                    </td> 
                                    <td align="right"
                                        <asp:Button ID="btnUpdate" runat="server" CausesValidation="true" ValidationGroup="newField" 
                                            Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
                                            CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' 
                                            OnClick="Update_Clicked" > 
                                        </asp:Button> 
                                        <asp:HiddenField ID="hdn_ID" runat="server" Value='<%# Bind("ID") %>' /> 
                                        &nbsp; 
                                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button><br /> 
                                    </td> 
                                </tr> 
                            </table>                         
                        </div>                   
                 
                    </FormTemplate> 
                </EditFormSettings> 
            </MasterTableView> 
        </rad:RadGrid> 
    </div> 
    </form> 
</body> 
</html> 

Default.aspx.cs:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
 
using Telerik.Web.UI; 
 
namespace StringsComboBoxExample 
    public partial class _Default : System.Web.UI.Page 
    { 
        List<Samples> _samples = null
 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            BindGridData(); 
        } 
 
        protected void BindGridData() 
        { 
            _samples = new List<Samples>(); 
            Samples sample = new Samples(); 
            sample.Name = "Sample 1"
            sample.SampleValue = SampleValues.SampleValue1; 
            sample.SampleValueString = Enum.GetName(typeof(SampleValues), sample.SampleValue); 
            _samples.Add(sample); 
 
            sample.Name = "Sample 2"
            sample.SampleValue = SampleValues.SampleValue2; 
            sample.SampleValueString = Enum.GetName(typeof(SampleValues), sample.SampleValue); 
            _samples.Add(sample); 
 
            radGrid.DataSource = _samples.ToArray(); 
        } 
 
        public static string[] GetSampleValues() 
        { 
            return Enum.GetNames(typeof(SampleValues)); 
        } 
 
        protected void Update_Clicked(object sender, EventArgs e) 
        { 
            Button btn = sender as Button; 
            if (btn == null) return; 
            List<string> keys = new List<string>(Request.Params.AllKeys); 
 
            string idKey = keys.Find(x => x.EndsWith("hdn_ID")); 
            string nameKey = keys.Find(x => x.EndsWith("tbx_Name")); 
            string sampleValuesKey = keys.Find(x => x.EndsWith("rcb_SampleValues_text")); 
 
            Samples sample = new Samples(); 
            sample.Name = Request.Params[nameKey].Trim(); 
            sample.SampleValue = (SampleValues)Enum.Parse(typeof(SampleValues), Request.Params[sampleValuesKey]); 
            sample.SampleValueString = Enum.GetName(typeof(SampleValues), sample.SampleValue); 
 
            _samples.Add(sample); 
 
            BindGridData(); 
            radGrid.Rebind(); 
        } 
    } 

0
Veronica
Telerik team
answered on 01 Jun 2010, 05:28 PM
Hello kevin floyd,

To be able to solve your problem you need to subscribe to the DataBound event on the RadComboBox:

<telerik:RadComboBox ID="rcb_SampleValues" runat="server" AllowCustomText="false" OnDataBound="rcb_SampleValues_DataBound"
                                                       DataSource='<%# GetSampleValues() %>' MaxLength="255" Text='<%# Bind("SampleValueString") %>'
                                                       Width="100" ToolTip="Please select an Sample Values." ValidationGroup="newField">
                                                   </telerik:RadComboBox>

and use this code in the handler:

protected void rcb_SampleValues_DataBound(object sender, EventArgs e)
    {
        ((RadComboBox)sender).FindItemByText(((Samples)GetDataItem()).SampleValueString).Selected = true;
    }

Find the edited code in the attached .zip file.

Hope this helps.

Regards,
Veronica Milcheva
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.
0
kevin floyd
Top achievements
Rank 1
answered on 03 Jun 2010, 03:25 PM
Thank you! Thank you! Thank you!

Veronica Wins!

Turns out, I don't have to have the Text='<%# Bind("SampleValueString") %>' at all.  Curious how it didn't work the way it was supposed to...  but not curious enough to make a deal out of it.

Thanks again!
Tags
ComboBox
Asked by
kevin floyd
Top achievements
Rank 1
Answers by
Veronica
Telerik team
kevin floyd
Top achievements
Rank 1
Share this question
or