我很难在VB中使用Web方法。 javascript被调用但似乎永远不会调用web方法。我在调试模式下运行并且没有错误。
以下是代码:
<System.Web.Services.WebMethod()>
Public Shared Sub PasteEvent(EventID As Integer,
startDate As DateTime,
endDate as DateTime,
newStart As DateTime)
' work out the diff between start and end
Dim difference As long = DateDiff(DateInterval.Minute,startDate,endDate)
' pasteStart + minutes from the event start
' this is because we can only paste on the hour, but events may have started after the hour
' ie 10:15
newStart.AddMinutes(startDate.Minute)
' new end = pastestart + diff
Dim newEnd As DateTime = newStart.AddMinutes(convert.ToDouble(difference))
' call database
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())
Dim cmd As New SqlCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spOPSCopyEvent"
cmd.Parameters.AddWithValue("@EventID", EventID)
cmd.Parameters.AddWithValue("@StartDate", newStart)
cmd.Parameters.AddWithValue("@EndDate", newEnd)
cmd.Connection = conn
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End sub
调用它的JavaScript:
<script type="text/javascript" language="javascript">
function eventCopy(eventID, start, end)
{
alert("copy");
// grab the event id and store it in a hidden text box
$("#ctl00_MainContent_hidCopyEventID").val(eventID);
$("#ctl00_MainContent_hidCopyStart").val(start);
$("#ctl00_MainContent_hidCopyEnd").val(end);
}
function eventPaste(eventStart)
{
alert("paste");
alert(eventStart);
// Call a web method, passing the eventID and the new start time
var eventID = $("#ctl00_MainContent_hidCopyEventID").val;
var startDate = $("#ctl00_MainContent_hidCopyStart").val;
var endDate = $("#ctl00_MainContent_hidCopyEnd").val;
PageMethods.PasteEvent(eventID, startDate, endDate, eventstart)
}
</script>
到目前为止,我有:
在主页面中更新了我的脚本管理器以获得enablePageMethods="true"
尝试添加Imports System.Web.Services
将javascript移动到正文而不是头部
答案 0 :(得分:2)
问题是您缺少异常处理机制 看看你得到了什么错误。
尝试并捕获javascript和vb代码并打印错误。
使用像Fidler这样的嗅探器来查看您要发送的内容。
尝试使用在Web服务中打印跟踪消息 Trace.Log,你可以在运行DebugView后看到它们,看看你在哪里
答案 1 :(得分:0)
使用此行时:
var eventID = $("#ctl00_MainContent_hidCopyEventID").val;
我应该说:
var eventID = $("#ctl00_MainContent_hidCopyEventID").val();
接受Gregorys作为小提琴手的回答是诊断问题最有帮助的。