[英]Call server side function from Client Side
我正在使用“母版和内容”页面。 我的客户端功能是-
<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请求从客户端调用非静态函数。
您不能这样做,因为所有控件都是实例,并且只能从静态方法访问静态成员。 我使用的一种解决方法是将控制值作为参数传递给服务器端方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.