0

如何将值传递给模块/函数。每当我在这里或谷歌搜索时,我都会得到 C# 而不是 VB.net 的代码

我希望能够单击一个按钮并将一个值传递给一个模块并对其进行操作。每个按钮都有一个值 1、2、3、4 等......它们会返回以使面板可见 = true/false。

即我想说下面的内容,但是当我单击 btnShow1 时,我想将其作为值传递给函数并隐藏/显示我正在谈论的面板。

当前代码

 Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click

        If panel1.Visible = True Then
            panel1.Visible = False
        Else
            panel1.Visible = True
        End If

    End Sub

猜码

 'Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
             vpanel = btnShowQ1.value
        Call fncPanel(vpanel as string) as string

    End Sub

然后在某个地方 - 我猜在 App_code 中我创建了 function.vb

Imports Microsoft.VisualBasic

Public Class ciFunctions
    Public Function fncPanel(vPanel as string)as string
            If (vPanel).visible = False then
                (vPanel).visible = true
            Else
                (vPanel).visible = false
             End IF 
    End Function
End Class
4

2 回答 2

0

您应该能够将 Panel 类型的参数添加到 sub,并在 btnShow 事件中传递该项目。

Public Sub ChangePanelVis(panel as Panel)
    If panel.Visible then
        panel.Visible = True
    Else
        panel.Visible = True
    End If
End Sub

Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
    ChangePanelVis(panel1)
End Sub
于 2013-04-23T14:54:25.293 回答
0

将按钮的 CommandArgument 属性设置为其显示或隐藏的面板编号:

<asp:Button ID="Button1" runat="server" Text="Show/Hide" CommandArgument="1" />
<asp:Button ID="Button2" runat="server" Text="Show/Hide" CommandArgument="2" />

在按钮单击事件上:

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, ...
    ' get the button which triggers this event
    Dim thisButton As Button = DirectCast(sender, Button) 

    ' construct the panel name to toggle visibility from the button's CommandArgument property
    Dim panelName as String = "panel" & thisButton.CommandArgument

    ' call the function, passing the panel name and the reference to current page
    TogglePanel(Me, panelName)
End Sub

您可以将所有按钮单击事件处理程序添加到此子程序,方法是将它们添加到 Handles... 列表中,如上所述。因此,您不必为页面上的每个按钮创建事件处理程序。只需一个即可处理所有问题。

创建函数来切换可见性:

Sub TogglePanel(pg As Page, panelName As String)
    ' get the panel control on the page
    Dim panel As Panel = DirectCast(pg.FindControl(panelName), Panel)

    ' toggle its visibility
    panel.Visible = Not panel.Visible
End Sub

pg如果将此函数放在一个类而不是它运行的页面或模块中,则该参数是必需的。如果函数位于同一个页面类中,则不需要 pg。

于 2013-04-23T15:21:57.847 回答