RadCombobox : Insert Item After DataBinding

6 posts, 0 answers
  1. Muhammad
    Muhammad avatar
    87 posts
    Member since:
    Oct 2012

    Posted 31 Jan 2011 Link to this post

    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
  2. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 02 Feb 2011 Link to this post

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

    Posted 18 Feb 2011 Link to this post

    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.
  5. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 27 Feb 2011 Link to this post

    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!
  6. Frankie
    Frankie avatar
    9 posts
    Member since:
    Jan 2008

    Posted 14 Nov 2011 Link to this post

    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
  7. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 15 Nov 2011 Link to this post

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