passing any control as parameter in vb.net function

10,084

You need to send ctrl as CONTROL(its a base class for all controls) as parameter.

You need to use latebinding and need to write separate cases for each control type....

Below code will work for checkbox only

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

For textbox and radiobutton you need write additional code

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
Share:
10,084
user1819920
Author by

user1819920

Updated on June 04, 2022

Comments

  • user1819920
    user1819920 almost 2 years

    I have a following vb.net function as below in which i am passing a checkbox control name as parameter.Code is here

    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
    

    Problem is that i have to use same function with radiobutton also with text box and i don't want to write separate function for all type of controls.I want to detect the control as parameter and.For Example if i pass text box then function should behave like a text box and if radio then radion behaviour and if checkbox then same behaviour for this.I have these three control to pass the function and i want make a automatic detection method for these controls