jquery调用vb.net函数并使用响应填充文本框
我正在使用以下代码:jquery调用vb.net函数并使用响应填充文本框,jquery,asp.net,vb.net,Jquery,Asp.net,Vb.net,我正在使用以下代码: $("#dropdownPaper").change(function () { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "NomexLine500A.aspx/CalcBlockCode",
$("#dropdownPaper").change(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "NomexLine500A.aspx/CalcBlockCode",
data: "{}",
dataType: "json",
success: function (data) {
$("#textBlockCode").text(data.d)
},
error: function (result) { }
});
});
响应dropdownlist更改以在aspx页面的代码隐藏文件中运行函数
下面是代码隐藏中的函数:
Protected Function CalcBlockCode() As String
Dim strReturn As String
Dim paper As New DropDownList
Dim cylinder As New DropDownList
'Dim blockcode As New TextBox
paper = FormView1.FindControl("dropdownPaperItem")
cylinder = FormView1.FindControl("dropdownCylinderNumber")
'blockcode = CType(FormView1.FindControl("textBlockCode"), TextBox)
If paper.Text = "" Or paper.Text = "None" Then
CalcBlockCode = "NA"
Exit Function
End If
If cylinder.Text = "" Or cylinder.Text = "None" Then
CalcBlockCode = "NA"
Exit Function
End If
Dim strCellSizeCode As String
Dim intMil As Decimal
Dim strCylinderID As String
strCellSizeCode = DLookup("CellSizeCode", "Cylinders", "CYLINDERS = '" & cylinder.Text & "'")
intMil = DLookup("Mil", "PaperPart", "ITEM_NBR = '" & paper.Text & "'")
strCylinderID = DLookup("CylinderID", "Cylinders", "CYLINDERS = '" & cylinder.Text & "'")
strReturn = Convert.ToInt32(intMil * 10) & strCellSizeCode & strCylinderID
CalcBlockCode = strReturn
End Function
在FirefoxWeb工具中,我没有看到任何证据表明jquery函数正在运行。如果它确实运行,我就不会得到返回值。我是否为ajax调用引用了正确的url以从代码隐藏中获取函数?首先在代码隐藏中:
Imports System.Web.Services
然后把这个放在你的函数上面,让你的函数公开共享
<WebMethod()> _
Public Shared Function CalcBlockCode() As String
'....
End Function
请使用
Return
而不是赋值给隐式函数变量并写入Exit函数
。甚至没有理由使用strReturn
…在c#中,您要调用的方法需要是public static
,并用WebMethod
属性修饰。我认为您的代码需要相同的代码。另外,由于该方法需要是静态的
,我认为您需要通过ajax调用传递相关值,而不是从该方法访问控件。这看起来很像SQL注入。另外,删除数据对象周围的引号。它应该是data:{}
$.ajax({
//...
data: {'paper': $('yourdropdown').val(), 'cylinder':$('yourotherdropdown')} // notice the removal of quotes from around {} as per the comment from @Archer
//...
})