Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a drop down list in my project. That contains a lot of items selected from database. The first item in drop down list is "select" and remaining items come from database. I want to apply validation when the first item is selected. I'm using ASP.NET(C#).
How can i do this??? Please help me...
Posted
Updated 10-Dec-12 21:41pm
v3

C#
var ObjDrp1 = document.forms[0]['ctl00_ContentPlaceHolder1_ddlGroupname'];
    var Idx = parseInt(ObjDrp1.selectedIndex);
if (Idx == 0)
   {
       alert ('Please Select Type ...!!!');
       ObjDrp1.style.backgroundColor='LemonChiffon';
       ObjDrp1.focus();
       return false;
   }

This is the way To validate by javascript
 
Share this answer
 
Use required field validator & set its Initialvalue property to 0 th postion value of dropdown. (here --Select-- is that value)

C#
<asp:dropdownlist id="ddlstatus" runat="server"   >

<asp:listitem>--Select--</asp:listitem> 
</asp:dropdownlist>

<asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" errormessage="Please select status" controltovalidate="ddlstatus" validationgroup="btnAdd" initialvalue="--Select--" ></asp:requiredfieldvalidator>
 
Share this answer
 
v3
Hi,

using Javascript : Clientside validation
=========================================

<script type="text/javascript">
function validate()
{
var ddllist= document.getElementById(<%=DropDownList1.ClientID %>).value;
if (ddllist.options[ddllist.selectedIndex].value== 'Select') {
alert('Select a value');
return false;
}
}
</script>

call this validate function onclick of dropdown or onclientclick of button

---------------------------------------------------------------------------------

On Server side validation :
============================
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
if (DropDownList1.SelectedItem.Text == "Select")
       {
Lable.Text="Select any value";
       }

   }
 
Share this answer
 
v3
suppose your drop down list id is ddlItems.
C#
protected bool ValidateItems(){
    //Dropdwon list selected index property will give you currently selected item index
    // if user have selected default value in you case it is "Select" then that property will return 0 because you are inserting default value at positioning 0 in your dropdownlist items. so only you need to check the current selected item index based on your requirement you can validate your selection.

    if(ddlItems.SelectedIndex > 0)
       retrurn true;
    
    retrun false;
}
 
Share this answer
 
XML
<script type="text/javascript" language="javascript">
        function Validate() {
 var country = document.getElementById("ddlCountry").selectedIndex;
if (country <= 0) {
                alert("Please choose country");
                return false;
            }
}
<asp:button id="btnSubmit" onclientclick="return Validate()" text="Submit" runat="server" onclick="btnSubmit_Click" xmlns:asp="#unknown" />

page.cs file

 string Conn = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillCountrys();           
        }
    }

protected void FillCountrys()
    {
        SqlConnection cnn = new SqlConnection(Conn);
        SqlDataAdapter ada = new SqlDataAdapter("select * from tbl_Countrys", cnn);
        DataSet ds = new DataSet();
        ada.Fill(ds);
        ddlCountry.DataSource = ds;
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryCode";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, "Select Country");        
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900