hello,
this is my first attempt at ajax...I found a good example for .net which is the following link:
I'm trying to re-use it by pulling the values from my database instead of the xml file they created. I'm also trying to get cities per state instead of states per country.
I'm understanding it but I can't get past the js function 'ClearAndSetCityListItems' as I'm getting an object required error because when the page renders it has a different name than 'cityList'...it has the autogenerated name and id which is 'ctl00_ContentColumn_cityList'...
When I download the sample and run it, do view source on the client page, I notice that the names of the controls are exactly as they declared in the aspx code. I can't figure out how they explicitly named the control 'stateList' to be able to access it from javascript. Did they do that in the xsl (it didn't appear that way to me).
my question is how do you explicitly name a control on a page so you can access it from the javascript?
thanks for any insight!!! I need to use ajax in many places...
web controls on aspx
javascript code
this is my first attempt at ajax...I found a good example for .net which is the following link:
I'm trying to re-use it by pulling the values from my database instead of the xml file they created. I'm also trying to get cities per state instead of states per country.
I'm understanding it but I can't get past the js function 'ClearAndSetCityListItems' as I'm getting an object required error because when the page renders it has a different name than 'cityList'...it has the autogenerated name and id which is 'ctl00_ContentColumn_cityList'...
When I download the sample and run it, do view source on the client page, I notice that the names of the controls are exactly as they declared in the aspx code. I can't figure out how they explicitly named the control 'stateList' to be able to access it from javascript. Did they do that in the xsl (it didn't appear that way to me).
my question is how do you explicitly name a control on a page so you can access it from the javascript?
thanks for any insight!!! I need to use ajax in many places...
web controls on aspx
Code:
<td><asp:DropDownList ID="stateList" runat="server" onchange="return StateListOnChange(this)" ></asp:DropDownList></td> <td><asp:DropDownList ID="cityList" runat="server"></asp:DropDownList></td>
javascript code
Code:
function StateListOnChange(object)
{
var stateList = object;
//Getting the selected state from country combo box.
var selectedState = stateList.options[stateList.selectedIndex].value;
// URL to get states for a given country
var requestUrl = AjaxServerPageName + "?SelectedState=" + encodeURIComponent(selectedState);
alert(requestUrl);
CreateXmlHttp();
// If browser supports XMLHTTPRequest object
if(XmlHttp)
{
//Setting the event handler for the response
XmlHttp.onreadystatechange = HandleResponse;
//Initializes the request object with GET (METHOD of posting),
//Request URL and sets the request as asynchronous.
XmlHttp.open("GET", requestUrl, true);
//Sends the request to server
XmlHttp.send(null);
}
}
function HandleResponse()
{
if(XmlHttp.readyState == 4)
{
if(XmlHttp.status == 200)
{
//ClearAndSetStateListItems(XmlHttp.responseXML.documentElement);
ClearAndSetCitiesListItems(XmlHttp.responseXML.documentElement);
}
else
{
alert("There was a problem retrieving data from the server." );
}
}
}
function ClearAndSetCitiesListItems(StateNode)
{
var cityList = document.getElementById("cityList");
for (var count = cityList.options.length-1; count >-1; count--)
{
cityList.options[count] = null;
}
var cityNodes = StateNode.getElementsByTagName('State');
var textValue;
var optionItem;
//Add new states list to the state combo box.
for (var count = 0; count < cityNodes.length; count++)
{
textValue = GetInnerText(cityNodes[count]);
optionItem = new Option( textValue, textValue, false, false);
cityList.options[cityList.length] = optionItem;
}
}