Dynamic button not working with AddHandler in VB.NET

会有一股神秘感。 提交于 2019-12-23 04:24:49

问题


I am working on a project in which I am trying to create a button dynamically in the code-behind dynamically to submit and upload a file to the server. I am using AddHandler, but the button will not post back. I read everywhere that I need to regenerate this button after each post back because of the way webpages work. I'm still not able to get this button to work. I have a main page with the HTML:

<%@ Page Language="vb"
    AutoEventWireup="false"
    MasterPageFile="~/Site.Master"
    CodeBehind="Departments.aspx.vb"
    Inherits="Homepage.Departments" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript" src="~/Scripts/jquery-1.8.2.js"></script>
    <script src="~/Scripts/jquery-ui-1.8.24.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    </script>
    <div id="Top" runat="server"> </div>
    <div id="Left" runat="server"></div>
    <div id="Right" runat="server"></div>
</asp:Content>

The Top, Left, and Right divs are just placeholders for where I am dynamically generating the content of the page. The code-behind for the page just calls a function in a class I have developed and adds it to one of the divs on the page (I heard if I handled it in the page_init, it should create the page in the correct order...):

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

    *SNIP business logic to determine what information to pull*

    'NewWebPart is my class that creates the custom parts and returns them as an object

                For Each DeptLayoutRow As MyDataSet.DeptLayoutRow In DeptLayout
                    Dim NewPartObject As Object = NewWebPart.CreateNewPart

                    Select Case DeptLayoutRow.Area
                        Case 1
                            Top.Controls.Add(NewPartObject)
                        Case 2
                            Left.Controls.Add(NewPartObject)
                        Case 3
                            Right.Controls.Add(NewPartObject)
                    End Select

                    i += 1
                Next
            End If
        End If
    End If
End Sub

The function CreateNewWebPart is returning an object in the form of a table with a gridview, a button, and some JavaScript code inside. The important part of this class is the function that is creating the part I am having trouble with. This part contains a gridview that lists the items in a folder on the server. If the user wants, they click the upload button below the gridview and then the JavaScript creates an overlay that lets them click which file they want and upload it to the server. The submit button is the one that I am having trouble with:

Private Function CreateDocument() As Table
        Dim Documents As New GridView
        Documents.Width = width
        Documents.Height = height

        Dim table As New Table

        'Dim Files As String()
        Dim FileLocation As String = _
          ConfigurationManager.AppSettings.Item("DeptLoc").ToString + _
          "\" + DeptName + "\" + PartName

        If Directory.Exists(FileLocation) Then
            Dim DirInfo As New IO.DirectoryInfo(FileLocation)
            Dim FileArray As IO.FileInfo() = DirInfo.GetFiles()
            Dim FileInfo As IO.FileInfo

            Dim Dt As New DataTable
            Dt.Columns.Add("File Name", GetType(String))
            Dt.Columns.Add("File Link", GetType(String))
            Dt.Columns.Add("File Size", GetType(String))
            Dt.Columns.Add("Last Updated", GetType(Date))

            For Each FileInfo In FileArray
                Dt.Rows.Add(FileInfo.Name, _
                            FileInfo.FullName, _
                            Math.Round(FileInfo.Length / 1024).ToString, _
                            FileInfo.LastWriteTime)
            Next

            Documents.DataSource = Dt
            Documents.Attributes.Add("Class", "mGrid")
            Documents.ID = "Documents" + IDNum.ToString

            'This function creates and returns a table with Documents in the first cell
            table = CreatePartTable(Documents)

            'Create Upload file button'''''''''''''''''''''''''''''''''''''
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Dim btnUpload As New HtmlGenericControl
            btnUpload.InnerHtml = "<button id='btnUpload" + IDNum.ToString + "'>Upload File</button>"

            Dim UploadRow As New TableRow
            Dim UploadRowCell As New TableCell
            UploadRowCell.Controls.Add(btnUpload)
            UploadRow.Cells.Add(UploadRowCell)

            Dim UploadDiv As New HtmlGenericControl("DIV")

            UploadDiv.ID = "UploadDiv" + IDNum.ToString
            Dim lbl As New Label With {.Text = "Choose what files you would like to upload!"}
            Dim FileUpload As New HtmlInputFile With {.ID = "FileUpload" + IDNum.ToString}
            Dim btnSubmit As New Button With {.ID = "Submit" + IDNum.ToString, .Text = "Submit"}
            'Neither of these seem to work either....
            'btnSubmit.Attributes.Add("runat", "server")
            'btnSubmit.Attributes.Add("onclick", "btnUploadSubmit_OnClick")

            UploadDiv.Controls.Add(lbl)
            UploadDiv.Controls.Add(New LiteralControl("<br />"))
            UploadDiv.Controls.Add(New LiteralControl("<br />"))
            UploadDiv.Controls.Add(FileUpload)
            UploadDiv.Controls.Add(New LiteralControl("<br />"))
            UploadDiv.Controls.Add(New LiteralControl("<br />"))
            UploadDiv.Controls.Add(btnSubmit)
            AddHandler btnSubmit.Click, AddressOf btnUploadSubmit_OnClick

            Dim UploadDialogRow As New TableRow
            Dim UploadDialogRowCell As New TableCell
            UploadDialogRowCell.Controls.Add(UploadDiv)
            UploadDialogRow.Cells.Add(UploadDialogRowCell)

            Dim UploadDivJQuery As New UI.HtmlControls.HtmlGenericControl
            Dim JQueryString As New StringBuilder
            JQueryString.Append("<script type='text/javascript'>")
            JQueryString.Append("   $(function() {")

            JQueryString.Append("       $(""*[id$='UploadDiv" + IDNum.ToString + "']"").dialog({")
            JQueryString.Append("           autoOpen: false,")
            JQueryString.Append("           modal: true,")
            JQueryString.Append("           show: 'clip',")
            JQueryString.Append("           hide: 'clip'")
            JQueryString.Append("       }); ")
            JQueryString.Append("       $(""*[id$='btnUpload" + IDNum.ToString + "']"").click(function() {")
            JQueryString.Append("       $(""*[id$='UploadDiv" + IDNum.ToString + "']"").dialog( 'open' );")
            JQueryString.Append("            return false; ")
            JQueryString.Append("       }); ")
            JQueryString.Append("   });")
            JQueryString.Append("</script>")
            UploadDivJQuery.InnerHtml = JQueryString.ToString

            Dim UploadDivJQueryRow As New TableRow
            Dim UploadDivJQueryRowCell As New TableCell
            UploadDivJQueryRowCell.Controls.Add(UploadDivJQuery)
            UploadDivJQueryRow.Cells.Add(UploadDivJQueryRowCell)

            table.Rows.Add(UploadRow)
            table.Rows.Add(UploadDialogRow)
            table.Rows.Add(UploadDivJQueryRow)

            'To fix up the document's DataGrid the way we want it!
            'This addhanlder for the documents gridview works fine...
            AddHandler Documents.DataBound, AddressOf DocumentsDataGridHandler
            Documents.DataBind()

        End If
        Return Table
    End Function

Sorry if I posted too much, but I've been searching for days for an answer to this problem and can't seem to figure it out. I'm not sure if it is because I'm not loading it in the correct order, if it is because it is in another class, or of it is because I am using Master Pages or what.

来源:https://stackoverflow.com/questions/13751376/dynamic-button-not-working-with-addhandler-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!