我正在使用主页面和内容页面。 我的客户端功能是-
<script type="text/javascript">
function StateChange(txtState) {
var state = document.getElementById(txtState);
PageMethods.StateSelectionChange(state.value, OnSuccess, onFailed);
}
function OnSuccess(state, userContext, methodName) {
alert(state);
}
function onFailed(state, userContext, methodName) {
alert(state);
}
</script>
服务器端代码是-
public static string StateSelectionChange(string state)
{
//txtCity.Text = "";
//if (txtState.Text != "")
//{
// SqlDataReader dr = cm.Allstate(txtState.Text);
// if (dr.Read())
// {
// con.Close();
// AutoCompleteExtender2.ContextKey = txtState.Text;
// txtCity.Enabled = true;
// txtCity.Focus();
// }
// else
// {
// con.Close();
// ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('State is not exist');", true);
// txtState.Text = "";
// txtState.Focus();
// }
//}
return (state);
}
由于将该函数声明为静态,我无法访问该代码的注释部分(文本框和类主函数)。如果我从此函数中删除静态字,则不应从客户端调用它。
最后我想使用 Java 脚本/ajax 请求从客户端调用非静态函数。
最佳答案
您不能这样做,因为所有控件都是实例,并且您只能从静态方法访问静态成员。我使用的一种解决方法是将控制值作为参数传递给服务器端方法。
关于javascript - 从客户端调用服务器端函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32710248/