由于样式问题以及在一个网页上需要多个表单,我目前使用runat = server有一个超出正常形式的表单。
我是否仍然可以使用ajax在此网页的C#代码隐藏中调用WebMethod?我想将此表单中的信息以不同的形式提交给我之前在页面中使用的相同连接字符串。
这是我目前的代码:
$().ready(function () {
$("input[type=submit]").click(function () {
handleClick();
createJob();
});
});
function createJob() {
var txtTestValue = document.getElementById('jobTitle').value;
$.ajax({
// POST signals a data request
type: "POST",
// This directs which function in the c# code behind to use
url: "Account/help.aspx/CreateJob",
// The paramater pageIndex, page number we need to load, to pass to GetCustomers(int pageIndex)
data: txtTestValue,
// Type of data we are sending to the server (i.e. the pageIndex paramater)
contentType: "application/json; charset=utf-8",
// Type of data we expect back from the server (to fill into the html ultimately)
dataType: "text",
// If all goes smoothly to here, run the function that fills our html table
success: OnSuccess,
// On failure, error alert user (aka me so that I know something isn't working)
failure: function (response) {
alert("failure");
},
error: function (response) {
alert("error");
}
});
});
我的WebMethod代码隐藏:
[WebMethod]
public string CreateJob()
{
//rest of my database code here
}
很抱歉这个混乱,但它在ajax代码之前做了一切,然后似乎忽略了它(并返回ajax失败并出现错误)。我的代码没有到达WebMethod,我在Visual Studio中设置的任何断点都没有在页眉中触发。在此先感谢您的帮助!
答案 0 :(得分:2)
您需要将方法声明为 static 。
[WebMethod]
public static string CreateJob()
^^^^^
{
//rest of my database code here
}
另一个问题是,如果input[type=submit]
是ASP.Net Button控件,它将回发给服务器。 您无法使用ASP.Net Server控件进行jQuery Ajax调用 - $ .ajax。
// This code won't work if `input[type=submit]` is a server button control
$(function () {
$("input[type=submit]").click(function () {
handleClick();
createJob();
});
});
您需要使用常规html 输入 或 按钮 控制type=button
而不是{ {1}}。
答案 1 :(得分:1)
网络方法应该是静态的。
CREATE PROCEDURE [Accounts].[GetUserPaymentsPartialRecordByClass]
@classID int=1
AS
BEGIN
create table #temp (sID int,cID int, accID int, className varchar(50),transactions varchar(max),PayableAmount money,TotalAmountPaid money,PendingAmount money)
INSERT into #temp
(sID,cID,accID,className)
select
distinct st.StudentID as sID,
st.ClassToWhichAdmitted_ID as cID ,
st.Account_ID as accID,
c.ClassName as className
from School.StudentInformation st
left join School.Classes c
on st.ClassToWhichAdmitted_ID= c.ClassID
where st.ClassToWhichAdmitted_ID= @classID
update temp
set temp.transactions=transactions
from #temp temp outer apply(
select convert(varchar,Convert(decimal(10,0),t.Credit))+', ' AS 'data()'
from Accounts.Transactions T
on T.Account_ID=temp.accID
and T.Credit>0 and T.IsReversed=0
and Particulars Like '%Received%'
inner join Accounts.Invoices inv
on inv.Student_ID=temp.sID and inv.InvoiceNumber=T.InvoiceNumber
FOR XML PATH('')
) s(transactions)
update T
set PayableAmount = sum(i.Amount+ i.Fine)
from #temp T
INNER JOIN Accounts.Invoices I ON I.Student_ID= T.sId and i.Class_ID = T.cID
update temp
set TotalAmountPaid =sum(t.Credit) from
#temp temp Inner join
Accounts.Transactions T
on t.Account_ID = temp.accID
and t.Credit>0 AND t.IsReversed=0 and
t.Particulars Like '%Received%'
inner join Accounts.Invoices inv
on inv.Student_ID=temp.sID and inv.InvoiceNumber=T.InvoiceNumber
group by T.Account_ID
update temp
sett PendingAmount= (PayableAmount - TotalAmountPaid)
SELECT st.StudentName, st.StudentRegisterationNo,ClassName,
transactions as Paid, Convert(decimal(10,0),TotalAmountPaid) as TotalPaid,
PayableAmount, PendingAmount as 'PendingAmount' FROM #temp T inner join School.StudentInformation ST
On st.StudentID= T.sId
END