I have a cascading dropdown list on my page. The first dropdown contains a list of events. The second dropdown contains a list of attendees. When an event is selected, an AJAX call is made to get the list of attendees for that event, and populate the second dropdown.
My problem is that for the attendees dropdown, I want the user to be redirected to a URL on the SelectedIndexChanged event, with the selected value in the query string. But on the SelectedIndexChanged event, all the items in the second dropdown are reset. Here's my code:
<script type="text/javascript" language="javascript">
$(document).ready(function()
{
$('#<%= ddlEvents.ClientID %>').change(function()
{
$.ajax({
type: "POST",
url: 'Default.aspx/PopulateAttendees',
data: '{EventId: ' + $('#<%= ddlEvents.ClientID %>').val() + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnAttendeesPopulated
});
});
function OnAttendeesPopulated(response)
{
var attendees = response.d;
var attendeesDropdown = $("#<%= ddlAttendees.ClientID %>");
attendeesDropdown.removeAttr("disabled");
$.each(attendees, function()
{
attendeesDropdown.append($("<option</option>").val(this['Value']).html(this['Text']));
});
}
});
</script>
<asp:DropDownList ID="ddlEvents" runat="server"></asp:DropDownList>
<br /><br />
<asp:DropDownList ID="ddlAttendees" runat="server" Enabled="false" onselectedindexchanged="ddlAttendees_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
And in my code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Bind dropdown to Events
ddlEvents.DataSource = EventsList();
ddlEvents.DataTextField = "Name";
ddlEvents.DataValueField = "EventID";
ddlEvents.DataBind();
}
}
[System.Web.Services.WebMethod]
public static ArrayList PopulateAttendees(int EventId)
{
ArrayList attendees = new ArrayList();
attendees = GetAttendees(EventId);
return attendees;
}
protected void ddlAttendees_SelectedIndexChanged(object sender, EventArgs e)
{
string AttendeeId = ddlAttendees.SelectedValue;
Response.Redirect("http://mysite.com?id=" + AttendeeId);
}
When the SelectedIndexChanged event is hit, ddlAttendees is empty.
Does anyone know how I can keep the items in ddlAttendees?