C# ajax jquery将变量返回到aspx页面
我有一个aspx页面,其中包含对my.cs的jquery/ajax调用,其中执行了一个存储过程。存储过程返回一个int值,我希望在不执行页面刷新的情况下将该值传递回原始表单 到目前为止,ajax调用工作得很好。存储过程正在更新我的sql数据库。我还能够将存储过程中输出的int放在一个局部变量中到.cs页面。当我试图将该变量存储到aspx页面上的隐藏字段中时,问题就出现了 Jquery/Ajax api声明传递给success函数的三个参数之一是“从服务器返回的数据”。这很好,但我不明白如何告诉ajax“这是我想要返回到成功函数中的变量”。下面是我的summerized.aspx代码:C# ajax jquery将变量返回到aspx页面,c#,jquery,asp.net,ajax,code-behind,C#,Jquery,Asp.net,Ajax,Code Behind,我有一个aspx页面,其中包含对my.cs的jquery/ajax调用,其中执行了一个存储过程。存储过程返回一个int值,我希望在不执行页面刷新的情况下将该值传递回原始表单 到目前为止,ajax调用工作得很好。存储过程正在更新我的sql数据库。我还能够将存储过程中输出的int放在一个局部变量中到.cs页面。当我试图将该变量存储到aspx页面上的隐藏字段中时,问题就出现了 Jquery/Ajax api声明传递给success函数的三个参数之一是“从服务器返回的数据”。这很好,但我不明白如何告诉a
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.
请在这里给我指一下正确的方向。谢谢。可能不是最好的解决方案,但您可以这样做:
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)
{
}
}
旁注:您可能应该将预期的mime类型设置为
text/html
。。以防万一你将来遇到问题。我很惊讶。谢谢你的快速回复。成功了!我在网上做了很多研究。也许我没有搜索正确的关键字。“ajax、jquery、返回值、从代码隐藏中获取”等等。如果有人能就这个话题提出一些好的建议,请把它寄给我。我需要我能得到的一切。泰。
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)
{
}
}