Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 7-Dec-12 2:18am
Edited 10-Dec-12 22:41pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use required field validator & set its Initialvalue property to 0 th postion value of dropdown. (here --Select-- is that value)
 
<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>
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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 :
============================
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
if (DropDownList1.SelectedItem.Text == "Select")
       {
Lable.Text="Select any value";
       }
 
   }
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

suppose your drop down list id is ddlItems.
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;
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

<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");        
    }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 485
1 OriginalGriff 310
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100