Search code examples
c#asp.nettelerikselectedvalueradcombobox

Telerik Radcombobox.SelectedValue not working


I am trying to populate my telerik dropdown on page load. I am using the following code for this:

ddIntervention.SelectedValue=3

The RadComboBox is inside two update panels.

aspx page:

<telerik:RadComboBox ID="ddIntervention" runat="server" DataSourceID="SqlDataSource3" 
 DataTextField="ProcedureDesc" MaxHeight="180px" Filter="Contains" 
 DataValueField="ProcedureID" HighlightTemplatedItems="True" MarkFirstMatch="True" 
 Width="350px" EmptyMessage="Search By Intervention Name" EnableAutomaticLoadOnDemand="True" 
 ShowMoreResultsBox="True" EnableVirtualScrolling="True" ItemsPerRequest="10" 
 DropDownWidth="350px" AutoPostBack="True" CausesValidation="False" 
 ResolvedRenderMode="Classic"  AllowCustomText="true">
</telerik:RadComboBox>


<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
 ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
 SelectCommand="SELECT [ProcedureID], [ProcedureDesc], [DateDeleted] FROM 
 [Vw_UploadRef_Procedures] WHERE ([DateDeleted] IS NULL)
</asp:SqlDataSource>

Solution

  • I think your problem will be EnableAutomaticLoadOnDemand="True" reason because the RadComboBox will be empty until you click on it. That's why you set SelectedValue won't take effect on Page_Load

    Update Code

    .aspx

    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Button ID="btn" runat="server" Text="Dummy Button" />
            <br /><br />
            <asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <telerik:RadComboBox ID="rcb" runat="server" AutoPostBack="true" 
                        AllowCustomText="true" HighlightTemplatedItems="true"
                        EmptyMessage="Search By Name" MarkFirstMatch="true"
                        ShowMoreResultsBox="true" EnableVirtualScrolling="true"
                        DropDownWidth="350px" CausesValidation="false"
                        ItemsPerRequest="10" RenderMode="Classic"
                        OnSelectedIndexChanged="rcb_SelectedIndexChanged"></telerik:RadComboBox>
                    <br />
                    <asp:Label ID="lbl" runat="server"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="rcb" />
                </Triggers>
            </asp:UpdatePanel>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btn" />
        </Triggers>
    </asp:UpdatePanel>
    

    .cs

    protected void Page_Load(object sender, EventArgs e)
    {
        // Check
        if (!IsPostBack)
        {
            // Variable
            string[] text = { "A","B","C", "D", "E", "F" };
            DataTable dt = new DataTable();
            dt.Columns.Add("Text");
            dt.Columns.Add("Value");
    
            // Add Rows
            for (int i = 0; i < text.Length; i++)
                dt.Rows.Add(text[i], i + "");
    
            // Bind to Drop Down
            rcb.DataSource = dt;
            rcb.DataTextField = "Text";
            rcb.DataValueField = "Value";
            rcb.DataBind();
    
            // Check
            if (rcb.Items.Count > 0)
            {
                rcb.SelectedValue = "3";
                rcb_SelectedIndexChanged(rcb, new RadComboBoxSelectedIndexChangedEventArgs
                (rcb.SelectedItem.Text.Trim(), "", rcb.SelectedValue, "")); 
                // Trigger Selected Index Changed
            }
        }
    }
    
    protected void rcb_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        // Check if the dropdown is selectedIndex is greater or equal first item
        // if you have "Please select" on first item just change ">=" to ">"
        if (rcb.SelectedIndex >= 0)
        {
            lbl.Text = rcb.SelectedItem.Text.Trim();
        }
    }
    

    Result On Page Load
    enter image description here