什么是在asp.net中调用Ajax代码的更好方法 [英] What is a better way to call Ajax code in asp.net
本文介绍了什么是在asp.net中调用Ajax代码的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找出一种更好的方法来执行ajax代码。现在从aspx文件我调用一个ashx文件,其中我把html代码与C#代码结合起来。
然而,它似乎不是最好的解决方案。有没有办法在执行C#代码后调用将返回结果的html或aspx?就像使用C#代码返回的实际值替换占位符的模板一样。
谢谢。
下面是一个示例代码:
ASPX文件:
var accountID = $(# <% = hdnAccountID.ClientID %> )。val();
$ .ajax({
类型:POST,
url:/ MyCourses / Handlers / defaultCourse.ashx?& AccountID =+ accountID,
数据:'AccountID ='+ accountID,
contentType:application / json; charset = utf-8,
成功:函数(数据){
if(data!=0 ){
$(#divAjaxDefaultCourse)。html(data);
$('#divAjaxDefaultCourse')。show();
}
} ,
错误:function(){
alert('抱歉发生了一些错误');
}
});
// **************************************** *
ashx文件
string htmlOutput =
< div class =' col-lg-6 mid_right' > +
< div 类 =' course_banner' > ; +
< div class =' banner_left' 跨度> <跨度class =code-keyword>> +
< img src =' / images / banner_right_icon.png' > +
< / div > +
< div class =' banner_content' > +
< div class =' formatCourseHeader' 样式 =' font-size:24px ;颜色:#000000; font-weight:500;宽度:50%; margin-top:43px; float:left;' > +
< div id =' courseNameDefault' > + defaultCourse.CourseTitle +< / div > +
< div id =' daysDefault' > 天数:+ defaultCourse.MeetingDays +< / div > ; +
< div id =' timesDefault' > 时间:+ defaultCourse.MeetingTime +< / div > +
< div runat =' server' id =' divClassSizeDefault' > + classSize +同学< / div > +
< / div > +
< / div > +
< / div > +
返回htmlOutput;
}
解决方案
(#<% = hdnAccountID.ClientID %> )。val();
.ajax({
type:POST ,
url:/ MyCourses/Handlers/defaultCourse.ashx?&AccountID=+ accountID,
data:'AccountID ='+ accountID,
contentType:application / json; charset = utf-8,
成功:函数(数据){
if(data!=0){
(#divAjaxDefaultCourse)。 HTML(数据);
I am trying to figure out a better way to execute ajax code. Right now from aspx file I call a ashx file in which I put html code combined with C# code.
However it seems not the best solution. Is there a way to call an html or aspx that will return the results after C# code executed? Like a template where you replace placeholders with actual values returned by the C# code.
Thank you.
Here is a sample code:
ASPX file:
var accountID = $("#<%= hdnAccountID.ClientID %>").val();
$.ajax({
type: "POST",
url: "/MyCourses/Handlers/defaultCourse.ashx?&AccountID=" + accountID,
data: 'AccountID=' + accountID,
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data != "0") {
$("#divAjaxDefaultCourse").html(data);
$('#divAjaxDefaultCourse').show();
}
},
error: function () {
alert('Sorry some error occurred');
}
});
//*****************************************
ashx file
string htmlOutput =
"<div class='col-lg-6 mid_right'>"+
"<div class='course_banner'>"+
"<div class='banner_left'>"+
"<img src='/images/banner_right_icon.png'>"+
"</div>"+
"<div class='banner_content'>"+
"<div class='formatCourseHeader' style='font-size: 24px; color: #000000; font-weight: 500; width: 50%; margin-top: 43px; float: left;'>"+
"<div id='courseNameDefault'>"+ defaultCourse.CourseTitle + "</div>" +
"<div id='daysDefault'>Days: " + defaultCourse.MeetingDays + "</div>" +
"<div id='timesDefault'>Time: " + defaultCourse.MeetingTime + "</div>" +
"<div runat='server' id='divClassSizeDefault'>" + classSize + " Classmates</div>"+
"</div>"+
"</div>"+
"</div>"+
return htmlOutput;
}
解决方案
("#<%= hdnAccountID.ClientID %>").val();
.ajax({ type: "POST", url: "/MyCourses/Handlers/defaultCourse.ashx?&AccountID=" + accountID, data: 'AccountID=' + accountID, contentType: "application/json; charset=utf-8", success: function (data) { if (data != "0") {
("#divAjaxDefaultCourse").html(data);
这篇关于什么是在asp.net中调用Ajax代码的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文