我正在钻研我不熟悉的领域。我正在创建一个我想在多个表单中使用的用户控件。 (因此用户控制的原因)。我只熟悉VB.NET语言。我用谷歌搜索,我认为我接近理解,但我无法解决这个问题。我的用户控件上有多个按钮。当"上传"在我的用户控件上按下按钮,我希望在我的父页面上运行一个名为UploadFile()的Sub,它有一个字符串参数。我会在我的页面上多次使用UserControl:
<uc1:UploadFile ID="UploadFile1" runat="server" />
<uc1:UploadFile ID="UploadFile2" runat="server" />
在我的父页面后面的代码中,我想在用户控件中的上传按钮的Click事件上执行此功能。然后,唯一参数将告诉我将上传文件的文件夹。因此,当用户在页面上的第一个用户控件中上传文件时,它将在我的父页面上运行我的上传子,获取参数&#34; a&#34;并运行我的Sub上传我的文档并保存到数据库以获取特定于&#34; a&#34;类型。然后,我将告诉用户控件更改它的标签以显示文件A已上载
Sub something somethingUploadFile1_Click
UploadFile("a")
End Sub
Sub something somethingUploadFile2_Click
UploadFile("b")
End Sub
Sub UploadFile(ByVal myString as string)
Select Case myString
Case "a"
If UploadFile1.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile1.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
Case "b"
If UploadFile2.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile2.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
End Select
End Sub
我知道在上面的代码中,我应该以某种方式引用UserControl上的按钮,但我不知道该怎么做。我也知道提升事件和事件代表团是这方面的两个主要组成部分,但同样,我还没有搞清楚。您能否告诉我如何使用我提供的现有示例完成我的代码?
以下是我现有UserControl背后的完整代码:
Public Class UploadFile
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
End Sub
Protected Sub btnDelete_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnDelete.Click
End Sub
Public Property lblViewFile() As Label
Get
Return _lblViewFile
End Get
Set(ByVal value As Label)
_lblViewFile = value
End Set
End Property
Public Property btnDelete() As ImageButton
Get
Return _btnDelete
End Get
Set(ByVal value As ImageButton)
_btnDelete = value
End Set
End Property
Public Property pnlUpload() As Panel
Get
Return _pnlUpload
End Get
Set(ByVal value As Panel)
_pnlUpload = value
End Set
End Property
Public Property FileUpload() As FileUpload
Get
Return _FileUpload
End Get
Set(ByVal value As FileUpload)
_FileUpload = value
End Set
End Property
Public Property btnUpload() As Button
Get
Return _btnUpload
End Get
Set(ByVal value As Button)
_btnUpload = value
End Set
End Property
Public Property lblReplaceMsg() As Label
Get
Return _lblReplaceMsg
End Get
Set(ByVal value As Label)
_lblReplaceMsg = value
End Set
End Property
Private _lblViewFile As Label
Private _btnDelete As ImageButton
Private _pnlUpload As Panel
Private _FileUpload As FileUpload
Private _btnUpload As Button
Private _lblReplaceMsg As Label
End Class
答案 0 :(得分:0)
asp.net vb user control raising an event on the calling page
根据您的结构和数据从EventArgs派生您需要的参数类并使用它。