• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

asp.net dynamic control handler question

Bigbacon

Fully [H]
2FA
Joined
Jul 12, 2007
Messages
22,641
hopefully I explain this correctly.

I have a function in a class file that builds a bunch of dynamic controls. I need to add a handler to some of these controls to call a sub routine in the code behind page. How is this accomplished? I just need to know how I do addhandler control.click, aspx.vb.subroutine.

My sub routine is defined as a public shared sub.

so my class file is call expReport.vb and I need to be able to add a handler to a sub in a file called start.aspx.vb. Sub routine is called showMenuList.
 
Here's how I'd do it in c#. Should be a simple translation to vb.net.

If I understood you correctly, the class start contains a shared method called ShowMenuList. This code is based on that assumption.

Code:
        /// <summary>
        /// Runs when Page initializes, before ViewState is loaded for any child Controls.
        /// </summary>
        /// <param name="sender">The current page</param>
        /// <param name="e">Page event args</param>
        protected void Page_Init(object sender, System.EventArgs e)
        {
            BuildControls();
        }

        /// <summary>
        /// Creates all my dynamic controls.
        /// </summary>
        private void BuildControls()
        {
            Button b = new Button();
            b.ID = "dynamicButton";
            b.Click += new EventHandler(b_Click);
            b.Text = "Click Me!";
            this.Controls.Add(b);
        }

        /// <summary>
        /// Handles a button being clicked.
        /// </summary>
        /// <param name="sender">The button that was clicked</param>
        /// <param name="e">Button event args</param>
        void b_Click(object sender, EventArgs e)
        {
            Start.ShowMenuList();
        }
 
they are in seperate files though. The function that creates the dynamic control is in it's own class file where as the event handler sub routine is just in the code behind file which is there the function was called from.

So I have start.aspx/start.aspx.vb

Something in that page says call function dayInput.

dayInput creates dynamic control

dynamic control needs a handler that calls the sub routine showMenuList that is in start.aspx.vb

If both things were in the same file of code, it would not be an issue. I really may not be explaining this correctly.
 
Is the other class an ascx control or something? If not, how does it reference the form on the Start.aspx page to add the control?

Code:
Partial Class Start
    Inherits System.Web.UI.Page

    Private Property DayOfWeekButtonCreated() As Boolean
        Get
            Return Session("DayOfWeekButtonCreated")
        End Get
        Set(ByVal value As Boolean)
            Session("DayOfWeekButtonCreated") = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init

        If (Not Page.IsPostBack) Then
            DayOfWeekButtonCreated = False
        End If

        If DayOfWeekButtonCreated Then
            CreateDayInput(DateTime.Today.DayOfWeek)
        End If

    End Sub

    Protected Sub ButtonCreateDayInput_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonCreateDayInput.Click
        CreateDayInput(DateTime.Today.DayOfWeek)
        DayOfWeekButtonCreated = True
    End Sub

    Protected Sub CreateDayInput(ByVal day As DayOfWeek)

        Dim dynamicButton As New Button()
        dynamicButton.ID = "dynamicButton"
        dynamicButton.Text = day.ToString()
        AddHandler dynamicButton.Click, AddressOf DynamicButton_Click
        form1.Controls.AddAt(Controls.IndexOf(ButtonCreateDayInput), dynamicButton)
    End Sub

    Protected Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ShowMenuList()
    End Sub

    Protected Sub ShowMenuList()
        'Show the menu here

    End Sub

End Class
 
no it's not an ascx.

its just a dynamic base control

Dim lb as new imagebutton
lb.id = "whatever"

I need to get

addhandler lb.click, addressOf showMenuItem (which is the routine on start.aspx.vb.

Does that make more sense? if not I'll get more of the code.
 
addhandler lb.click, addressOf showMenuItem would only work if showMenuItem has parameters of (Object,EventArgs) in that order. That's why I was calling the method from within the event handler.

Further, you would only be able to handle the event if you re-created the image button on the page_init on the next postback. If you don't, the image button's view state won't get loaded and the event won't show up.
 
Back
Top