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

radComboBox problem with SelectedValue

13 Answers 1137 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 31 Mar 2008, 01:21 PM
I have a simple rad ComboBox with a value field and a text field.  I bind the combobox to a data source and this works fine.  Now I  want to highlight the selectedvalue.  But I cannot get this to work.  Here is my code:

<

Telerik:radComboBox id="cbPolicyAccountValueSource"

DataTextField="value"

DataValueField="key"

runat="server">

</Telerik:radComboBox>

cbPolicyAccountValueSource.DataSource = LifePolicyAccountValue.PolicyAccountValueSources

cbPolicyAccountValueSource.DataBind()


Dim

PolicyAccountValueSourceEnumId As Object = DataBinder.Eval(DataItem, "PolicyAccountValueSourceEnumId")


cbPolicyAccountValueSource.SelectedValue =

CInt(PolicyAccountValueSourceEnumId)


If I run in debug mode, the above field (PolicyAccountValueSourceEnumId) has the selected value I want the combobox to be selected.  But it is not.  No item is selected.

What am I doing wrong?

Thanks,
Bob

13 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 03 Apr 2008, 10:56 AM
Hi Bob,

Thank you for writing to us.

Do you execute the code below in the ItemDataBound event handler?

Dim 
 
PolicyAccountValueSourceEnumId As Object = DataBinder.Eval(DataItem, "PolicyAccountValueSourceEnumId"
 
 
cbPolicyAccountValueSource.SelectedValue = 
 
CInt(PolicyAccountValueSourceEnumId) 

If this is so, you could skip this step and simply set the selected index or the selected value of the RadComboBox right after you data bind it.

I hope this helps.

Regards,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Bob
Top achievements
Rank 1
answered on 03 Apr 2008, 12:37 PM

Simon,

I do not use the itemdatabound event.  I am doing as you said.  I load my radgrid with my data then I try to set the selected row right after it.  Here is my code again:


cbPolicy.DataSource = LifePolicye.PolicyAccountValue

cbPolicy.DataBind()


Dim

PolicyId As Object = DataBinder.Eval(DataItem, "PolicyId")


cbPolicy.SelectedValue = CInt(PolicyId)

Like I said, if I debug the above line, PolicyID is the correct value.

0
Simon
Telerik team
answered on 07 Apr 2008, 11:25 AM
Hello Bob,

Is it possible that the posted code is in the ItemDataBound event handler of some other control which has the RadComboBox in its item template like a RadGrid?

Greetings,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Bob
Top achievements
Rank 1
answered on 07 Apr 2008, 02:35 PM
Simon,

Here is my VB.Net code behind:

Partial

Class PolicyValuesDetails

Inherits System.Web.UI.UserControl

Private _dataItem As Object = Nothing

#Region

"Web Form Designer generated code"

Protected Overrides Sub OnInit(ByVal e As EventArgs)

'

' CODEGEN: This call is required by the ASP.NET Web Form Designer.

'

InitializeComponent()

MyBase.OnInit(e)

End Sub 'OnInit

 

'/ <summary>

'/ Required method for Designer support - do not modify

'/ the contents of this method with the code editor.

'/ </summary>

Private Sub InitializeComponent()

AddHandler DataBinding, AddressOf Me.PolicyValuesDetails_DataBinding

End Sub 'InitializeComponent

#End

Region

Public Property DataItem() As Object

Get

Return Me._dataItem

End Get

Set(ByVal value As Object)

Me._dataItem = value

End Set

End Property

Protected Sub PolicyValuesDetails_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)

cbPolicyAccountValueSource.DataSource = LifePolicyAccountValue.PolicyAccountValueSources

cbPolicyAccountValueSource.DataBind()

cbPolicyAccountValueSource.SelectedValue = DataBinder.Eval(DataItem,

"PolicyAccountValueSourceEnumId")

End Sub

End

Class

Here is my ASCX code for the radComboBox:

<

telerik:RadComboBox ID="cbPolicyAccountValueSource"

TabIndex="2"

DataTextField="value"

DataValueField="key"

runat="server">

</telerik:RadComboBox>

This line of VB.Net.

cbPolicyAccountValueSource.SelectedValue = DataBinder.Eval(DataItem, "PolicyAccountValueSourceEnumId")

is where I want to set the selectedvalue.  In debug mode,  DataBinder.Eval(DataItem, "PolicyAccountValueSourceEnumId")
 = 2 which is correct.  But the first value is always selected. 

I stripped out all the AJAX stuff too thinking this was the problem.  But it still does not work.  Please note that this is a user control that displays when the Edit button is clicked on a radGrid which resides in another user control.  It is impossible to send you a working version since my client will not allow it.  But I do not understand why this does not work.  When I try to set the SelectedValue with the correct value, it just does not work.

Thanks,
Bob

0
Simon
Telerik team
answered on 09 Apr 2008, 03:05 PM
Hello Bob,

Thank you for providing the additional code.

You could try assigning an empty data source to the RadComboBox control at the end of the DataBinding routine, like this:

Protected Sub PolicyValuesDetails_DataBinding(ByVal sender As ObjectByVal e As System.EventArgs) 
 
cbPolicyAccountValueSource.DataSource = LifePolicyAccountValue.PolicyAccountValueSources 
 
cbPolicyAccountValueSource.DataBind() 
 
cbPolicyAccountValueSource.SelectedValue = DataBinder.Eval(DataItem, 
 
"PolicyAccountValueSourceEnumId"
 
cbPolicyAccountValueSource.DataSource = Nothing 
 
End Sub

You could read this online help article for more information - Custom Edit Forms and more specifically the "Binding selected values for MS DropDownLists when having user control custom edit form" section.

Please let me know how it goes.

Sincerely yours,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Bob
Top achievements
Rank 1
answered on 09 Apr 2008, 03:32 PM
That worked!

Thanks Simon.

Bob
0
Ravindra
Top achievements
Rank 1
answered on 19 Apr 2014, 05:54 AM
Dear Sir, 
I am also facing the same problem with RadComboBox.
Problem is By RadListView Selection Radcombobox selected value is to be change but it is not working.
0
Shinu
Top achievements
Rank 2
answered on 19 Apr 2014, 06:37 AM
Hi Ravindra,

Please have a look into the sample code snippet which works fine at my end.

ASPX:
<telerik:RadListView ID="RadListView1" Width="97%" runat="server" DataSourceID="SqlDataSource1"
    ItemPlaceholderID="ProductsHolder" DataKeyNames="ProductID" OnItemDataBound="RadListView1_ItemDataBound">
    <LayoutTemplate>
        <fieldset style="max-width: 920px;" id="FieldSet1">
            <legend>Products</legend>
            <asp:Panel ID="ProductsHolder" runat="server" />
        </fieldset>
    </LayoutTemplate>
    <ItemTemplate>
        <telerik:RadComboBox ID="radcboSelectedValue" runat="server">
        </telerik:RadComboBox>
    </ItemTemplate>
</telerik:RadListView>

C#:
protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
{
    RadComboBox selectCombo = e.Item.FindControl("radcboSelectedValue") as RadComboBox;
    selectCombo.DataSource = "SqlDataSource1";
    selectCombo.DataBind();
    selectCombo.DataValueField = "ProductID";
    selectCombo.SelectedItem.Text = "3";
    selectCombo.DataSource = null;
}

Let me know if you have any concern.
Thanks,
Shinu.
0
Ravindra
Top achievements
Rank 1
answered on 19 Apr 2014, 06:45 AM
Actually i have a user control which have RadListview with databinding from backend Database and a button in item template.
By clicking button another user control visible true and it has radcombobox with databind method.

What happen is at first time clicked row button of listview is selected in visibled radcombobox.

But second time not changes the value of radcombobox of usercontrol. 
0
Ravindra
Top achievements
Rank 1
answered on 19 Apr 2014, 06:48 AM
By checking code using breakpoints i found that
RadCombobox SelectedItems(Text and value) changes every time based on listview but Text is not changes .

My code works fine with asp:dropdownlist but i am to use RadCombobox Please help me out.
0
Ravindra
Top achievements
Rank 1
answered on 19 Apr 2014, 06:57 AM
please checkout this image.
0
Ravindra
Top achievements
Rank 1
answered on 19 Apr 2014, 07:25 AM
another form image please have a look
0
Shinu
Top achievements
Rank 2
answered on 21 Apr 2014, 05:36 AM
Hi Ravindra,

Please have a look into the sample code snippet which works fine at my end. Please provide your full code if it doesn't help.

ASPX:
<uc1:ListViewControl ID="ListViewControl1" runat="server" />

ListViewUserControl.ASCX:
<telerik:RadListView ID="radlstviewServerBind" runat="server" ItemPlaceholderID="ProductsHolder"
    OnNeedDataSource="radlstviewServerBind_NeedDataSource">
    <LayoutTemplate>
        <fieldset style="max-width: 920px;" id="FieldSet1">
            <legend>Products</legend>
            <asp:Panel ID="ProductsHolder" runat="server" />
        </fieldset>
    </LayoutTemplate>
    <ItemTemplate>
        <telerik:RadButton ID="radbtnShowCombo" runat="server" Text="Apply" OnClick="radbtnShowCombo_Click">
        </telerik:RadButton>
    </ItemTemplate>
</telerik:RadListView>

ListViewUserControl C#:
protected void radlstviewServerBind_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
{
    radlstviewServerBind.DataSourceID = "SqlDataSource1";
}
protected void radbtnShowCombo_Click(object sender, EventArgs e)
{
    UserControl control = LoadControl("ComboBox.ascx") as UserControl;
    this.Controls.Add(control);
    RadComboBox combo = control.FindControl("radcboSelectedValue") as RadComboBox;
    //here you can select the item of the RadComboBox
    combo.SelectedValue = "10259";
}

ComboBox.ASCX:
<telerik:RadComboBox ID="radcboSelectedValue" runat="server" DataSourceID="SqlDataSource2"
    DataValueField="OrderID" DataTextField="CustomerID">
</telerik:RadComboBox>

Thanks,
Shinu.
Tags
ComboBox
Asked by
Bob
Top achievements
Rank 1
Answers by
Simon
Telerik team
Bob
Top achievements
Rank 1
Ravindra
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or