【问题标题】:how to use webmethod on webform in asp.net如何在asp.net的webform上使用webmethod
【发布时间】:2014-11-30 17:51:04
【问题描述】:

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

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

界面

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

这是我必须在用户控件中使用的网络方法来传递参数

 <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

我被困在这里了。

【问题讨论】:

    标签: asp.net vb.net webmethod


    【解决方案1】:

    您不能在用户控件上使用网络方法。网络方法必须存在于网页上。由于 UserControl 可以存在于多个页面上,因此您不能将 web 方法放在 UserControl 的代码隐藏页面中。

    所以你的选择是:

    • 为您的 webmethod 创建一个 .asmx 并从您的 webcontrol (old-skool) 中调用它
    • 创建一个 WCF 端点 (.svc) 并从您的 webcontrol (semi-old-skool) 中调用它
    • 创建一个 WebAPI 控制器并从您的 webcontrol (new-skool) 中调用它

    但是,您要做的是从用户控件的代码隐藏中调用 Web 服务。如果你想这样做,你正在混合服务器端和客户端。您的 asmx 可以从客户端使用。您要么需要从 asmx 复制代码,要么提供可用于 web 服务和用户控件的抽象。

    【讨论】:

    • 您正在混合服务器端和客户端。从服务器端调用本地 Web 服务是没有用的。 asmx 可以从客户端调用。您可以将代码从 asmx 复制到 submit 方法,或者将方法抽象到某个共享位置。
    • 你能举个例子吗?
    猜你喜欢
    • 2023-03-26
    • 2021-11-12
    • 1970-01-01
    • 2013-09-06
    • 2015-07-25
    • 2017-06-29
    • 2013-10-07
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多