Hey,
I have lots of drop down list in my project . Here I need use "select" on page load to the drop down list and also when I could not select anything from the list and press submit button, a null value should be send to the data base. Below is my code:
ddlCountry.Items.Insert(0, new ListItem(string.Empty, string.Empty));
ddlCountry.SelectedIndex = 0;
In this a blank is shown in drop down list, I need "select" word on that instead.

Jignesh TrivediPosted Apr 22, 2014, 3:53 AM
hi,
ListItem Class has four constructor.
you are using ListItem(String, String) type of constructor
here first argument is text and second is value. so you are passing blank value in text argument.
try...
ddlCountry.Items.Insert(0, new ListItem("--Select--", string.Empty));
hope this will help you.
Jignesh TrivediPosted Apr 22, 2014, 8:22 AM
can we write conditional statement for same?
Like
var optiontext = parm.options[parm.selectedIndex].text
if( optiontext != "select")
{
document.getElementById('lblCountry').innerHTML = parm.options[parm.selectedIndex].text
}
else
{
document.getElementById('lblCountry').innerHTML = "";
}
Renjith V SPosted Apr 22, 2014, 8:15 AM
var parm = document.getElementById("ddlCountry");
document.getElementById('lblCountry').innerHTML = parm.options[parm.selectedIndex].text
Jignesh TrivediPosted Apr 22, 2014, 8:07 AM
Can u please share the code when you set label text?
you can also write code on dropdown selected change event.
Renjith V SPosted Apr 22, 2014, 8:02 AM
Your reply is correct. But the problem I faced is that : I have display this drop down list in a label, so that at the the time of not selecting any value in drop down list and enter the submit button. At that time actual process is that nothing is selected and the label should be blank. But here the label is displayed as --select--. I need blank instead of --select--.
Renjith V SPosted Apr 22, 2014, 4:54 AM
Abhay ShankerPosted Apr 22, 2014, 4:09 AM
ddlCountry.Items.Insert(0, new ListItem("--Please Select--", "-1"));
and Check
condition ddlCountry.selectedvalue is equal -1 then save null value to database
Anupam SinghPosted Apr 22, 2014, 3:49 AM
Hope this will help.
Srikanth ReddyPosted Apr 22, 2014, 3:43 AM
ddlCountry.Items.Insert(0,"--Select Country--");