Click here to Skip to main content
14,666,459 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all,

I create a web user control and added it dynamically. On the
usc I have a dropdownlist.

My issue is that I am trying to add a handler when the usc
is added dynamically for dropdownlist SelectedIndexChanged.

This is what I tried:

Dim Cntrl_cbo As New Control

Private Sub CreateBDTable()
        'BD_PH.Controls.Clear()
        Dim iBD_RowCounter As Integer = 0
        Dim tblRows As Integer = 7
        Dim tblCols As Integer = 1
        Dim tblBD As Table = New Table()
        tblBD.ID = "tblBD"
        BD_PH.Controls.Add(tblBD)
        For i As Integer = 0 To tblRows - 1
            iBD_RowCounter = iBD_RowCounter + 1
            Dim tr As TableRow = New TableRow()
            For j As Integer = 0 To tblCols - 1
                Dim tc As TableCell = New TableCell()
                If iBD_RowCounter = 1 Then
                    OP_Cntrl_cbo.lblDescription_PM = "Bank"
                    OP_Cntrl_cbo.cboInput_PM = "cboBD_1"
                    OP_Cntrl_cbo.div_PM = "First"
                    Cntrl_cbo = LoadControl("~/US Controls/OP_Cntrl_cbo.ascx")
                    Cntrl_cbo.ID = "cntrlBD_1"
                    tc.Controls.Add(Cntrl_cbo)

                    Dim cboInput As DropDownList = DirectCast(Cntrl_cbo.FindControl("cboBD_1"), DropDownList)
                    If cboInput.Text = "" Then
                        Label12.Text = "Not Successfull!"
                    Else
                        Label12.Text = "Successfull"
                    End If
                    AddHandler cboInput.SelectedIndexChanged, AddressOf Cntrl_cbo_SelectedIndexChanged

                ElseIf iBD_RowCounter = 2 Then
                    OP_Cntrl_REQ.lblDescription_PM = "Specify Bank Name"
                    OP_Cntrl_REQ.txtInput_PM = "txtBD_1"
                    OP_Cntrl_REQ.rfvInput_PM = "Required!"
                    OP_Cntrl_REQ.rfvCntrlToValidate_PM = "txtBD_1"
                    OP_Cntrl_REQ.div_PM = ""
                    Cntrl_ReqV = LoadControl("~/US Controls/OP_Cntrl_REQ.ascx")
                    Cntrl_ReqV.ID = "cntrlBD_2"
                    tc.Controls.Add(Cntrl_ReqV)
                ElseIf iBD_RowCounter = 3 Then
                    OP_Cntrl_Add_Empty.lblDescription_PM = "Branch Address"
                    OP_Cntrl_Add_Empty.txtInput_StreetAndNo_PM = "txtBD_2"
                    OP_Cntrl_Add_Empty.txtInput_City_PM = "txtBD_3"
                    OP_Cntrl_Add_Empty.txtInput_Suburb_PM = "txtBD_4"
                    OP_Cntrl_Add_Empty.div_PM = ""
                    Cntrl_Add_Empty = LoadControl("~/US Controls/OP_Cntrl_Add_Empty.ascx")
                    Cntrl_Add_Empty.ID = "cntrlBD_3"
                    tc.Controls.Add(Cntrl_Add_Empty)
                ElseIf iBD_RowCounter = 4 Then
                    OP_Cntrl_ReqRgx.lblDescription_PM = "Branch Number"
                    OP_Cntrl_ReqRgx.txtInput_PM = "txtBD_5"
                    OP_Cntrl_ReqRgx.rfvInput_PM = "Required!"
                    OP_Cntrl_ReqRgx.rgxInput_PM = "Numbers only!"
                    OP_Cntrl_ReqRgx.rfvCntrlToValidate_PM = "txtBD_5"
                    OP_Cntrl_ReqRgx.rgxCntrlToValidate_PM = "txtBD_5"
                    Cntrl_ReqRgx = LoadControl("~/US Controls/OP_Cntrl_ReqRgx.ascx")
                    Cntrl_ReqRgx.ID = "cntrlBD_4"
                    tc.Controls.Add(Cntrl_ReqRgx)
                ElseIf iBD_RowCounter = 5 Then
                    OP_Cntrl_ReqRgx.lblDescription_PM = "Account Number"
                    OP_Cntrl_ReqRgx.txtInput_PM = "txtBD_6"
                    OP_Cntrl_ReqRgx.rfvInput_PM = "Required!"
                    OP_Cntrl_ReqRgx.rgxInput_PM = "Numbers only!"
                    OP_Cntrl_ReqRgx.rfvCntrlToValidate_PM = "txtBD_6"
                    OP_Cntrl_ReqRgx.rgxCntrlToValidate_PM = "txtBD_6"
                    Cntrl_ReqRgx = LoadControl("~/US Controls/OP_Cntrl_ReqRgx.ascx")
                    Cntrl_ReqRgx.ID = "cntrlBD_5"
                    tc.Controls.Add(Cntrl_ReqRgx)
                ElseIf iBD_RowCounter = 6 Then
                    OP_Cntrl_REQ.lblDescription_PM = "Account Name"
                    OP_Cntrl_REQ.txtInput_PM = "txtBD_7"
                    OP_Cntrl_REQ.rfvInput_PM = "Required!"
                    OP_Cntrl_REQ.rfvCntrlToValidate_PM = "txtBD_7"
                    OP_Cntrl_REQ.div_PM = ""
                    Cntrl_ReqV = LoadControl("~/US Controls/OP_Cntrl_REQ.ascx")
                    Cntrl_ReqV.ID = "cntrlBD_6"
                    tc.Controls.Add(Cntrl_ReqV)
                ElseIf iBD_RowCounter = 7 Then
                    OP_Cntrl_REQ.lblDescription_PM = "Type Of Account"
                    OP_Cntrl_REQ.txtInput_PM = "txtBD_8"
                    OP_Cntrl_REQ.rfvInput_PM = "Required!"
                    OP_Cntrl_REQ.rfvCntrlToValidate_PM = "txtBD_8"
                    OP_Cntrl_REQ.div_PM = "Last"
                    Cntrl_ReqV = LoadControl("~/US Controls/OP_Cntrl_REQ.ascx")
                    Cntrl_ReqV.ID = "cntrlBD_7"
                    tc.Controls.Add(Cntrl_ReqV)
                End If
                tr.Cells.Add(tc)
            Next j
            tblBD.Rows.Add(tr)
            If iBD_RowCounter = "2" Then
                tr.Visible = False
            End If
        Next i
        ViewState("dynamictable") = True
    End Sub

Private Sub Cntrl_cbo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        btnNext.Text = "Success"
    End Sub
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

The dynamically loaded control will not hold the view state. so, you have to re-create the control every time in Page Init event on every postback. so, then you may get the events fired.

check these links for more on loading controls.
Keep Sites Running Smoothly By Avoiding These 10 Common ASP.NET Pitfalls[^]
Dynamically Loading User Controls[^]
Dynamic Loading of ASP.NET User Controls[^]

hope it helps.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100