如何在asp.net中的webform上使用webmethod

时间:2014-11-30 17:51:04

标签: asp.net vb.net webmethod

我的网络表单上有一个用户控件,如下所示 enter image description here

我选择了程序年份,类别类型,类别和位置组,然后单击搜索,它应该返回一个集合,其中包含下拉列表中相应所选项目的programyearID,categorytypeID和positionGroupID,并将其传递给webmethod已定义为获取特定的认证ID。

界面

Namespace SI.Certification.UserControl
    Public Interface IUserSearchResultList
         Property DataSource() As User.Learning.Business.HR.UserCollection
         ReadOnly Property List() As User.Web.UI.WebControls.PagedRepeater
    End Interface 
End Namespace

这是我的usercontrol1.vb的代码

  Public Class curriculum_search
    Inherits System.Web.UI.UserControl
    Implements IUserSearchResultList

  Private _dataSource As UserCollection

  Public Property DataSource As UserCollection Implements UserControl.IUserSearchResultList.DataSource
        Get
            Return _dataSource
        End Get
        Set(value As UserCollection)

        End Set
    End Property

    Public ReadOnly Property List As PagedRepeater Implements UserControl.IUserSearchResultList.List
        Get
            Return ctlListControl
        End Get
    End Property

 #Region  "Public Properties"
 Public Property ProgramYearID() As Int32
        Get
            If Me.ShowProgramYearSearch Then
                If Me.lstProgramYear.SelectedValue = 0 Then
                    Return Integer.MinValue
                Else
                    Return Me.lstProgramYear.SelectedValue
                End If
            Else
                If ViewState("ProgramYearID") Is Nothing Then
                    Return Integer.MinValue
                Else
                    Return ViewState("ProgramYearID")
                End If
            End If
        End Get
        Set(ByVal Value As Int32)
            If Me.ShowProgramYearSearch Then
                Me.lstProgramYear.SelectedValue = Value.ToString()
            End If
            ViewState("ProgramYearID") = Value
        End Set
    End Property

 Public ReadOnly Property CategoryTypeID() As Int32
        Get
            If Me.lstCategoryType.SelectedValue = 0 Then
                Return Integer.MinValue
            Else
                Return Me.lstCategoryType.SelectedValue
            End If
        End Get
    End Property

 Public ReadOnly Property CategoryID() As Int32
        Get
            If Me.lstCategory.SelectedValue = 0 Then
                Return Integer.MinValue
            Else
                Return Me.lstCategory.SelectedValue
            End If
        End Get
    End Property

 Public Property PositionGroupID() As Int32
        Get
            If Me.ShowPositionCodeSearch Then
                If Me.lstPositionGroup.Selected = -1 Then
                    Return Integer.MinValue
                Else
                    Return Me.lstPositionGroup.Selected
                End If
            Else
                If ViewState("PositionGroupID") Is Nothing Then
                    Return Integer.MinValue
                Else
                    Return ViewState("PositionGroupID")
                End If
            End If
        End Get
        Set(ByVal Value As Int32)
            If Me.ShowPositionCodeSearch Then
                Me.lstPositionGroup.Selected = Value.ToString()
            End If
            ViewState("PositionGroupID") = Value

        End Set
    End Property

Public Property ShowPositionCodeSearch() As Boolean
        Get
            If ViewState("ShowPositionCodeSearch") Is Nothing Then
                Return True
            Else
                Return ViewState("ShowPositionCodeSearch")
            End If
        End Get
        Set(ByVal Value As Boolean)
            ViewState("ShowPositionCodeSearch") = Value
            If Not Value Then
                spnPositionGroup.Visible = False
            Else
                spnPositionGroup.Visible = True
            End If
        End Set
    End Property

 Public Property ShowProgramYearSearch() As Boolean
        Get
            If ViewState("ShowProgramYearSearch") Is Nothing Then
                Return True
            Else
                Return ViewState("ShowProgramYearSearch")
            End If
        End Get
        Set(ByVal Value As Boolean)
            ViewState("ShowProgramYearSearch") = Value
            If Not Value Then
                spnProgramYear.Visible = False
            End If
        End Set
    End Property
   #End Region

   #Region "Events"
    Public Event Submit(ByVal sender As Object, ByVal e As System.EventArgs)
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load

        If Page.IsPostBack = False Then
            If spnCategoryType.Visible = True Then
                BindCertificationCategoryTypeCollection()
            'method to bind the category collection
            End If
            If spnProgramYear.Visible = True AndAlso Me.ShowProgramYearSearch Then
                BindProgramYearCollection(GetProgramYearCollection())
            'method to bind the porgram year collection
            End If
            If spnPositionGroup.Visible = True AndAlso Me.ShowPositionCodeSearch Then
            End If
            lstPositionGroup.DefaultToPrimary = False
        End If
    End Sub

    Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlSearch.SearchClick
        RaiseEvent Submit(sender, e)
    End Sub

这是我必须在usercontrol中使用的web方法来传递参数

 <WebMethod(Description:="Get list of certification levels")> _
 Public Function GetCertificationLevelsList(ByVal programYearId As Integer, ByVal PositionGroupId As String, ByVal CategoryId As
String) As User.Department.Application.Curriculum.Items
     Dim items As User.Department.Application.Curriculum.Items =  User.Department.Application.Curriculum.CategoryCollection.GetCategoryLevelsList(programYearId,
PositionGroupId, CategoryId)
     items.Sort()
     Return items
 End Function

我被困在这里。

1 个答案:

答案 0 :(得分:0)

您无法在Usercontrols上使用webmethods。 Web方法必须存在于网页上。由于UserControls可以存在于多个页面上,因此您无法将webmethod放在UserControl的代码隐藏页面中。

所以你的选择是:

  • 为您的网络方法创建一个.asmx并从您的webcontrol(old-skool)调用它
  • 创建一个WCF点(.svc)并从你的webcontrol(半旧skool)中调用它
  • 创建一个WebAPI控制器并从您的webcontrol(new-skool)
  • 中调用它

但是,您要做的是从usercontrol的代码隐藏中调用Web服务。如果您想这样做,那么您可以将服务器端和客户端混合在一起。你的asmx是从客户端使用的。您将需要从asmx复制代码,或者提供可在webservice和usercontrol中使用的抽象。