0

我正在尝试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,我进行了更改并且它起作用了。

4

1 回答 1

0

我认为问题出在一线

foreach (DataRow row in dstFillState.Tables[0].Rows) // Loop over the rows.
{
 strHTML = strHTML + "<option value=" + (string)row["Id"] +">" + (string)row["State"] +"</option>";
}

你应该这样使用

foreach (DataRow row in dstFillState.Tables[0].Rows) // Loop over the rows.
{
 strHTML = strHTML + "<option value='" +row["Id"].ToString() +"'>" + row["State"].ToString() +"</option>";
}

甚至

foreach (DataRow row in dstFillState.Tables[0].Rows) // Loop over the rows.
{
 strHTML = strHTML + "<option value='" + row["Id"] +"'>" + row["State"] +"</option>";
}

这是一个很好的例子
Invalid postback or callback argument。当使用 jquery 填充下拉列表并且有回发时

于 2012-12-10T13:06:36.187 回答