简体   繁体   English

ajax jquery将vaule返回到aspx页面

[英]ajax jquery return vaule to aspx page

I have an aspx page containing a jquery/ajax call to my .cs where a stored procedure is executed. 我有一个aspx页面,其中包含对执行存储过程的.cs的jquery / ajax调用。 The stored procedure returns an int value that I would like to pass back to the orginating form without performing a page refresh. 该存储过程返回一个int值,我希望将该值传递回原始表单,而不执行页面刷新。

So far the ajax call is working great. 到目前为止,ajax调用运行良好。 The stored procedure is updating my sql db. 存储过程正在更新我的sql db。 I'm also able to put the outputted int from the stored procedure in a variable local to the .cs page. 我还可以将存储过程中输出的int放在.cs页面本地的变量中。 The problem arises when I attempt to store that variable into a hidden field on the aspx page. 当我尝试将该变量存储到aspx页面上的隐藏字段中时,就会出现问题。

The Jquery/Ajax api states that one of the three argumests passed to the success function is "The data returned from the server". Jquery / Ajax api指出传递给成功函数的三个最大的参数之一是“从服务器返回的数据”。 thats great but I don't understand how to tell the ajax "this is the variable I want to return into my success function". 太好了,但是我不明白如何告诉ajax“这是我想返回到我的成功函数中的变量”。 below is my summerized .aspx code: 下面是我的总结.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.

Here is summerized version of my .cs code behind: 这是我的.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)
        {

        }

}

Please try to point me in the right direction here. 请在这里尝试指出正确的方向。 Thank you. 谢谢。

Probably not the best solution but you can do it like this: 可能不是最好的解决方案,但您可以这样做:

    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)
        {

        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM