【问题标题】:Call server side function from Client Side从客户端调用服务器端函数
【发布时间】:2015-09-22 06:52:42
【问题描述】:

我正在使用主页面和内容页面。 我的客户端功能是-

 <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 script/ajax 请求从客户端调用非静态函数。

【问题讨论】:

    标签: javascript asp.net ajax c#-4.0


    【解决方案1】:

    您不能这样做,因为所有控件都是实例,您只能从静态方法访问静态成员。我使用的一种解决方法是将控制值作为参数传递给服务器端方法。

    【讨论】:

    • 是的,先生,我知道,但我想从客户端触发 ajax 请求以访问非静态函数后面的代码,这可能吗??
    • 不,要使用ajax,方法必须是静态的。
    猜你喜欢
    • 2017-06-27
    • 2015-01-10
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多