Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to validate a DropDownList in an ASP.NET project and am trying to use JavaScript even though I have never used it before.
I'm using the following ASP declaration and code JavaScript:

ASP declaration:
ASP.NET
<asp:DropDownList ID="ddl1" runat="server" onprerender="ddl1_PreRender" ValidationGroup="AddNewCollection">
</asp:DropDownList>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Choose Type!"
ControlToValidate="ddl1"  ForeColor="Red" ValidationGroup="AddNewCollection" ClientValidationFunction="clientSideCheckValue"></asp:CustomValidator>>


JavaScript code:

JavaScript
<script type="text/javascript">
    function clientSideCheckValue(source, args) 
     {
       var result1 = args.Value;   
       var rsult2 = document.getElementById("ddl1").value;
       if (result2 == null) {
          args.IsValid = false;
          return true;
       }
      args.IsValid = true;
    }
</script>


I have two questions about code mentioned above:

1.Why when I'm printing this row's code var result1 = args.Value; intelisense dosen't give me option to choose "Value" extension.

2.At this line of JavaScript code var rsult2 = document.getElementById("ddl1").value;

I get this error message Microsoft JScript runtime error: Object required.Have you got any idea why i meet this problem and how can i fix it?

Thank you in advance.
Posted
Comments
[no name] 1-Feb-12 21:12pm    
BTW,
var rsult2 = document.getElementById("ddl1").value;
if (result2 == null) {

You're using rsult2 and result2, typo?

1. for code
JavaScript
var result1 = args.Value 
(intelisense not supported)
2. use
JavaScript
var rsult2 = document.getElementById('<%=ddl1.ClientID %>').value;
instead of
JavaScript
var rsult2 = document.getElementById("ddl1").value;
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 2-Feb-12 1:57am    
Added pre tag
Hi,

ddl does not works like this.

use below code for access ddl in JS.

JavaScript
ddlObj = document.getElementById('<%=ddl1.ClientID %>');
ddlVal = ddlObj.options[ddlObj.selectedIndex].value;
 
Share this answer
 
1.Why when I'm printing this row's code var result1 = args.Value; intelisense dosen't give me option to choose "Value" extension.
Here[^] is a nice explanation on your question.

2.At this line of JavaScript code var rsult2 = document.getElementById("ddl1").value;
If you are using user controls, the name of the dropdownlist will definitely change after your page has been rendered to the browser. Try to view the source of the page on your browser to verify the name of the dropdownlist if it has indeed changed.
 
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