Drop Down List Control

Last post 01-08-2008 6:53 AM by sureshkumar.veesam. 11 replies.

Sort Posts:

  • Re: Drop Down List Control

    01-05-2008, 9:07 AM
    Answer
    • Loading...
    • gunteman
    • Joined on 07-11-2007, 12:57 PM
    • Norrköping, Sweden
    • Posts 2,327

    What you want is a select with optgroup tags in it, which is something the DropDownList doesn't offer. You could create your own control or maybe use this: http://www.codeplex.com/SharpPieces.  

    -- "Mark As Answer" if my reply helped you --
  • Re: Drop Down List Control

    01-07-2008, 3:03 AM

    Hi sureshkumar, 

         You can use requiredFieldValidator to get the job done. Here is my example code for you.

    <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>--select--</asp:ListItem>
                <asp:ListItem>a</asp:ListItem>
                <asp:ListItem>b</asp:ListItem>
                <asp:ListItem>c</asp:ListItem>
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"
                ErrorMessage="RequiredFieldValidator" InitialValue="--select--"></asp:RequiredFieldValidator>

    Please pay attention to the words that are in bold font.

    This DropDownLIst will show "--select--" to the user as the first item, but the user can't pass the validation until he select an item of a, b or c.

    Regards,

    Ivan.

  • Re: Drop Down List Control

    01-07-2008, 3:21 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 3:47 PM
    • Cebu Philippines
    • Posts 7,422

    Based on my experience, All list of data in a dropdownlist is selectable if you set autopostback to true.. Why not validate in your codes that if the user selects the Programming and design value then do not execute any codes else do something....

        protected void Page_Load(object sender, EventArgs e)
        { 

          if (!Page.IsPostBack)
            {
                DropDownList1.Items.Add("--Programming & Design--");
                for (int i = 0; i < 2; i++)
                {
                    string a = "test";
                    DropDownList1.Items.Add(a);
                }

            }

        } 

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
       
            if (DropDownList1.SelectedItem.Text != "--Programming & Design--");

            {

            //do something here

            }
          
        }

     

    If somebody knows how to make a certain data in the DDL not selectable then please let me know.. I'm also willing to know about it.... 

    Cheers,
    Vincent Maverick Durano

    "Life is like music; it must be composed by ear, feeling, and instinct, not by rule..."

  • Re: Drop Down List Control

    01-07-2008, 4:20 AM
    • Loading...
    • nital soni
    • Joined on 11-19-2007, 5:58 AM
    • Ahmedabad
    • Posts 228

    Hi,

         U can achieve this using client side javascript

       Try this........

     

    ASP

    <asp:DropDownList ID="DDLA" runat="server" AutoPostBack="True" onselectedindexchanged="DDLA_SelectedIndexChanged">

    C#

    protected void Page_Load(object sender, EventArgs e)
     {

        DDLA.Items.Add(new ListItem("--select--", "-1"));

        DDLA.Attributes.Add("onchange", "return ValidateDDL();");

    }

    protected void DDLA_SelectedIndexChanged(object sender, EventArgs e)
    {
            //Ur server side Code here .......if any
    }
     

    JS

        function ValidateDDL()
            {
                var DDL = document.getElementById("<%= DDLA.ClientID %>");
                if(DDL.value == -1)
                {
                    alert('Invalid Value');
                    DDL.focus();
                    return false;
                }
                else
                {

                     __doPostBack(DDL.id,'');
                    return true;
                }
            }

    Nital Soni

    Don't forget to MARK AS ANSWER if the post is helpful to you.
  • Re: Drop Down List Control

    01-07-2008, 6:25 AM
    • Loading...
    • jocker_wow
    • Joined on 01-19-2007, 8:20 AM
    • Egypt
    • Posts 81

    You can use the compare validator and set the validation to be NotEqual to the desired value.

     

    <asp:DropDownList ID="DropDownList1" runat="server">

    <asp:ListItem Value="Select one:">Select one:</asp:ListItem>

    <asp:ListItem>a</asp:ListItem>

    <asp:ListItem>b</asp:ListItem>

    <asp:ListItem>c</asp:ListItem>

    </asp:DropDownList>

     

    <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="DropDownList1"

    ErrorMessage="Please select one!" Operator="NotEqual" ValueToCompare="Select one:"></asp:CompareValidator>

    <asp:Button ID="Button1" runat="server" Text="Button" />

     

    Hope that helps!

    Regards...
    M.Nagieb
    Jocker_Wow
  • Re: Drop Down List Control

    01-07-2008, 7:55 AM
    • Loading...
    • prah.NET
    • Joined on 11-23-2007, 4:53 PM
    • Delhi
    • Posts 58

    Dear sureshkumar.veesam,

    You can use custom validator if you don't want to select a particular listitem from a drop downlist box...

     

    a sample is here..

     

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">

    function checkVal(sender, args)

    {

     

    if(args.Value=="pg")

    {

    args.IsValid=
    false;

    alert(args.Value);

    alert(args.IsValid);

    }

    else

    {

    args.IsValid=
    true;

    alert(args.IsValid);

    alert(args.Value);

    }

    }

    </script> </head>

    <body>

    <form id="form1" runat="server">

    <asp:DropDownList ID="ddl" runat="server">

    <asp:ListItem>

    Select

    </asp:ListItem>

    <asp:ListItem>pg</asp:ListItem>

    <asp:ListItem>

    A

    </asp:ListItem>

    <asp:ListItem>

    B

    </asp:ListItem>

    </asp:DropDownList>

    <asp:CustomValidator ID="abc" runat="server" ControlToValidate ="ddl" ClientValidationFunction="checkVal" ErrorMessage=" you cannot select this"></asp:CustomValidator>

    </form>

    </body>

    </html>

    Thanks and regards

    prahlad kumar sharma

    Prahlad Kumar Sharma
  • Re: Drop Down List Control

    01-08-2008, 1:52 AM

     Thanks to All, for your Replies.

    But I don't want  to validate the Dropdownlist.

    please once check this site http://www.naukri.com/ . In this site, under jobs category, you will find one dropdownlist under FunctionalArea heading. In that dropdownlist, we cannot select
    Top Categories item. I want in that way, Now tell, how to do that..

    Thanks in Advance 

  • Re: Drop Down List Control

    01-08-2008, 2:20 AM
    • Loading...
    • gunteman
    • Joined on 07-11-2007, 12:57 PM
    • Norrköping, Sweden
    • Posts 2,327

    See my first reply. You have to create optgroups. 

    -- "Mark As Answer" if my reply helped you --
  • Re: Drop Down List Control

    01-08-2008, 3:02 AM
    Answer
    • Loading...
    • jocker_wow
    • Joined on 01-19-2007, 8:20 AM
    • Egypt
    • Posts 81

    sureshkumar.veesam:

     Thanks to All, for your Replies.

    But I don't want  to validate the Dropdownlist.

    please once check this site http://www.naukri.com/ . In this site, under jobs category, you will find one dropdownlist under FunctionalArea heading. In that dropdownlist, we cannot select
    Top Categories item. I want in that way, Now tell, how to do that..

    Thanks in Advance 

     hi sureshkumar,

    I hope I've understood u well,

    By default the top item in the DropDownList control is selected so u cannot select it unless you've changed the selection. In the site you've provided, it thoughs an error message - by javascript or by asp.net validation summary with the validation control.

    To set this up using the validation summary, beside the validation control create a validation summary, and set the ShowMessageBox property to True:

    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"

    ShowSummary="False" />

    I hope that help you.

    Regards...
    M.Nagieb
    Jocker_Wow
  • Re: Drop Down List Control

    01-08-2008, 3:33 AM
    Answer

    Hi sureshkumar,

        

    sureshkumar.veesam:

     Thanks to All, for your Replies.

    But I don't want  to validate the Dropdownlist.

    please once check this site http://www.naukri.com/ . In this site, under jobs category, you will find one dropdownlist under FunctionalArea heading. In that dropdownlist, we cannot select
    Top Categories item. I want in that way, Now tell, how to do that..

    Thanks in Advance 

          In this case, qunteman's suggest is quite right. Use option group tag <optgroup> will help you out. It's very easy to use, simpley html tag.

    for example:

    <select>

    <option>---select---<option>

    <optgroup label="ABC">

    <option value="A">A</option>

    <option value="B">B</option>

    <option value="C">C</option>

    </optgroup>

    </select>

    Here are two links for you to get a better understanding of it.

    http://www.w3schools.com/tags/tag_optgroup.asp

    http://htmlhelp.com/reference/html40/forms/optgroup.html

    Regards,

    Ivan.

  • Re: Drop Down List Control

    01-08-2008, 6:53 AM

     Thanks for your replies.

Page 1 of 1 (12 items)