Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox,button and a dropdownlist.
C#
dropdownlist item
select the item
card1
card2
card3

I click the button without selection of item,"select the item" it is selected automatically.I do not want it.
Posted
Updated 6-Dec-11 23:31pm
v3

I think you want user must select any of the options except select the item.
for that you need to validate your DropDownList from client side/server side.

client side validation
JavaScript
<script>
var dropdownvalue=document.getElementById("ClientIDofYourDropDownList").value;
if(dropdownvalue=="select the item")
{
alert("You need to select any of the card");
document.getElementById("ClientIDofYourDropDownList").focus();
return;
}
</script>

here we are forcing the user to select one option of the DropDownList.

Server side validation

SQL
if (IDofYourDropDownList.SelectedValue == "select the item")
        {
            Label.Text = "You need to select any of the card";
            return;
        }


Hop it helps.
 
Share this answer
 
By default a dropdown list is selecting the first item in the list (in all browsers), that's why you (we) are using the first item as "select the item" to notice the user that he needs to select an item and to give us a way of validating the input. The first item from your list is a "dummy" item that does not represent anything for the list, but just offers you a way to know if the user selected anything meaningful for you.

What everyone is doing is checking if the selected item of the list is different from the "dummy" item, that way we know the input is valid for that list and we can continue.

eg:
The drop down list:
- as asp.net control:
ASP.NET
<asp:dropdownlist id="ddlItems" runat="server" xmlns:asp="#unknown">
    <asp:listitem value="-1" text="select the item"></asp:listitem>
    <asp:listitem value="1" text="card 1"></asp:listitem>
    <asp:listitem value="2" text="card 2"></asp:listitem>
    <asp:listitem value="3" text="card 3"></asp:listitem>
</asp:dropdownlist>

- or as simple html control:
ASP.NET
<select id="ddlItems" runat="server">
    <option value="-1">select the item</option>
    <option value="1">card 1</option>
    <option value="2">card 2</option>
    <option value="3">card 3</option>

</select>


and the condition on your button could be something like this:

C#
if (ddlItems.SelectedValue == "-1")
{
    //you selected "select the item" option - the "dummy" element
    //do something here
}


And if you still do not want it as you said, than maybe you need to remove it from the list.
 
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