次のAjax呼び出しメソッドとasp.net webmethodがあります。
私のasp.net関数は、Ajax呼び出しで必要な値を返しています。
私は多くのことを試しましたが、まだ成功していません。
AJAX CALL
<script type="text/javascript">
$(document).ready(function () {
// Add the page method call as an onclick handler for the control.
$("#<%=ddlEmailTemplate.ClientID%>").change(function () {
debugger;
var myparam = $("#<%=ddlEmailTemplate.ClientID%>").val(); //id name for dropdown list
$.ajax({
type: "POST",
url: "FileTax.aspx/ddlEmailTemplate_SelectedIndexChanged",
data: '{param:"' + myparam + '"}',
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d)
}
});
});
});
</script>
WebMethod asp.net
回答後に更新
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static string ddlEmailTemplate_SelectedIndexChanged(string param)
{
string subject;
string Description;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ForMyTaxConnectionString"].ConnectionString))
{
con.Open();
DataSet ds = new DataSet();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Spo_ShowEmailTemplateContent";
cmd.Parameters.Add(new SqlParameter("@Tid", SqlDbType.Int)).Value = Convert.ToInt32(param);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
con.Close();
da.Dispose();
}
if (ds.Tables[0].Rows.Count > 0)
{
DataRow dr = ds.Tables[0].Rows[0];
subject = Convert.ToString(dr["TemplateSubject"]);
Description = Convert.ToString(dr["TemplateDescription"]);
}
}
}
return JsonConvert.SerializeObject(new { subject = subject, description = Description });
// return subject ;
含むusing Newtonsoft.Json;
[〜#〜] cs [〜#〜]
public string CheckDetails(string param1, string param2)
{
var chk = new check
{
subject = "hello! " +param1 ,
description = param2 +" Years Old"
};
return JsonConvert.SerializeObject(chk);
}
public class check
{
public string subject { get; set; }
public string description { get; set; }
}
[〜#〜] html [〜#〜]
<div>
<input type="text" name="name" id="txtname"/>
<input type="text" name="age" id="txtage"/>
<input type="button" id="btnSubmit" value="details"/>
</div>
Jquery
$(function () {
$('#btnSubmit').on('click', function () {
var options = {
type: "POST",
url: '/Ajax/CheckDetails/',
data: '{param1:"' + $('#txtname').val() + '",param2:"' + $('#txtage').val() + '"}',
async: false,
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response != null && response.d != null) {
var data = response.d;
alert(typeof (data)); //it comes out to be string
//we need to parse it to JSON
data = $.parseJSON(data);
alert(data.subject);
alert(data.description);
}
}
};
$.ajax(options);
});
});