0

Asp.Net 4.0

コード ビハインドで親ページからユーザー コントロールの関数を呼び出すことは可能ですか? ユーザーコントロールは他のプログラマーによって作成されますが、それぞれに、メインページに必要な値を返す「出力」と呼ばれる共通のパブリック関数があります。メイン ページにはメイン メニューがあるため、メイン メニューの選択に基づいて 1 つのユーザー コントロールのみが表示されます。

ユーザー コントロールを含む WebApp 内のフォルダー:

> UserControls
  ProductA.ascx
  ProductB.ascx
  ProductC.ascx
  ProductD.ascx
  etc...

ユーザーがメニュー ボタンをクリックしたときのコード ビハインド:

Dim product As string = Session("MenuProduct")
Dim uc As UserControl
uc = LoadControl("~/UserControls/" & product & ".ascx")
InputPanel.Controls.Add(uc)

アクセスしたいユーザーコントロールの機能。これは共通の関数になります。

Public Function Output(ByVal ParamArray expr() As Object) As Object

   ...code

End Function
4

2 に答える 2

0

適切なアクセス修飾子があれば、親ページから userControl の関数を呼び出すことができます。パブリック 私は信じています

Dim product As string = Session("MenuProduct")
Dim uc As UserControl
uc = LoadControl("~/UserControls/" & product & ".ascx")
InputPanel.Controls.Add(uc)

if (uc is TypeWithMethod )
{
    Dim ucTyped As TypeWithMethod
    ucTyped = uc As TypeWithMethod
    ucTyped.Method()
}

Vb 構文はさびており、入れ続けました。すべての行の後。

于 2013-03-29T19:40:35.887 に答える
0

アンドリューが話していることの詳細を以下に示します...

Public Interface IGroupable
    Function GetGroupId() As Int32
End Interface


Class MyUserControl
    Inherits UserControl
    Implements IGroupable
    Public Function GetGroupId() As Integer Implements IGroupable.GetGroupId
        Return 1
    End Function
End Class

Class MyPage
    Inherits Page
    Dim id As Int32 = DirectCast(myUserControl, IGroupable).GetGroupId()
End Class

したがって、基本的にはインターフェイスを作成し、すべてのユーザー コントロールにそのインターフェイスを実装させます。次に、Web ページ内にユーザー コントロールをロードするときに、それらをインターフェイス名の型にキャストするだけです。インターフェイスは次のようになります。関数へのアクセス。

于 2013-03-30T07:38:45.487 に答える