【问题标题】:Calling C# event using ajax is not working使用 ajax 调用 C# 事件不起作用
【发布时间】:2017-04-14 08:05:30
【问题描述】:

Button 点击事件上,我使用了ajax 方法,它调用了在服务器端编写的webmethod。

我想在单击按钮时调用该方法。但是使用我当前的代码,它并没有达到写在服务器端的[WebMethod]

这是两个代码

Ajax 代码

$(document).ready(function () {
        $('#btnAttachParty').click(function () {                
            $.ajax({
                url: 'FrmAgreementMaster.aspx/btnAttachParty_Click?id=' + $(this).attr('id')
            });
        });
    });

服务器端代码

[WebMethod]
public void btnAttachParty_Click(int id)
{
    if (strMode == "A")
    {
        if (HidAttachVal.Value == "")
        {
            ObjPriCon.Open();
            OracleCommand objpricmd = new OracleCommand("select xxcus.xxacl_pn_party_info_SEQ.nextval from dual", ObjPriCon);
            HidAttachVal.Value = Convert.ToString(objpricmd.ExecuteOracleScalar());
            ObjPriCon.Close();
        }
        ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "AttachmentCallParty(" + HidAttachVal.Value + ");", true);
    }
    else
    {
        if (HidAttachVal.Value == "")
        {
            ObjPriCon.Open();
            OracleCommand ObjPriCmd = new OracleCommand("select xxcus.xxacl_pn_party_info_SEQ.nextval from dual", ObjPriCon);
            HidAttachVal.Value = Convert.ToString(ObjPriCmd.ExecuteOracleScalar());
            ObjPriCon.Close();
        }
        ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "AttachmentCallParty(" + HidAttachVal.Value + ");", true);
    }
}

请告诉我为什么它没有命中网络方法部分。

【问题讨论】:

    标签: javascript c# jquery ajax webmethod


    【解决方案1】:

    您在 ASPX 中的 WebMethod必须static

    [WebMethod]
    public static void btnAttachParty_Click(int id)
    {
    
    }
    

    【讨论】:

    • 如果我添加static 字,我的变量开始报错
    • @nad 所以调试你的错误,看看它是什么。另外我建议你在 ajax 的 data 属性中传递函数的参数!
    • 对于strMode,它给出了错误,因为非静态字段、方法或属性需要对象引用
    • 那么strMode是什么定义为静态的。
    • 我已经定义了,我只是在这里检查一些条件,我不能再次将它定义为静态的。
    【解决方案2】:

    我认为问题就在这里,

    $(this).attr('id') // btnAttachParty
    

    这将返回按钮的 id。

    尝试使用value 属性而不是id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多