locked
Stop adding item in Combo Box RRS feed

  • Question

  • User-1466065877 posted

    Hi,

    I have a combo box with some item. However, when I input an invalid item (that is not existed in the list), it postbacks and add this invalid item to the list.

    I want to disable the adding item function but cannot find any properties / behaviour can do it.

    Is there any method to stop the adding function? Or, is it possible to check the validation of the inputted value before it autopostbacks?

    Thanks.

    Thursday, May 15, 2014 11:51 PM

Answers

  • User1918509225 posted

    Hi Phc,

    If you are using Ajax Control toolkit ComboBox,I suggest that you can try to set AppendDataBoundItems as false just like my example below:

    Page code :

      <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
             <asp:Label   
                ID="Label1"  
                runat="server"  
                Font-Size="Large"  
                ForeColor="Green"  
                >  
            </asp:Label>  
            <br /><br />  
            <ajaxToolkit:ComboBox   
                ID="ComboBox1"   
                runat="server"   
                DropDownStyle="DropDown"   
                AutoCompleteMode="None"  
                CaseSensitive="false"  
                RenderMode="Block"  
                AutoPostBack="false"  
                Font-Names="Comic Sans MS"  
                Font-Size="Medium"  
                >  
                <asp:ListItem Text="White"></asp:ListItem>  
            </ajaxToolkit:ComboBox>  
    
            <asp:Button   
                ID="Button2"  
                runat="server"  
                OnClick="Button2_Click"  
                Text="Populate ComboBox: AppendDataBoundItems False"  
                Height="45"  
                Font-Bold="true"  
                ForeColor="DodgerBlue"  
                />  
        </div>

    Server code:

    {
               
            }
    
            void ComboBoxDataBind()
            {
                string[] Colors = { "Red", "Green", "Blue" };
                ComboBox1.DataSource = Colors;
                ComboBox1.DataBind();
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                ComboBox1.AppendDataBoundItems = false;
                ComboBox1.SelectedIndex = -1; 
                ComboBoxDataBind();
                Label1.Text = "AppendDataBoundItems Now: False";
            }
    

    Hope it can help you.

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 19, 2014 3:18 AM
  • User555306248 posted

    DropDownStyle - Determines whether the user is allowed to enter text that does not match an item in the list, and whether the list is always displayed. If "DropDownList" is specified, users are not allowed to enter text that does not match an item in the list. When "DropDown" (the default value) is specified, any text is allowed. If "Simple" is specified, any text is allowed and the list is always displayed regardless of the AutoCompleteMode property value.

    http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/ComboBox/ComboBox.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 19, 2014 11:59 PM

All replies

  • User-1716253493 posted
    Don't bind SelectValue directly. Use code behind to select by check if(ddl.items.FindByValue(value)!=null)
    Friday, May 16, 2014 1:04 AM
  • User-1466065877 posted

    I'm sorry, Can you description this method more details? If I do not databind the value, how can I set the combo box list data?

    Friday, May 16, 2014 2:31 AM
  • User1918509225 posted

    Hi Phc,

    If you are using Ajax Control toolkit ComboBox,I suggest that you can try to set AppendDataBoundItems as false just like my example below:

    Page code :

      <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
             <asp:Label   
                ID="Label1"  
                runat="server"  
                Font-Size="Large"  
                ForeColor="Green"  
                >  
            </asp:Label>  
            <br /><br />  
            <ajaxToolkit:ComboBox   
                ID="ComboBox1"   
                runat="server"   
                DropDownStyle="DropDown"   
                AutoCompleteMode="None"  
                CaseSensitive="false"  
                RenderMode="Block"  
                AutoPostBack="false"  
                Font-Names="Comic Sans MS"  
                Font-Size="Medium"  
                >  
                <asp:ListItem Text="White"></asp:ListItem>  
            </ajaxToolkit:ComboBox>  
    
            <asp:Button   
                ID="Button2"  
                runat="server"  
                OnClick="Button2_Click"  
                Text="Populate ComboBox: AppendDataBoundItems False"  
                Height="45"  
                Font-Bold="true"  
                ForeColor="DodgerBlue"  
                />  
        </div>

    Server code:

    {
               
            }
    
            void ComboBoxDataBind()
            {
                string[] Colors = { "Red", "Green", "Blue" };
                ComboBox1.DataSource = Colors;
                ComboBox1.DataBind();
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                ComboBox1.AppendDataBoundItems = false;
                ComboBox1.SelectedIndex = -1; 
                ComboBoxDataBind();
                Label1.Text = "AppendDataBoundItems Now: False";
            }
    

    Hope it can help you.

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 19, 2014 3:18 AM
  • User555306248 posted

    DropDownStyle - Determines whether the user is allowed to enter text that does not match an item in the list, and whether the list is always displayed. If "DropDownList" is specified, users are not allowed to enter text that does not match an item in the list. When "DropDown" (the default value) is specified, any text is allowed. If "Simple" is specified, any text is allowed and the list is always displayed regardless of the AutoCompleteMode property value.

    http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/ComboBox/ComboBox.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 19, 2014 11:59 PM