Click here to Skip to main content
15,847,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in vb.net web project, user forget to select a value in dropdownlist, so i need to give alert message if they forget to select, how to do? i don't know help me please
Posted
Comments
pugazhbala 19-Oct-11 3:31am    
i generate dynamically so i don't know how to give

Hi, you can implement a simple JavaScript validation function and call it on Button's OnClientClick method.
Below is the code.

aspx code
XML
<asp:DropDownList ID="dropDown1" runat="server" AutoPostBack="false">
    <asp:ListItem Text="None" />
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text3" />
</asp:DropDownList>

<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="javascript:return ValidateDropDown();" />


JavaScript Code
JavaScript
function ValidateDropDown() {
    var cmbID = "<%=dropDown1.ClientID %>";
    if (document.getElementById(cmbID).selectedIndex == 0) {
        alert("Please select a Value");
        return false;
    }
    return true;
}


Hope this will solve your problem..
 
Share this answer
 
v2
Comments
pugazhbala 19-Oct-11 3:32am    
i tried already required field validator, it not working
kiran dangar 19-Oct-11 4:18am    
Yes thats y i have provided JS validation in the answer... it will work fine... please click on "accept answer" if it helps you.
JQuery Geeks 24-Oct-11 1:35am    
nice
Member 9994226 10-May-13 13:28pm    
where I write javascript code
XML
<asp:DropDownList ID="dropDown1" runat="server" AutoPostBack="false">
    <asp:ListItem Text="None" selected="True"/>
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text3" />
</asp:DropDownList>

<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" text="*" controltovalidate="dropDown1" errormessage="Please Select Category" initialvalue="None" xmlns:asp="#unknown"></asp:requiredfieldvalidator>


<asp:Button ID="Button1" runat="server" Text="Submit"  />
 
Share this answer
 
Comments
raj ch 19-Oct-11 2:08am    
my vote of 5
pugazhbala 19-Oct-11 3:34am    
required field validator is not working i tried already any dynamic form validation
NeenaM 2-Apr-13 15:34pm    
Why in the above code you have used xmlns:asp="#unknown"
can you explain

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