1

我正在尝试通过 jQuery 运行 webmethod,它将根据一对级联保管箱的内容查询 SQL 数据库。我尝试调用该方法并使用各种 jQueries 获取结果,并在某一时刻使用 UpdatePanel 尝试使其工作,但在每次尝试结束时,我似乎也停留在同一点,所有代码都运行没有错误但是传递给最终标签文本的结果字符串是空白的。

我猜我可能错过了 WebMethod 中一些非常基本的东西,并想知道是否有人能指出我正确的方向:

数据库中的目标表(称为驱动程序)有 4 列:

model_id , 整数, 应该是 ddlModel.SelectedItem.Value model
, varchar (255), 应该是
ddlModel.SelectedItem.Text为了允许记录创建页面正确运行),它包含一个 1 或 0 来定义是否应在结果字符串
subs中使用 subs 字段,一个 varchar (255),其中包含要添加到结果字符串的数据如果驱动程序为 0

我没有详细说明用于填充级联下拉框的表格,因为它们似乎运行正常,但是如果需要此信息或任何其他信息,请随时询问。

我的代码如下:

打印机.asmx.vb

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports AjaxControlToolkit
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

<WebService(Namespace:="http://printers.mydomainname.com/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class printers
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function GetMake(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strMakeQuery As String = "SELECT * FROM manufacturers ORDER BY make ASC"
    Dim cmdFetchMake As SqlCommand = New SqlCommand(strMakeQuery, sqlConn)

    Dim dtrMake As SqlDataReader
    Dim myMake As New List(Of CascadingDropDownNameValue)

    sqlConn.Open()
    dtrMake = cmdFetchMake.ExecuteReader

    While dtrMake.Read()
        Dim strMakeName As String = dtrMake("make").ToString
        Dim strMakeId As String = dtrMake("make_id").ToString

        myMake.Add(New CascadingDropDownNameValue(strMakeName, strMakeId))
    End While

    Return myMake.ToArray
End Function

<WebMethod()> _
Public Function GetModel(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strModelQuery As String = "SELECT * FROM printers WHERE make_id = @makeid"
    Dim cmdFetchModel As SqlCommand = New SqlCommand(strModelQuery, sqlConn)

    Dim dtrModel As SqlDataReader
    Dim kvModel As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

    Dim intMakeId As Integer

    If Not kvModel.ContainsKey("make") Or Not Int32.TryParse(kvModel("make"), intMakeId) Then
        Return Nothing
    End If

    cmdFetchModel.Parameters.AddWithValue("@makeid", intMakeId)
    Dim myModel As New List(Of CascadingDropDownNameValue)

    sqlConn.Open()
    dtrModel = cmdFetchModel.ExecuteReader

    While dtrModel.Read()
        Dim strModelName As String = dtrModel("model").ToString
        Dim strModelId As String = dtrModel("model_id").ToString

        myModel.Add(New CascadingDropDownNameValue(strModelName, strModelId))
    End While

    Return myModel.ToArray
End Function

<WebMethod()> _
Public Function GetDriver(ByVal model As String) As String
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strDriverQuery As String = "SELECT * FROM drivers WHERE model = @model"
    Dim cmdFetchDriver As SqlCommand = New SqlCommand(strDriverQuery, sqlConn)

    Dim dtrDriver As SqlDataReader
    Dim intModel As Integer

    cmdFetchDriver.Parameters.AddWithValue("@model", intModel)
    Dim strResult As String = "The selected printer is"

    sqlConn.Open()
    dtrDriver = cmdFetchDriver.ExecuteReader

    dtrDriver.Read()
    Dim intDriver As Integer = dtrDriver("driver")
    Dim strSubs As String = dtrDriver("subs").ToString

    If intDriver = 1 Then
        strResult = strResult + "fully compatible with the Windows 2003 Hosted platform."
    Else
        strResult = strResult + "supported on the Windows 2003 Hosted platform via a subsituted driver:" + strSubs
    End If

    Return strResult

End Function

End Class

默认.aspx

<%@ Page Language="VB" AutoEventWireup="false" EnableEventValidation="false" Inherits="Printer_Compatibility_Matrix_VB._Default" Codebehind="Default.aspx.vb" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

<title>Printer Compatibility Matrix</title>
<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    function CallService() {
        $.ajax({
            type: "POST",
            url: "printers.asmx/GetDriver",
            data: $("#ddlModel option:selected").text(),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError
        });
    }

    function OnSuccess(data, status) {
        $("#lblResult").html(data.d);
    }

    function OnError(request, status, error) {
        $("#lblResult").html(request.statusText);
    }
</script>

</head>
<body>        

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
    <Services>
        <asp:ServiceReference Path="printers.asmx" />
    </Services>
</asp:ScriptManager>
<div>

    Manufacturer:  <asp:DropDownList ID="ddlMake" runat="server" Width="170" /><br />
    Printer:  <asp:DropDownList ID="ddlModel" runat="server" Width="170" /><br />

    <asp:Button ID="btnDriver" Text="Submit" OnClientClick="CallService(); return false;" runat="server" />
    <asp:Label ID="lblResult" Text="&nbsp;" Width="100%" runat="server" />


    <cc1:CascadingDropDown 
        id="CascadingDropDown1" 
        runat="server"
        category="Make"
        prompttext="Select a Manufacturer..."
        ServiceMethod="GetMake"
        ServicePath="printers.asmx"
        TargetControlId="ddlMake"
    />

    <cc1:CascadingDropDown 
        id="CascadingDropDown2" 
        runat="server" 
        category="Model"
        prompttext="Select a Printer..." 
        ServiceMethod="GetModel" 
        ServicePath="printers.asmx"
        TargetControlId="ddlModel"
        ParentControlId="ddlMake"
    />      
</div>
</form>
</body>
</html>

非常感谢您的时间,

吉姆

编辑

似乎除了 Frédéric 发现的错误之外,我还忘记引用 jQuery,我已经根据这些错误修改了上面的代码以匹配我当前的迭代。

在其当前状态下,它现在将“内部服务器错误”返回到标签文本中。我怀疑这与我的 java 有关,因为甚至尝试使用此代码调用简单的“Hello World”都会向标签返回空白结果。

4

1 回答 1

0

我现在已经解决了这个问题,我在传递 $("#ddlModel").val() 而不是文本方面做了更多的摆弄,并设法让它正确响应。

对于其他有类似问题的人,我修改后的工作功能如下:

打印机.asmx.vb

<WebMethod()> _
Public Function GetDriver(ByVal model_id As Integer)
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("PrinterConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strDriverQuery As String = "SELECT * FROM drivers WHERE model_id = @model_id"
    Dim cmdFetchDriver As SqlCommand = New SqlCommand(strDriverQuery, sqlConn)

    Dim dtrDriver As SqlDataReader
    Dim intModel As Integer

    cmdFetchDriver.Parameters.AddWithValue("@model_id", intModel)
    cmdFetchDriver.Parameters("@model_id").Value = model_id

    Dim strResult As String = ""

    sqlConn.Open()
    dtrDriver = cmdFetchDriver.ExecuteReader

    dtrDriver.Read()
    Dim intDriver As Integer = dtrDriver("driver")
    Dim strSubs As String = dtrDriver("subs").ToString

    If intDriver = 1 Then
        strResult = "<font color=11CC11>The selected printer is fully compatible.</font>"
    ElseIf intDriver = 0 Then
        strResult = "<font color=FF9933>The selected printer may be compatible via the substitute print driver: " + strSubs + "</font>"
    End If

    Return strResult

End Function

默认.aspx

<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    function CallService() {
        $.ajax({
            type: "POST",
            url: "printers.asmx/GetDriver",
            data: "{ 'model_id': " + $("#ddlModel :selected").val() + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnError
        });
    }

    function OnSuccess(data, status) {
        $("#lblResult").html(data);
    }

    function OnError(request, status, error) {
        $("#lblResult").html("<font color=#FF0000>Please make a valid selection.<font>");
    }
</script>
于 2010-12-16T10:49:20.367 回答