1

我正在开发一个项目,在该项目中我试图在代码隐藏中动态创建一个按钮,以将文件提交并上传到服务器。我正在使用 AddHandler,但按钮不会回发。由于网页的工作方式,我到处都读到我需要在每次回帖后重新生成此按钮。我仍然无法让这个按钮工作。我有一个带有 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>

Top、Left 和 Right div 只是我动态生成页面内容的占位符。页面的代码隐藏只是调用我开发的类中的一个函数并将其添加到页面上的一个 div 中(我听说如果我在 page_init 中处理它,它应该以正确的顺序创建页面.. .):

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

函数 CreateNewWebPart 以表格的形式返回一个对象,其中包含一个网格视图、一个按钮和一些 JavaScript 代码。这个类的重要部分是创建我遇到问题的部分的函数。这部分包含一个网格视图,它列出了服务器上文件夹中的项目。如果用户需要,他们可以单击gridview 下方的上传按钮,然后JavaScript 会创建一个叠加层,让他们可以单击想要的文件并将其上传到服务器。提交按钮是我遇到问题的按钮:

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

抱歉,如果我张贴太多,但我一直在寻找这个问题的答案,但似乎无法弄清楚。我不确定是不是因为我没有按正确的顺序加载它,是因为它在另一个类中,还是因为我正在使用 Master Pages 或其他什么。

4

0 回答 0