0
votes

I'm trying to add a new section on one of my system's main pages (web forms) that would allow users to see documents they've uploaded into folders they've created. I don't know in advance how many folders they've set up or what the folders are named. I'm trying to achieve this by a GridView with a series of dynamically-generated LinkButtons above it, with a LinkButton for each folder. When the user clicks a LinkButton I will rebind the grid so that it shows documents in that folder. I was hoping to use a tab control to represent the folders but apparently that isn't available in .NET 3.5. I am coding in VB.NET.

The issue I'm running into is adding an OnClick event handler dynamically. The code I am using is basically implementing an OnClientClick event. If I add a Javascript function called lnkFolder_Click with an alert it is triggering that function, when I need it to trigger a server-side function to rebind the grid. I've seen many pages on this problem with various suggestions, including placing the code in the page's PreInit event. None of these suggestions worked for me, so I'm hoping the StackExchange community can help nudge me in the right direction as it has in the past! The following code is what I'm trying and is located in the page load event.

Thanks in advance!

        Dim dtFolders As DataTable
        Dim i As Integer

        dtFolders = DAL.GetClientFolders(Session("ClientKey"))
        If dtFolders.Rows.Count > 0 Then
            For i = 0 to dtFolders.Rows.Count - 1
                Dim lnkFolder As LinkButton = New LinkButton
                lnkFolder.ID = "lnkFolder" & i.ToString
                lnkFolder.Text = dtFolders.Rows(i)("FolderName")
                lnkFolder.CommandArgument = i
                lnkFolder.Attributes.Add("OnClick", "lnkFolder_Click()")
                'lnkFolder.OnClientClick += New EventHandler(lnkFolder_Click)
                phFolders.Controls.Add(lnkFolder)                  
            Next
        End If

The lnkFolder_Click function is just "stubbed-out" as follows:

Protected Sub lnkFolder_Click(sender As Object, e As EventArgs) Handles lnkFolder.Click

        Dim i As Integer

        i = 1

    End Sub
1
AddHandler lnkFolder.Click, AddressOf Me.lnkFolder_Click will do the trick via postback - alessandro
Thanks for the quick response! I added the line lnkFolder.Attributes.Add("lnkFolder.Click", "AddressOf Me.lnkFolder_Click") but it still is not triggering the server-side function. - Mike
can you please provide the signature of your lnkFolder_Click function? - alessandro
any error in browser console? - alessandro
No errors, InvernoMuto. Added function signature to OP. Sorry, my bad for not including it originally. - Mike

1 Answers

0
votes

You can add your server-side callback with AddHandler Statement

Associates an event with an event handler at run time.

AddHandler event, AddressOf eventhandler

The AddHandler statement connects procedures to events at run time.

Dim dtFolders As DataTable
        Dim i As Integer

        dtFolders = DAL.GetClientFolders(Session("ClientKey"))
        If dtFolders.Rows.Count > 0 Then
            For i = 0 to dtFolders.Rows.Count - 1
                Dim lnkFolder As LinkButton = New LinkButton
                lnkFolder.ID = "lnkFolder" & i.ToString
                lnkFolder.Text = dtFolders.Rows(i)("FolderName")
                lnkFolder.CommandArgument = i

                AddHandler lnkFolder.Click, AddressOf Me.lnkFolder_Click 

                phFolders.Controls.Add(lnkFolder)                  
            Next
        End If

Protected Sub lnkFolder_Click(sender As Object, e As EventArgs) 

  Dim i As Integer

  i = 1

End Sub