簡體   English   中英

ajax jquery將vaule返回到aspx頁面

[英]ajax jquery return vaule to aspx page

我有一個aspx頁面,其中包含對執行存儲過程的.cs的jquery / ajax調用。 該存儲過程返回一個int值,我希望將該值傳遞回原始表單,而不執行頁面刷新。

到目前為止,ajax調用運行良好。 存儲過程正在更新我的sql db。 我還可以將存儲過程中輸出的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)
        {

        }

}

請在這里嘗試指出正確的方向。 謝謝。

可能不是最好的解決方案,但您可以這樣做:

    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