Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Problem : fill Drop-down list with state and city, by Ajax on client side. Then on post-back this will raise error : Invalid post-back or callback argument...
I have found this look at this Microsoft code:
ASP.NET
<%@ Page EnableEventValidation="true" Language="C#"  %>
<!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 id="Head1" runat="server">
    <title>Register an option for event validation</title>
    <script runat="server">
        protected override void Render(HtmlTextWriter writer)
        {
            ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 1");
            ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 2");
            ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 3");
            // Uncomment the line below when you want to specifically register the option for event validation.
            // ClientScript.RegisterForEventValidation("DropDownList1", "Is this option registered for event validation?");
            base.Render(writer);
        }
    </script>
    <script type="text/javascript">
        function Initialize()
        {
            var oOption = document.createElement("OPTION");
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "This is Option 1";            
            oOption = document.createElement("OPTION");            
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "This is Option 2";
            oOption = document.createElement("OPTION");                
            document.all("DropDownList1").options.add(oOption); 
            oOption.innerText = "This is Option 3";
            oOption = document.createElement("OPTION");                
            document.all("DropDownList1").options.add(oOption); 
            oOption.innerText = "Is this option registered for event validation?";
        }
    </script>
</head>
<body onload="Initialize();">
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="Postback to server for validation" />
    </div>
    </form>
</body>
</html>


In this code Drop-down list is filled in client side, but all items are predicted and filling is just before it is rendered (on server side).
By Ajax it is not possible to register all items user may select.
Example : user select a state and Ajax fill all cities. How many cities is there in the USA? It must all be registered to this page!!!???
So there must be a better way to register new collection to page , when user select a new stat.
Question : how can I call this code "ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 1");" by java script? I want to register each list item when I am adding them to list, simultanoiusly? Can any one help?
Posted

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