Home / Community & Support / Knowledge Base / RadControls for ASP.NET and ASP.NET AJAX / ComboBox / Setting an initial value in RadComboBox while using Load on Demand

Setting an initial value in RadComboBox while using Load on Demand

Article Info

Rating: 3

Article information

Article relates to

Web.UI 2007.3 1314

or

RadComboBox v2.6+

Created by

Stephen, Telerik

Last modified  February 21, 2008

Last modified by

Simon, Telerik


HOW-TO
Set an initial value in RadComboBox while using Load on Demand.

DESCRIPTION
This can be helpful when the developer wants to display a default value in RadComboBox and still trigger async requests when the user changes the text in the control input.





SOLUTION
This example demonstrates how this can be done with Access datasource and OleDbDataReader (similar actions can be performed for other types of combobox sources). We use PopulateCombo(text) method to generate the combobox items both on initial load and on callback invocation. The difference is determined by the parameter value passed in the PopulateCombo method and the Page.IsCallback property.
We also hook the OnClientDropDownOpening event of the combobox and invoke explicitly the RequestItem(itemText, bool) method to display the appropriate set of items after form submit.

Note that this code logic ensures that the selected item value will be persisted after postback even if the user does not change the initial selection in it.

ASPX
<html xmlns="http://www.w3.org/1999/xhtml"
<head id="Head1" runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
        <div> 
            <telerik:RadComboBox  
                ID="RadComboBox1"  
                runat="server"  
                EnableLoadOnDemand="True"  
                MarkFirstMatch="True"  
                OnClientDropDownOpening="GetItems" 
                OnItemsRequested="RadComboBox1_ItemsRequested"
            </telerik:RadComboBox> 
            <asp:Button id="Button1" runat="server" Text="Get selected value" OnClick="Button1_Click"
            </asp:Button> 
        </div> 
    </form> 
    <script language="javascript"
        function GetItems(sender, eventArgs) 
        { 
            sender.requestItems(sender.get_text(), false); 
        } 
    </script> 
</body> 
</html> 

ASPX.CS
private void Page_Load(object sender, System.EventArgs e) 
    if ( ! Page.IsPostBack) 
    { 
        PopulateCombo(String.Empty); 
    } 
 
protected void RadComboBox1_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e) 
    PopulateCombo(e.Text); 
 
private void PopulateCombo(string text) 
    OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/NWind.mdb")); 
    dbCon.Open(); 
 
    OleDbCommand command; 
 
    if ((text == String.Empty) && ( ! Page.IsCallback)) 
    { 
        command = new OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE 'Hanna Moos'", dbCon); 
    } 
    else if ((text == String.Empty) && (Page.IsCallback)) 
    { 
        command = new OleDbCommand("SELECT * FROM Customers", dbCon); 
    } 
    else 
    { 
        command = new OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE '" + text + "%'", dbCon); 
    } 
 
    OleDbDataReader reader; 
    reader = command.ExecuteReader(); 
 
    RadComboBox1.DataTextField = "ContactName"
    RadComboBox1.DataValueField = "ContactTitle"
    RadComboBox1.DataSource = reader; 
    RadComboBox1.DataBind(); 
 
    dbCon.Close(); 
 
protected void Button1_Click(object sender, System.EventArgs e) 
    Response.Write("Selected value is: " + RadComboBox1.Text); 

ASPX.VB
    Private Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load 
        If Not Me.IsPostBack Then 
            PopulateCombo([String].Empty) 
        End If 
    End Sub 
 
    Protected Sub RadComboBox1_ItemsRequested(ByVal o As ObjectByVal e As Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs) 
        PopulateCombo(e.Text) 
    End Sub 
 
    Private Sub PopulateCombo(ByVal text As String
        Dim dbCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/NWind.mdb")) 
        dbCon.Open() 
 
        Dim command As OleDbCommand 
 
        If (text = [String].Empty) AndAlso (Not Me.IsCallback) Then 
            command = New OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE 'Hanna Moos'", dbCon) 
        ElseIf (text = [String].Empty) AndAlso (Me.IsCallback) Then 
            command = New OleDbCommand("SELECT * FROM Customers", dbCon) 
        Else 
            command = New OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE '" + text + "%'", dbCon) 
        End If 
 
        Dim reader As OleDbDataReader 
        reader = command.ExecuteReader() 
 
        RadComboBox1.DataTextField = "ContactName" 
        RadComboBox1.DataValueField = "ContactTitle" 
        RadComboBox1.DataSource = reader 
        RadComboBox1.DataBind() 
 
        dbCon.Close() 
    End Sub 
 
    Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) 
        Response.Write("Selected value is: " + RadComboBox1.Text) 
    End Sub 





SOLUTION
This example demonstrates how this can be done with Access datasource and OleDbDataReader (similar actions can be performed for other types of combobox sources). We use PopulateCombo(text) method to generate the combobox items both on initial load and on callback invocation. The difference is determined by the parameter value passed in the PopulateCombo method and the RadComboBox.IsCallback property.
We also hook the OnClientDropDownOpening event of the combobox and invoke explicitly the RequestItem(itemText, bool) method to display the appropriate set of items after form submit.

Note that this code logic ensures that the selected item value will be persisted after postback even if the user does not change the initial selection in it.

ASPX
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <div> 
            <rad:RadComboBox  
                ID="RadComboBox1"  
                runat="server" 
                EnableLoadOnDemand="True" 
                MarkFirstMatch="True"  
                OnClientDropDownOpening="GetItems"
            </rad:RadComboBox> 
             
            <asp:Button id="Button1" runat="server" Text="Get selected value"
            </asp:Button> 
        </div> 
     
        <script language="javascript"
            function GetItems(combo) 
            { 
                combo.RequestItems(combo.GetText(), false); 
            } 
        </script> 
    </form> 
</body> 
</html> 

ASPX.CS
private void Page_Load(object sender, System.EventArgs e) 
    if ( ! this.IsPostBack) 
    { 
        PopulateCombo(String.Empty); 
    } 
 
protected void RadComboBox1_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e) 
    PopulateCombo(e.Text); 
 
private void PopulateCombo(string text) 
    OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/NWind.mdb")); 
    dbCon.Open(); 
 
    OleDbCommand command; 
 
    if ((text == String.Empty) && ( ! RadComboBox1.IsCallback)) 
    { 
        command = new OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE 'Hanna Moos'", dbCon); 
    } 
    else if ((text == String.Empty) && (RadComboBox1.IsCallback)) 
    { 
        command = new OleDbCommand("SELECT * FROM Customers", dbCon); 
    } 
    else 
    { 
        command = new OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE '" + text + "%'", dbCon); 
    } 
 
    OleDbDataReader reader; 
    reader = command.ExecuteReader(); 
 
    RadComboBox1.DataTextField = "ContactName"
    RadComboBox1.DataValueField = "ContactTitle"
    RadComboBox1.DataSource = reader; 
    RadComboBox1.DataBind(); 
 
    dbCon.Close(); 
 
protected void Button1_Click(object sender, System.EventArgs e) 
    Response.Write("Selected value is: " + RadComboBox1.Text); 

ASPX.VB
    Private Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load 
        If (Not Page.IsPostBack) Then 
            PopulateCombo(String.Empty) 
        End If 
    End Sub 
 
    Private Sub RadComboBox1_ItemsRequested(ByVal o As ObjectByVal e As Telerik.WebControls.RadComboBoxItemsRequestedEventArgs) Handles RadComboBox1.ItemsRequested 
        PopulateCombo(e.Text) 
    End Sub 
    Private Sub PopulateCombo(ByVal text As String
        Dim dbCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/NWind.mdb")) 
        dbCon.Open() 
 
        Dim command As OleDbCommand 
 
        If (text = String.Empty AndAlso Not RadComboBox1.IsCallBack) Then 
            command = New OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE 'Hanna Moos'", dbCon) 
        ElseIf (text = String.Empty AndAlso RadComboBox1.IsCallBack) Then 
            command = New OleDbCommand("SELECT * FROM Customers", dbCon) 
        Else 
            command = New OleDbCommand("SELECT ContactName, ContactTitle FROM Customers WHERE ContactName LIKE '" + [text] + "%'", dbCon) 
        End If 
 
        Dim reader As OleDbDataReader 
        reader = command.ExecuteReader() 
 
        RadComboBox1.DataTextField = "ContactName" 
        RadComboBox1.DataValueField = "ContactTitle" 
        RadComboBox1.DataSource = reader 
        RadComboBox1.DataBind() 
 
        dbCon.Close() 
    End Sub 
 
    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click 
        Response.Write("Selected value is: " + RadComboBox1.Text) 
    End Sub 

Comments

  • Michael Strasser , Mar 13, 2007

    C# sample code please!

  • Telerik Admin , Mar 30, 2007

    We will add a C# sample asap.

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.