在客户端函数中调用服务器端方法(Javascript)
谁能帮我在 javascript 函数中调用 VB.NET 方法?我的方法不是共享/静态的,并且不返回任何内容。它只是简单地将数据保存到数据库并重定向用户。请帮助我,这是我的代码:
VB方法
Public Function SaveVehicles()
Dim res As Boolean
If res = True Then
Dim sModel As String = cboModel.SelectedValue
Dim sVariant As String = cboVariant.SelectedValue
Dim sColor As String = cboColor.SelectedValue
cboModel.SelectedValue = sModel
cboVariant.SelectedValue = sVariant
cboColor.SelectedValue = sColor
Dim oData As New WebServVehSwapping
Dim strSql As String
Dim sDealer As String
Dim sUserName As String
'sDealer = "TBP01"
sDealer = Trim(Request.QueryString("dealercode"))
If sDealer = "" Then sDealer = "TBP01"
sUserName = "User1"
'------------------------------------------
strSql = oData.InsertTblRequirement( _
sDealer _
, Now.ToString _
, sUserName _
, cboColor.Text.Trim _
, cboModel.Text.Trim _
, cboVariant.Text.Trim, "Open")
MsgBox("OKAY")
Response.Redirect("MyRequirements.aspx?DealerCode=" & sDealer)
Else
'do Nothing
End If
End Function
,这是我的Javascript函数,
function ConfirmView()
{
var Ok = confirm('There is/are existing vehicle(s) in Network Vehiches for sale, View Vehicle(s)?');
if(Ok==true)
{
location.href = 'NetworkVehiclesforSale.aspx';
return false;
}
else if (Ok!=true)
{
//THE VB METHOD GOES HERE
}
}
我尝试过回调处理程序,它只适用于返回某些内容/字符串的函数
,我尝试过Pagemethod,但它只适用于静态/共享函数。请帮助我,我真的非常需要它。请看。谢谢
can anyone help me in calling my VB.NET method within my javascript function? my method is not shared/static and does not return anything. It just simply saves data to database and redirect the user. PLease help me, Here's my code:
VB method
Public Function SaveVehicles()
Dim res As Boolean
If res = True Then
Dim sModel As String = cboModel.SelectedValue
Dim sVariant As String = cboVariant.SelectedValue
Dim sColor As String = cboColor.SelectedValue
cboModel.SelectedValue = sModel
cboVariant.SelectedValue = sVariant
cboColor.SelectedValue = sColor
Dim oData As New WebServVehSwapping
Dim strSql As String
Dim sDealer As String
Dim sUserName As String
'sDealer = "TBP01"
sDealer = Trim(Request.QueryString("dealercode"))
If sDealer = "" Then sDealer = "TBP01"
sUserName = "User1"
'------------------------------------------
strSql = oData.InsertTblRequirement( _
sDealer _
, Now.ToString _
, sUserName _
, cboColor.Text.Trim _
, cboModel.Text.Trim _
, cboVariant.Text.Trim, "Open")
MsgBox("OKAY")
Response.Redirect("MyRequirements.aspx?DealerCode=" & sDealer)
Else
'do Nothing
End If
End Function
and here's my Javascript function
function ConfirmView()
{
var Ok = confirm('There is/are existing vehicle(s) in Network Vehiches for sale, View Vehicle(s)?');
if(Ok==true)
{
location.href = 'NetworkVehiclesforSale.aspx';
return false;
}
else if (Ok!=true)
{
//THE VB METHOD GOES HERE
}
}
I've tried the callback handler, and it just works with function that return something/string
and i've tried Pagemethod but it just works with static/shared function. please help me, I really need it Badly. PLEasee. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.Net Web 服务无法发挥魔法,即您无法对服务器上的 Ajax 请求发出重定向响应并期望整个页面被重定向。
唯一会发生的事情是 Ajax 调用被重定向到另一个页面并尝试从那里获取数据。如果您想在客户端浏览器中更改页面,则必须在客户端通过 JavaScript 进行操作,例如
document.location = url_returned_by_your_function_through_ajax_call
。.Net web services cannot perform magic, i.e. you cannot issue a redirect response to an Ajax request on the server and expect the whole page to be redirected.
The only thing that will happen is that the Ajax call got redirected to another page and tries to get data from there. If you want to change the page in the client browser, you must do it on the client side through JavaScript, e.g.
document.location = url_returned_by_your_function_through_ajax_call
.您可能想阅读“构建 Windows Communication Foundation Services 简介” - http:// msdn.microsoft.com/en-us/library/aa480190.aspx
特别是:“使用 WCF 3.5 设计和构建 RESTful Web 服务的指南”- http://msdn.microsoft.com/en-us/library/dd203052.aspx
并查看一些可以轻松调用 RESTful Web 服务的 javascript 库,例如 jQuery - http://www.jquery.com/
使用 jQuery,您可以像这样调用服务器端 VB.NET 代码:
You might want to read "Introduction to Building Windows Communication Foundation Services" - http://msdn.microsoft.com/en-us/library/aa480190.aspx
And especially: "A Guide to Designing and Building RESTful Web Services with WCF 3.5" - http://msdn.microsoft.com/en-us/library/dd203052.aspx
And check out some javascript libraries that make calling RESTful web services easy, like jQuery - http://www.jquery.com/
With jQuery you could make a call to your server-side VB.NET code like this: