我正在尝试bind a state ddl by onChange of country ddl
使用PageMethods
aspx 中的 JS 代码
function GetStates() {
var e = document.getElementById("<%=ddlCountry.ClientID %>");
var Countryid = e.options[e.selectedIndex].value;
PageMethods.GetStates(Countryid,ShowStates);
}
function ShowStates(results)
{
document.getElementById("li2").innerHTML=results;
}
.CS 代码
[System.Web.Services.WebMethod]
public static string GetStates(int Cid)
{
DataSet dstFillState = Tbl_State.FillDDLState(Cid);
string strHTML = "<select id='ddlState' name='ddlState'>";
foreach (DataRow row in dstFillState.Tables[0].Rows) // Loop over the rows.
{
strHTML = strHTML + "<option value=" + (string)row["Id"] +">" + (string)row["State"] +"</option>";
}
strHTML = strHTML + "</select>";
return strHTML;
}
错误信息
The server method 'GetStates' failed with the following error: System.InvalidCastException-- Unable to cast object of type 'System.Int32' to type 'System.String'.
我作为浏览器弹出窗口收到此错误。我在这里缺少什么?
解决方案
我认为这里的问题是 CS 和 JS 函数的同名 GetStates,我进行了更改并且它起作用了。