Click here to Skip to main content
14,982,408 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends ....

I need to write Javascript for dropdown list .

For selected value not equal to 'Select'

ASP.NET
<asp:DropDownList ID="dropstatus" runat="server" CssClass="dropdown">
                            <asp:ListItem Text="Select"></asp:ListItem>
                            <asp:ListItem Text="Open"></asp:ListItem>
                            <asp:ListItem Text="Fixed"></asp:ListItem>
                            <asp:ListItem Text="Processing"></asp:ListItem>
                        </asp:DropDownList>




C#
script type="text/javascript">
    function validate() {

        if (document.getElementById('<%=txtcomp.ClientID%>').value == "") {
            alert("Name Feild can not be blank");
            document.getElementById("<%=txtcomp.ClientID%>").focus();
            return false;

        }

        var txt = document.getElementById('<%=dropstatus.ClientID %>')
        if ((txt.options[txt.selectedIndex].text) == "Select") {
            alert("Password Feild can not be blank");
            document.getElementById("<%=dropstatus.ClientID%>").focus();
            return false;

        }

    }

    </script>

The Above code is correct or Wrong.

Please help me
Posted
Updated 30-May-12 2:00am
v3
Comments
Rahul Rajat Singh 30-May-12 6:55am
   
looks fine to me. are you getting any problem?
Arul R Ece 30-May-12 7:00am
   
I am getting the below error.
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
member60 30-May-12 7:19am
   
see my edited answer
Arul R Ece 30-May-12 7:59am
   
Till get same error

You can register the control in your code behind as mentioned below:

C#
Page.RegisterHiddenField("hdnDropDownList", dropstatus.ClientID);


And use it in your Javascript Code:

JavaScript
var _dropDown = document.getElementById('hdnDropDownList').value;
if (document.getElementById(_dropDown).selectedIndex =="Select") {
    alert("Please select City");
    document.getElementById(_dropDown).focus();
    return false;
}
   
the code you are posted works well . The problem could occur when you use Value proerty in ListItem , such as :
ASP.NET
<asp:listitem value="0" text="Select" xmlns:asp="#unknown"></asp:listitem>


then you can have Text by :
JavaScript
var txt=document.getElementById('<%=dropstatus.ClientID %>')
if((txt.options[txt.selectedIndex].text)=="Select")
//further code

or
C#
if((txt.options[txt.selectedIndex].value)=="Select")
//further code


To fix the error you described in comments follow:
1.Remove the part which has server tags and place it somewhere else if you want to add dynamic controls from code behind. You can add this javascript code to the html body it will solve your problem.

2. You can use <%# instead of <%=

This changes the code block from a Response.Write code block to a databinding expression. Since <%# … % > databinding expressions aren’t code blocks, the CLR won’t complain.
   
v3
Comments
Arul R Ece 30-May-12 8:20am
   
Ya .. i used above mentioned....

But same error.
C#
if (document.getElementById("<%=dropstatus.ClientID %>").selectedIndex =="Select") {
            alert("Please select City");
document.getElementById("<%=dropstatus.ClientID%>").focus();
            return false;
        }




try this.

put SelectedIndex instead of value.

if this help you than accept solution so other can view this.
   
Comments
Arul R Ece 30-May-12 7:09am
   
Hi Friend,...
I am getting the below error.
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).] System.Web.UI.ControlCollection.Add(Control child) +8680983 AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) in C:\Users\wahlind\Desktop\Code\ControlToolkit\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:249 AjaxControlToolkit.ExtenderControlBase.OnPreRender(EventArgs e) in C:\Users\wahlind\Desktop\Code\ControlToolkit\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:352 System.Web.UI.Control.PreRenderRecursiveInternal() +80 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
member60 30-May-12 7:22am
   
have a look at my edited answer.
markovl 30-May-12 7:18am
   
This is just wrong. The selectedIndex property of the select object is a number, it's not a string, so this condition will always evaluate to false.

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