Click here to Skip to main content
15,844,387 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi ..

I am using java script for validate drop down control..

but getting Error msg.

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Update_My_task.aspx.cs" Inherits="Update_My_task" %>
<%@ Register Src="~/Include/UserMenu.ascx" TagName="Usermenu" TagPrefix="uc2" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <title></title>
   
<script type="text/javascript" >
    function val() {
        if (document.getElementById("<%=dropstatus.ClientID%>").value == "Select") {
            alert("Please select the Status");
            document.getElementById("<%=dropstatus.ClientID%>").focus();
            return false;
        }
    }
</script>
</head>
<body>
    <form id="form1"  runat="server">
    <AjaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1"  runat="server">
    </AjaxToolkit:ToolkitScriptManager>
    <div>
        <table align="center" >
            <tr>
                <td>
                    <uc2:Usermenu ID="üserMenu"  runat="server" />
                </td>
            </tr>
        </table>
        <table width="50%" align="center">
            <tr>
                <th colspan="2" align="center" style="font-family: 'Palatino Linotype'; font-size: medium;
                    color: #FF0000">
                    Update Task
                </th>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lbltaskid" Text="Task ID" CssClass="lbl" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <asp:DropDownList ID="dropid" CssClass="dropdown" runat="server" AutoPostBack="True"
                        OnSelectedIndexChanged="dropid_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lbltask" Text="Task" CssClass="lbl" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <asp:TextBox ID="txttask" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lblreq" CssClass="lbl" Text="Requested For" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <asp:Label ID="lblreqfor" Text="" CssClass="lbl" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lblasn" Text="Assign To" CssClass="lbl" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <asp:Label ID="lblassto" Text="" CssClass="lbl" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lblDate" Text="Assigned date" CssClass="lbl" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <asp:Label ID="lblassdate" Text="" CssClass="lbl" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lblcomptime" Text="Estimated time" CssClass="lbl" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <asp:Label ID="lblest" Text="" CssClass="lbl" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lblcomp" Text="Completed time" CssClass="lbl" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <asp:TextBox ID="txtcomp" CssClass="txtbox" runat="server"></asp:TextBox>
                    <AjaxToolkit:CalendarExtender ID="calExtender3"  runat="server" PopupButtonID="btnDate3"
                                    TargetControlID="txtcomp">
                                </AjaxToolkit:CalendarExtender>
                    <asp:ImageButton ID="btnDate3" ImageUrl="~/images/CalendarIcon.jpg" Width="20px"
                        runat="server" />
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="lblsta" CssClass="lbl" Text="Status" runat="server"></asp:Label>
                </td>
                <td align="left">
                    <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>
                </td>
            </tr>
        </table>
        <table align="center">
            <tr>
                <td>
                    <asp:Button ID="Button3" runat="server" CssClass="btn" Text="Edit" OnClick="Button3_Click" />
                </td>
                <td>
                    <asp:Button ID="Button1" runat="server" CssClass="btn" Text="Update" OnClientClick="return val();"
                        OnClick="Button1_Click" />
                </td>
                <td>
                    <asp:Button ID="Button2" runat="server" CssClass="btn" Text="Cancel" OnClick="Button2_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



The above is my code.

Error Message: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.
Posted

hi,

replace your script by following
C#
var ddl=document.getElementById("<%=dropstatus.ClientID%>");

if (ddl.selectedIndex == "0") {
            alert("Please select the Status");
            ddl.focus();
            return false;
        }

Best Luck
 
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