【问题标题】:ajax jquery return vaule to aspx pageajax jquery返回值到aspx页面
【发布时间】:2013-03-20 18:25:21
【问题描述】:

我有一个 aspx 页面,其中包含对执行存储过程的 .cs 的 jquery/ajax 调用。存储过程返回一个 int 值,我想将其传递回原始表单而不执行页面刷新。

到目前为止,ajax 调用运行良好。存储过程正在更新我的 sql 数据库。我还可以将存储过程中输出的 int 放入 .cs 页面的本地变量中。当我尝试将该变量存储到 aspx 页面上的隐藏字段中时,就会出现问题。

Jquery/Ajax api 声明传递给成功函数的三个参数之一是“从服务器返回的数据”。太好了,但我不明白如何告诉 ajax “这是我想返回到我的成功函数中的变量”。以下是我总结的 .aspx 代码:

   var dataToSend = { somedata: $("#somefield").val(), MethodName: 'myMethod'};
    var options =
        {
          url: '<%=ResolveUrl("~/mypage.aspx") %>',
          async: false,
          data: dataToSend,
          dataType: 'text',
          type: 'POST',
          success: function (result) {
          // I would like to store the returned data 
          // into a hidden field on the client side (this aspx page)         
          alert(result); //currently the result only shows the aspx/html of my page.

这是我的 .cs 代码的夏季版本:

using System;
using System.Collections.Generic;
using System.Data;  //
using System.Data.SqlClient; //
using System.Data.SqlTypes; //
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;


public partial class _Default : System.Web.UI.Page
{ 
    public int passmetoaspx;
    private string strsomedata;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Form["MethodName"] == "myMethod")
            {
                updateDatabase();
                return;
            }
        }
    }
    protected void updateDatabase()
    {
        try
        { 
            // create string variables for form data
            somedata = Request.Form["somedata"].ToString();
            using (SqlConnection sconn = new SqlConnection(myconnstring))
            {
                using (SqlCommand scmd = new SqlCommand("mystored_procedure", sconn))
                {
                 scmd.CommandType = CommandType.StoredProcedure;
                 scmd.Parameters.Add("@somedata", SqlDbType.VarChar).Value = strsomedata;
                 scmd.Parameters.Add("@sndtoASPXpage", SqlDbType.Int);
                 scmd.Parameters["@sndtoASPXpage"].Direction = ParameterDirection.Output; 
                 sconn.Open();
                 scmd.ExecuteScalar(); 
                 int returnUID;
                 passmetoASPXpage =     int.Parse(scmd.Parameters["@sendtoASPXpage"].Value.ToString());
                }
            }   
        }
        catch (Exception er)
        {

        }

}

请尝试在这里为我指明正确的方向。谢谢。

【问题讨论】:

  • 旁注:您可能应该将预期的 mime 类型设置为 text/html.. 以防万一您将来遇到问题。

标签: c# jquery asp.net ajax code-behind


【解决方案1】:

可能不是最好的解决方案,但您可以这样做:

    protected void updateDatabase()
    {
        try
        { 
            // create string variables for form data
            somedata = Request.Form["somedata"].ToString();
            using (SqlConnection sconn = new SqlConnection(myconnstring))
            {
                using (SqlCommand scmd = new SqlCommand("mystored_procedure", sconn))
                {
                 scmd.CommandType = CommandType.StoredProcedure;
                 scmd.Parameters.Add("@somedata", SqlDbType.VarChar).Value = strsomedata;
                 scmd.Parameters.Add("@sndtoASPXpage", SqlDbType.Int);
                 scmd.Parameters["@sndtoASPXpage"].Direction = ParameterDirection.Output; 
                 sconn.Open();
                 scmd.ExecuteScalar(); 
                 int returnUID;
                 passmetoASPXpage =     int.Parse(scmd.Parameters["@sendtoASPXpage"].Value.ToString());

//Write to REsponse
      HttpContext.Current.Response.Write(passmetoASPpage.ToString());
        HttpContext.Current.Response.End();


                }
            }   
        }
        catch (Exception er)
        {

        }

}

【讨论】:

  • 哇。我很惊讶。感谢您的快速响应。那行得通!我在网上做了很多研究。也许我没有搜索正确的关键字。 “ajax、jquery、返回值、从代码隐藏中获取”等等。如果有人可以建议一些关于这个主题的好读物,请以我的方式发送。我需要我能得到的一切。泰。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-04
  • 1970-01-01
  • 2013-01-15
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多