获取ASP.NEt函数以使用Ajax来填充DropDown列表

时间:2014-02-27 21:43:51

标签: jquery asp.net ajax vb.net webmethod

我正在尝试使用共享函数和Ajax与WebMethod来填充ans asp.net DropDown。我已经在页面中测试了该函数,而在UpdatePanel中使用按钮点击了DropDown并工作。

Public Function Update()

    Dim objDB As New DB

    Dim drProducts As SqlClient.SqlDataReader
    objDB.Conn.Open()
    drProducts = objDB.cmdGetProducts.ExecuteReader(CommandBehavior.CloseConnection)
    Do While drProducts.Read
        DDLProducts.Items.Add(New ListItem(drProducts.Item("Title"), drProducts.Item("ProductID")))
    Loop
    drProducts.Close()
    objDB.Conn.Close()

End Function


Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs)
        Update()
End Sub

当我尝试使用Ajax时,如果使用共享功能,我会在Web方法中获得“无法引用实例成员”,如果使用公共函数则无法工作

<WebMethod()> _
Public Shared Function UpdateList()

    Try
        Update()
        Return "Success"

    Catch ex As Exception
        Return "failure"
    End Try

End Function

这是一个jQuery脚本在对话框关闭时触发

    <script>
        $(function () {

            $("#MyDialog").dialog({
                autoOpen: false,
                height: 600,
                width: 300,
                modal: true,
                buttons: {
                    "Save": function () {
                        $.ajax({
                            type: 'POST',
                            contentType: "application/json; charset=utf-8",
                            url: '/AddProduct.aspx/UpdateList',
                            async: false,
                            success: function (response) {
                                alert("Record saved successfully in database");
                            },
                            error: function () {
                                alert("some problem in saving data");
                            }
                        });
                        $(this).dialog("close");
                    },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                },
                close: function () {

                }
            });

            $("#name").click(function () { $("#MyDialog").dialog("open"); });
        });
    </script>

任何想法如何用Ajax填充列表元素,如果可以引用WebMethod中的控件 - 我认为这是不可能的 - 如果有解决方法?

更新

我已经尝试了this solution但无法使用我的代码并且无法知道问题出在哪里

1 个答案:

答案 0 :(得分:0)

直接对此函数进行ajax调用url: '/AddProduct.aspx/Update'

Public Function Update() As String

   Dim objDB As New DB

   Dim drProducts As SqlClient.SqlDataReader
   Try
      objDB.Conn.Open()
      drProducts = objDB.cmdGetProducts.ExecuteReader(CommandBehavior.CloseConnection)
      Do While drProducts.Read
          DDLProducts.Items.Add(New ListItem(drProducts.Item("Title"), drProducts.Item("ProductID")))
      Loop
      drProducts.Close()
      objDB.Conn.Close()
      Return "Success"

   Catch ex As Exception
       Return "failure"
   End Try
 End Function

<强> AJAX

                   $.ajax({
                        type: 'POST',
                        url: '/AddProduct.aspx/Update',
                        async: false,
                        success: function (response) {
                            alert("Record saved successfully in database");
                        },
                        error: function () {
                            alert("some problem in saving data");
                        }
                    });

更新按钮

 Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim result As String = Update()
    If(result = "failure") Then
     'Show some kind of error
    End If
 End Sub