jQuery调用返回数据的ASMX Web服务

时间:2011-02-28 23:48:59

标签: c# jquery asp.net

我正在使用jsTree来组织用户创建的页面。右键单击并按“重命名”后,我想触发一个JS函数,该函数在我的代码后面没有回发(如果可能的话)。我希望它能抓住我在ID上的任何项目,并在数据库中重命名并更新jsTree。

以下是代码隐藏功能示例:

[System.Web.Services.WebMethod]
protected void RenamePage(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(Global.conString))
    {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("contentPageUpdate", con))
        {
            cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = Global.SafeSqlLiteral(txtPage.Text, 1);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery();
        }
        con.Close();

        //Update Content Page Repeater
        using (SqlCommand cmd = new SqlCommand("contentPageGetAll", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

不要认为它是jQuery命中C#函数,而是jQuery命中由URI标识的资源。

当您使用jQuery发出AJAX请求时,您将提供目标的URL,并为该URL执行标准的HttpRequest。这可以是页面(webforms)或控制器动作(MVC)。

由于看起来您正在使用webforms,因此您可以创建一个新页面来处理此请求。如果在Page_Load方法中放置断点,则会在调用AJAX方法时看到它被触发:

在右键单击的javascript中,您可以使用jQuery的ajax方法,如下所示:

$.ajax('mypage.aspx');

在你的mypage.aspx中:

public void Page_Load(object sender, EventArgs e)
{
    Response.Write("You called me!");
}

您可以通过在$.ajax电话中添加一些参数来获取回复的回复:

$.ajax('mypage.aspx', { success: function(data, textStatus, jqXHR) {
    alert(data);
}});

答案 1 :(得分:0)

它不完全是你想要做的,但我认为这个可能给你一个基本的想法,即使用JQuery调用URI并做一些“数据库”*所涉及的内容; - )

你也需要@MichaelShimmins的Jquery AJax代码(我在帖子中省略了它)

Autocomplete jquery and SQL , ASP.NET

HTH。

* (that's not a technical term - apologies)