Filling a DropDownList with an AJAX response and getting the selected value on the server with ASP.NET WebForms
I'm having trouble with this simple scenario.
First, I'm using ASP.NET WebForms.
I'm making an ajax call using jQuery after the user select a value on a first dropdown (select). This call will return me an array 开发者_JAVA技巧of values with which I fill a second DropDownList. After a postback, of course the DropDownList has no items.
I have tried to re-fill the DropDownList in the Init event of the page, but the viewstate has not been processed and I need the selected value of the first DropDownList to get the correct values.
I know that I could probably hack something with putting the selected value of the dropdown inside an hidden field and get it back later in the page cycle, but surely there is something I'm missing? A more easy way (no UpdatePanel solution please).
With JavaScript/AJAX
You can use PageMethods to get the Data for the second list.
Page Methods are static functions with the [WebMethod]
attribute. eg:
[WebMethod]
public static string GetHello()
{
return "hello";
}
To see how to do this, you should read the page using-jquery-to-directly-call-aspnet-ajax-page-methods
Without Ajax
- Enable the CausesPostBack property in the first list.
- Fill the second drop down list in the SelectedIndexChanged event of the first.
精彩评论