VB.Net传递/调用变量

时间:2013-04-23 14:32:33

标签: asp.net vb.net

如何将值传递给模块/函数。每当我在这里搜索或谷歌时,我都会获得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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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。