What do I do get the dropdownlist to recognize it has a selected object?
So I got the JQUERY AJAX JSON to populate a server control dropdownlist.
The user selects an option in the afore mentioned list.
Then clicks on a serverControl button.
I get a “Invalid postback or callback argument.” after trying for an hour or so to “use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation” I give up and set the EnableEventValidation=”false” in the page tag.
Now no more error but when I check the dropdownlist.selected value, it is empty.
What do I do get the dropdownlist to recognize it has a selected object?
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Ajax.aspx.cs" Inherits="something" EnableEventValidation="false" %>
<!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></title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" language="javascript">
$().ready(function () {
$("#ddlCountry1").change(function () {
$.ajax({
type: "POST",
url: "Ajax.aspx/PopulateStates",
data: "{countryCode:" + "'" + bozo + "'" + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#ddlState").get(0).options.length = 0;
$("#ddlState").get(0).options[0] = new Option("Select State", "-1");
$.each(msg.d, function (value, item) {
$("#ddlState").get(0).options[$("#ddlState").get(0).options.length] = new Option(item.Display, item.Value);
});
var itemCount = $("#ddlState").children().length;
if (itemCount < 2) {
$("#ddlState").hide();
}
else
{ $("#ddlState").show(); };
},
error: function () {
alert("Failed to load states");
}
});
});
});
</script>
</head>
and the form
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><th>country</th><td>
<asp:DropDownList ID="ddlCountry1" runat="server" ClientIDMode="Static">
</asp:DropDownList>
</td></tr>
<tr><th>state</th><td>
<asp:DropDownList ID="ddlState" runat="server" ClientIDMode="Static">
</asp:DropDownList>
</td></tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
and the code behind
protected void Page_Load(object sender, EventArgs e)
{
BindCountryDropDown();
}
private void BindCountryDropDown()
{
ddlCountry1.Items.Add(new ListItem("country1", "country1"));
ddlCountry1.Items.Add(new ListItem("country2", "country2"));
ddlCountry1.DataSource = managers.profileManager.GetCountries();
ddlCountry1.DataTextField = "CountryName";
ddlCountry1.DataValueField = "CountryAbbr";
ddlCountry1.DataBind();
}
[WebMethod]
public static ArrayList PopulateStates(string countryCode)
{
var p = managers.profileManager.GetStates(countryCode);
ArrayList myStates = new ArrayList(p.ToArray());
return myStates;
}
protected void Button1_Click(object sender, EventArgs e)
{
var dd = ddlState.SelectedValue; //is empty
}
The ASP.NET ID is not the client ID. It’s the internal ID for a postback. You can either look at the generated ID when it gets to the browser, or you can see the clientId attribute of the drop downs.