我正在开发一个项目,我试图动态地在code-behind中动态创建一个按钮,以便将文件提交并上传到服务器。我正在使用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函数以表格形式返回一个对象,其中包含gridview,一个按钮和一些JavaScript代码。这个类的重要部分是创建我遇到问题的部分的功能。此部分包含一个gridview,列出服务器上文件夹中的项目。如果用户想要,他们会点击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或者是什么。