简体   繁体   中英

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. The stored procedure returns an int value that I would like to pass back to the orginating form without performing a page refresh.

So far the ajax call is working great. The stored procedure is updating my sql db. I'm also able to put the outputted int from the stored procedure in a variable local to the .cs page. The problem arises when I attempt to store that variable into a hidden field on the aspx page.

The Jquery/Ajax api states that one of the three argumests passed to the success function is "The data returned from the server". thats great but I don't understand how to tell the ajax "this is the variable I want to return into my success function". below is my summerized .aspx code:

   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:

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

        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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