添加带有事件的动态Web用户控件 [英] Adding dynamic web user control with events

查看:82
本文介绍了添加带有事件的动态Web用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我创建一个Web用户控件并将其动态添加.在
我有一个下拉列表.

我的问题是,当usc
时,我试图添加一个处理程序. 为下拉列表SelectedIndexChanged动态添加.

这是我尝试过的:

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

推荐答案



动态加载的控件将不保持视图状态.因此,您必须在每次回发的每次Page Init事件中都重新创建控件.因此,您可能会触发事件.

请查看这些链接以获取有关加载控件的更多信息.
通过避免这10个常见的ASP.NET陷阱来保持站点平稳运行 [^ ]
动态加载用户控件 [动态加载ASP.NET用户控件 [ ^ ]

希望对您有所帮助.
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.


这篇关于添加带有事件的动态Web用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆