Click here to Skip to main content
15,891,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i have several dropdown lists in my asp.net c# project.

i have set default value say "choose one" in drop down list and aslo set a validator for that dropdownlist.

my problem is on buttonclick event my validator does not fire for the dropdownl list and also the default value "choose one" saved in my database if i dont select any value.

i cant figure out how to prevent this default value to store in my databse and also fire validator when the value selected is default value "choose one".

thanks in advance
Posted
Comments
arathi_suresh 10-Aug-11 4:52am    
Show your code
koolprasad2003 11-Aug-11 5:22am    
It's a validation customisation. on click of submit button u need to check "choose one" should not be selected.

<asp:DropDownList ID="inquiry_type" runat="server" CausesValidation="True" ValidationGroup="g1" Width="180" TabIndex="1">

<asp:ListItem Value="0">--Choose one--</asp:ListItem>
<asp:ListItem Value="1">Option 1</asp:ListItem>
<asp:ListItem Value="2">Option 2</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="inquiry_type"ErrorMessage="Inquiry type required" ValidationGroup="g1" Style="display:none" InitialValue="0">
</asp:RequiredFieldValidator>


Add a value field to each list item. And in the required fiels validator set the Initial Value to 0. This will help you to validate your dropdown and the "Choose one..." will not be sent to DB.


Hope this will help you!!!
 
Share this answer
 
set the datasource of dropdownlist after that write a code below

dropDownList.Items.Insert(0, new ListItem("--choose one--", "0"));


example

SQL
dropDownList.DataSource = List.ToString();
dropDownList.DataBind();

dropDownList.Items.Insert(0, new ListItem("-- choose one--", "0"));
 
Share this answer
 
If it is a RequiredFieldValidator, set it's InitialValue property to "choose one". This way, validator will show error if nothing is selected in the dropdown. If you are using any other validator, let us know.
 
Share this answer
 
C#
void BinddatatoCombo()
{
                comboBox.DataSource = null;

                comboBox.DisplayMember = "Name";//Display in Combo box
                comboBox.ValueMember = "ID";

                DataSet myds = null;
                myds = GetDataSet("SELECT * FROM table");//get data set from database
                DataRow myRow = myds.Tables[0].NewRow();

                LedgerRow["ID"] = "000";
                LedgerRow["Name"] = "---Choose One---";

                myds.Tables[0].Rows.Add(myRow);

                DataView myDataView = myds.Tables[0].DefaultView;
                myDataView.Sort = "ID ASC";

                comboBox.DataSource = myDataView;
}



for an example sql table like this

Table_Name

ID | Name


thnks..
 
Share this answer
 
Comments
Soft009 10-Aug-11 5:09am    
you can check the value if you not select any from the combo box..

if(comboBox.selectedvalue == "000"
{
messageBox.show("Select value");
//something you want to do
}
Method 1
C#
ListItem itm = new ListItem();
itm.Text = "";
itm.Value = "-1";
itm.Selected = true;
DropDownList1.Items.Insert(0, itm);


Method 2 (dat I prefer)
C#
DropDownList1.Items.Add(new ListItem("- Select -", "-1"));


Also you can check

C#
if(DropDownList1.SelectedIndex=-1)
{
//whateve you want
}
else
{
//whateve you want
}
 
Share this answer
 
v2

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