如何使用ajax post方法调用C#代码方法 [英] How to call C# code method by using ajax post method
问题描述
您好我在asp.net中有一个ajax post方法,请参阅下面的代码
$('#Button1')。click(function(){
$ .ajax({
url:AjaxFunctionality.aspx / GetElemtData,
data:{helpTextKey:$('txtPlantId')。val ()},
方法:'post',
dataType:'xml',
sucess:function(response,status,xhr) {
var resp = $(回复);
alert(resp);
}
});
});
});
使用post方法我必须调用C#方法,请参阅下面的方法:
public string GetElemtData(string obj)
{
// String obj = Request [helpTextKey];
String returnValue = string.Empty;
String connectionstring = ConfigurationManager.ConnectionStrings [ECSConnectionString]。ConnectionString;
using(SqlConnection con = new SqlConnection (connectionstring))
{
SqlCommand cmd = new SqlCommand(选择top from 1 * from Carriers plantid ='+ obj +',con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
returnValue = dr [3] .ToString();
}
con.Close();
dr.Close();
}
返回returnValue;
}
我已经尝试了几次调用方法但是我无法调用。请帮忙解决这个问题。
Hi I have a ajax post method in asp.net see below the code
$('#Button1').click(function () {
$.ajax({
url: "AjaxFunctionality.aspx/GetElemtData",
data: { helpTextKey: $('txtPlantId').val() },
method: 'post',
dataType: 'xml',
sucess: function (response, status, xhr) {
var resp = $(response);
alert(resp);
}
});
});
});
by using post method i have to call C# method see method below:
public string GetElemtData(string obj)
{
//String obj = Request["helpTextKey"];
String returnValue = string.Empty;
String connectionstring = ConfigurationManager.ConnectionStrings["ECSConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionstring))
{
SqlCommand cmd = new SqlCommand("select top 1 * from Carriers where plantid='" + obj + "'", con);
con.Open();
SqlDataReader dr= cmd.ExecuteReader();
while(dr.Read())
{
returnValue = dr[3].ToString();
}
con.Close();
dr.Close();
}
return returnValue;
}
I have tried several time to calling the method but but i couldn't be called. please help on this how to approach.
推荐答案
('#Button1')。点击(function(){
('#Button1').click(function () {
.ajax({
url:AjaxFunctionality.aspx / GetElemtData,
data:{helpTextKey:
.ajax({
url: "AjaxFunctionality.aspx/GetElemtData",
data: { helpTextKey:
( 'txtPlantId')。val()},
方法:'post',
dataType:'xml',
sucess:function(响应,状态,xhr){
var resp =
('txtPlantId').val() },
method: 'post',
dataType: 'xml',
sucess: function (response, status, xhr) {
var resp =
这篇关于如何使用ajax post方法调用C#代码方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!