How to disable items according to a condition in combo box?

7 posts, 2 answers
  1. Seda
    Seda avatar
    5 posts
    Member since:
    Jan 2014

    Posted 06 Jan 2014 Link to this post

     I'm developing an ASP.NET Web Application in C#.

    The user is selecting some listed values from radcombobox. Those values are being saved in Database. When the same user login and select the combo box, already selected values should be visible but shouldn't be get selected. I need to disable the values... Is there any options to do it.

    Note : I'm populating the combo-box items from a database table.

  2. Jon
    Jon avatar
    35 posts
    Member since:
    Sep 2013

    Posted 06 Jan 2014 Link to this post

    Hello Seda,

    Could you clarify if you are creating a Windows application or an ASP.NET Web application?  This forum is for the ASP.NET RadComboBox.  If you are creating a Windows application, please post your question here.

    Thanks,

    Jon
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Seda
    Seda avatar
    5 posts
    Member since:
    Jan 2014

    Posted 06 Jan 2014 Link to this post

    Hello Jon thanks for your  warning. I made the correction, Do you have any idea about the solution?
  5. Answer
    Jon
    Jon avatar
    35 posts
    Member since:
    Sep 2013

    Posted 06 Jan 2014 Link to this post

    I'm assuming you're using CheckBoxes because you said the user is selecting more than one item.

    I set up a sample project.  I just used an XML file for the datasource.

    <?xml version="1.0" encoding="utf-8" ?>
    <items>
     <Item Text="New York" Value="1" />
     <Item Text="Paris" Value="2" />
     <Item Text="London" Value="3" />
     <Item Text="Houston" Value="4" />
     <Item Text="Chicago" Value="5" />
     <Item Text="Atlanta" Value="6" />
    </items>

    Here is the XmlDataSource on the page:

    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="Cities.xml"/>


    Here is the RadComboBox on the page:
    <telerik:RadComboBox id="RadComboBox1"
        runat="server"
        OnDataBound="RadComboBox1_OnDataBound"
        CheckBoxes="True"
        datasourceid="XmlDataSource1"
        datatextfield="Text"
        datavaluefield="Value"/>


    Here is the RadComboBox1_OnDataBound event handler in the code behind:

    protected void RadComboBox1_OnDataBound(object sender, EventArgs e)
    {
        var citiesSelected = new List<string> {"2", "4"};
        var rcb = (RadComboBox) sender;
     
        foreach (RadComboBoxItem item in rcb.Items)
        {
            if (citiesSelected.Contains(item.Value))
            {
                item.Checked = true;
                item.Enabled = false;
            }
        }
    }

    So pretend that your user has already previously selected items with ID "2" and "4" (and this was stored in your database).  Then these items will show up checked but disabled.

    I think this is what you are looking for.  Let me know if you still have questions or I assumed something different than what you were asking.
  6. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 Jan 2014 Link to this post

    Hi 

    Please have a look into the following sample code snippet to achieve your scenario.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource1"
        DataTextField="Cityname"  EmptyMessage="--select--">
    </telerik:RadComboBox>
    <telerik:RadButton ID="RadButton1" runat="server" Text="Add Checked Item" OnClick="RadButton1_Click">
    </telerik:RadButton>

    C#:
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        String connstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstring);
        SqlDataAdapter adapter = new SqlDataAdapter();
        conn.Open();
        foreach (RadComboBoxItem item in RadComboBox1.CheckedItems)
        {
            item.Enabled = false;
            SqlCommand cmd = new SqlCommand("insert into CountryTable (CountryName) values('" + item.Text + "')", conn);
            cmd.ExecuteNonQuery();
        }
        conn.Close();
            RadComboBox1.ClearCheckedItems();
    }

    Let me know if you have any concern.
    Thanks,
    Shinu.
  7. Seda
    Seda avatar
    5 posts
    Member since:
    Jan 2014

    Posted 07 Jan 2014 Link to this post

    Thank you so much Jon,  the problem solved with your help

    Seda
  8. Seda
    Seda avatar
    5 posts
    Member since:
    Jan 2014

    Posted 07 Jan 2014 Link to this post

    Hi Shinu, thank you so much for your posting, the problem is solved 

    Thanks
    Seda
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017