Yesterday i got a requirement as follows

As in the screen shot depending upon the selection in the radio button list I need to fill drop down, similar to cascading drop down lists I used Jquery ajax to call database. Following steps are used in developing functionality Step 1 Add using System.Web.Services; in the name space region Add attribute [WebMethod] above the method to fill data. example [WebMethod] function filldropdown() { // do db interaction to get data } Step 2 Add following javascript to web page $(document).ready(function () { $('#<%= rdbFormType.ClientID%> :input').click(function () { var selRad = $("input[@name=<%=rdbFormType.ClientID%>]:radio:checked").val(); if (selRad == 1) { $('#AssociatedForm'). css('visibility', 'visible'); var loc = window.location.href; loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "AddForm.aspx" : loc; $.ajax({ type: "POST", url: loc + "/" + 'methodname', data: "{}", beforeSend: function (xhr) { xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); }, dataType: "json", success: function (msg) { $('#<%= ddlForm.ClientID%>') .get(0).options.length = 0; $('#<%= ddlForm.ClientID%>') .get(0).options[0] = new Option("Select Form", "-1"); $.each(msg.d, function (index, item) { $('#<%= ddlForm.ClientID%>').get(0).options[$('#<%= ddlForm.ClientID%>').get(0).options.length] = new Option(item.Display,item.Value); }); }, error: function (res, status, ex) { alert('Not able to load Form Data'); } }); } else { $('#AssociatedForm').css('visibility', 'collapse'); } }); }); Step 3 Add following to the web.config add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" In following above steps you may encounter following errors Error 1) 500 internal server error while posting data to server fix : replace data: "{}" with data: {} Error 2) 200 +ok but it will return whole html design. Fix: check your database calling method is having keywords Public and static and also check whether step 3 is followed or not Error 3)Invalid postback or callback argument. A Event validation is enabled using in configuration or <%@ Page EnableEventValidation=”true” %> in a page. Â For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. Â If the data is valid and expected, use the ClientScriptManager. RegisterForEventValidation method in order to register the postback or callback data for validation. Fix: protected override void Render(HtmlTextWriter writer) { Page.ClientScript. RegisterForEventValidation (dropdownlistcontrol.UniqueID, value); base.Render(writer); }

Comments

Popular Posts