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

RadCombobox : Insert Item After DataBinding

5 Answers 493 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Muhammad
Top achievements
Rank 1
Muhammad asked on 31 Jan 2011, 06:26 PM
I have a radcombobox and in that I have placed one checkbox after databinding I want to insert one more checkbox inside that radcombobox to select all items.

<telerik:RadComboBox ID="cmbRoleName" EmptyMessage="- please select group -" runat="server"
                                     AutoCompleteSeparator="true" HighlightTemplatedItems="true"
                                    AutoPostBack="True" AllowCustomText="true" OnClientDropDownClosed="onDropDownClosing">
                                    <Items>
                                        <telerik:RadComboBoxItem Value="0" Text="-- Select All Users --" />
                                    </Items>
                                    <ItemTemplate>
                                        <table border="0" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td>
                                                    <asp:CheckBox runat="server" ID="chkRole" Text='<%#DataBinder.Eval(Container.Dataitem,"RoleName")%>'
                                                        Onclick="chkBox_Click(this)" />
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </telerik:RadComboBox>

I have tried to do this on server side like this but the problem is that two checkboxes are appearing one with text and without text. Any Help would be really appreciated.

Protected Sub CreateSelectAllUsersCheckBox()
        Dim chkSelectAllUsers As New CheckBox
        chkSelectAllUsers.Text = "Select All Users"
        chkSelectAllUsers.ID = "chk1"
        Dim radComboBoxItem As New RadComboBoxItem
        radComboBoxItem.Text = "Select All Users"  
radComboBoxItem.Controls.Add(chkSelectAllUsers)     
        cmbRoleName.Items.Insert(0, radComboBoxItem)
    End Sub

5 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 02 Feb 2011, 11:56 AM
Hi Muhammad,

You can resolve this by calling DataBind on radComboBoxItem after you add it to the Items collection of the RadComboBox.

Regards,
Simon
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Muhammad
Top achievements
Rank 1
answered on 18 Feb 2011, 05:59 PM
I don't understand how it can be done ... If you can provide me some code so I can easily understand because I am just start using telerik controls.
0
Simon
Telerik team
answered on 27 Feb 2011, 03:34 PM
Hi Muhammad,

The DataBind method is a method of the RadComboBoxItem class, so you can directly invoke it on a RadComboBoxItem object.

Here is how you need to modify your code to make this work:
Protected Sub CreateSelectAllUsersCheckBox()
        Dim chkSelectAllUsers As New CheckBox
        chkSelectAllUsers.Text = "Select All Users"
        chkSelectAllUsers.ID = "chk1"
        Dim radComboBoxItem As New RadComboBoxItem
        radComboBoxItem.Text = "Select All Users"  
    radComboBoxItem.Controls.Add(chkSelectAllUsers)    
        cmbRoleName.Items.Insert(0, radComboBoxItem)
 
        radComboBoxItem.DataBind()
    End Sub

I hope this helps.

Greetings,
Simon
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Frankie
Top achievements
Rank 2
answered on 14 Nov 2011, 07:36 PM
Hi, I have my code as below, however, the inserted item after databind() is still not in the dropdown.. anything wrong with my code?

private DataTable GetDataTable(string queryString)
{
    String ConnString = ConfigurationManager.ConnectionStrings["NxtFinancialMain_ConnString"].ConnectionString;
    SqlConnection MySqlConnection = new SqlConnection(ConnString);
    SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();
    MySqlDataAdapter.SelectCommand = new SqlCommand(queryString, MySqlConnection);
 
    DataTable myDataTable = new DataTable();
    MySqlConnection.Open();
    try
    {
        MySqlDataAdapter.Fill(myDataTable);
    }
    finally
    {
        MySqlConnection.Close();
    }
 
    return myDataTable;
}
 
 
protected void ParentId_DataBinding(object sender, System.EventArgs e)
{
    //
    ddlBox3.Items.Insert(0, new RadComboBoxItem("Root", "0"));
 
    // the dropdown list is now only with SBG... BusinessUnitTypeId = 2
    ddlBox3.DataSource = GetDataTable("SELECT [Id], [Code], [Name] FROM [BusinessUnits] WHERE [BusinessUnitTypeId]=2");
    ddlBox3.DataTextField = "Code";
    ddlBox3.DataValueField = "Id";
    ddlBox3.DataBind();
    //ddlBox3.SelectedIndex = 0;
 
}

Regards,
Frank
0
Simon
Telerik team
answered on 15 Nov 2011, 06:34 PM
Hello Frankie,

Calling RadComboBox.DataBind() clears all previously existing items in the control. You can preserve them if you set the RadComboBox.AppendDataBoundItems property to true.

Best wishes,
Simon
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
ComboBox
Asked by
Muhammad
Top achievements
Rank 1
Answers by
Simon
Telerik team
Muhammad
Top achievements
Rank 1
Frankie
Top achievements
Rank 2
Share this question
or