radComboBox problem with SelectedValue

14 posts, 0 answers
  1. Bob
    Bob avatar
    87 posts
    Member since:
    Jan 2008

    Posted 31 Mar 2008 Link to this post

    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

  2. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 03 Apr 2008 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bob
    Bob avatar
    87 posts
    Member since:
    Jan 2008

    Posted 03 Apr 2008 Link to this post

    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.

  5. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 07 Apr 2008 Link to this post

    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
  6. Bob
    Bob avatar
    87 posts
    Member since:
    Jan 2008

    Posted 07 Apr 2008 Link to this post

    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

  7. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 09 Apr 2008 Link to this post

    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
  8. Bob
    Bob avatar
    87 posts
    Member since:
    Jan 2008

    Posted 09 Apr 2008 Link to this post

    That worked!

    Thanks Simon.

    Bob
  9. Ravindra
    Ravindra avatar
    5 posts
    Member since:
    Apr 2014

    Posted 19 Apr 2014 Link to this post

    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.
  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Apr 2014 in reply to Ravindra Link to this post

    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.
  11. Ravindra
    Ravindra avatar
    5 posts
    Member since:
    Apr 2014

    Posted 19 Apr 2014 in reply to Shinu Link to this post

    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. 
  12. Ravindra
    Ravindra avatar
    5 posts
    Member since:
    Apr 2014

    Posted 19 Apr 2014 in reply to Ravindra Link to this post

    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.
  13. Ravindra
    Ravindra avatar
    5 posts
    Member since:
    Apr 2014

    Posted 19 Apr 2014 in reply to Ravindra Link to this post

    please checkout this image.
  14. Ravindra
    Ravindra avatar
    5 posts
    Member since:
    Apr 2014

    Posted 19 Apr 2014 in reply to Ravindra Link to this post

    another form image please have a look
  15. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Apr 2014 in reply to Ravindra Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017