在vb.net函数中传递任何控件作为参数

时间:2012-11-03 09:47:52

标签: vb.net function parameters controls

我有一个以下的vb.net函数,我在其中传递一个复选框控件名称作为参数.Code在这里

Public Function emaildata(ByVal grdv As GridView, ByVal ctrl As String, ByVal celpos As Integer) As GridView
        Dim comm As OleDbCommand = New OleDbCommand()
        Dim bpv As String = ""
        Dim gv As New GridView
        For Each gvrow As GridViewRow In grdv.Rows
            Dim chkbx As CheckBox = CType(gvrow.FindControl(ctrl), CheckBox)
            If chkbx.Checked Then
                If bpv <> "" Then
                    bpv += ","
                End If
                bpv += gvrow.Cells(celpos).Text
                comm.CommandText = "SELECT chq_num Cheque#,to_char(bpv_amt,'9,999,999,999') Amount,vch_nar Narration,bnf_nam PartyName,acc_des Bank from  CHECK_DATA where bpv_num in(" & bpv.ToString() & ") and BPV_DTE=to_date('" & TreeView2.SelectedValue & "')"
                comm.CommandType = CommandType.Text
                comm.Connection = con
                Dim da As New OleDbDataAdapter(comm)
                Dim ds As New DataSet
                da.Fill(ds)
                gv.DataSource = ds
                gv.DataBind()
            End If
        Next
        Return gv
    End Function

问题是我必须使用带有文本框的radiobutton相同的功能,我不想为所有类型的控件编写单独的函数。我想将控件检测为参数和。例如,如果我传递文本框然后功能应该像一个文本框,如果无线电然后辐射行为和如果复选框然后相同的行为为此。我有这三个控制传递功能,我想为这些控件做一个自动检测方法

1 个答案:

答案 0 :(得分:1)

你需要发送ctrl作为CONTROL(它是所有控件的基类)作为参数。

您需要使用后期绑定,并且需要为每种控件类型编写单独的案例....

以下代码仅适用于复选框

Dim chkbx As CheckBox = CType(gvrow.FindControl(ctrl), CheckBox)
If chkbx.Checked Then

对于textbox和radiobutton,您需要编写其他代码

Public Function emaildata(ByVal grdv As GridView, ByVal ctrl As Control, ByVal celpos As Integer) As GridView

 If TypeOf ctrl Is Button Then

 ElseIf TypeOf ctrl Is RadioButton Then 

 Else

 EndIf