Asp.net 动态创建的按钮在回发时不调用函数

Asp.net 动态创建的按钮在回发时不调用函数,asp.net,vb.net,vba,postback,Asp.net,Vb.net,Vba,Postback,在我的VisualBasicWeb应用程序中,我有一个生成的按钮列表,这些按钮应该允许在单击时下载文件 我有一个在pageload上使用生成按钮的例子,但是突然下载功能在回发时停止了调用,现在所有的按钮点击(对于任何按钮)都会导致回发页面 我的代码: Public folder As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If

在我的VisualBasicWeb应用程序中,我有一个生成的按钮列表,这些按钮应该允许在单击时下载文件

我有一个在pageload上使用生成按钮的例子,但是突然下载功能在回发时停止了调用,现在所有的按钮点击(对于任何按钮)都会导致回发页面

我的代码:

Public folder As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        folder = "Main"
        PopulateFiles(folder)
    End If
End Sub

Protected Sub PopulateFiles(ByVal folder As String)
    Dim myConnection As SqlConnection
    Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Dim myCommand As SqlCommand
    Dim myDataReader As SqlDataReader
    Dim text As String
    Dim size As Decimal
    Dim name As String
    Dim type As String
    Dim id As Integer
    folderName.Text = folder
    container.Controls.Clear()
    myConnection = New SqlConnection(conString)
    myConnection.Open()
    myCommand = New SqlCommand("Uploads_GetAllFiles", myConnection)
    myCommand.CommandType = CommandType.StoredProcedure
    myCommand.Parameters.AddWithValue("@folder", folder)
    Try
        myDataReader = myCommand.ExecuteReader()
        If myDataReader.HasRows Then
            Do While myDataReader.Read()
                name = myDataReader.Item("Name")
                type = myDataReader.Item("Type")
                id = myDataReader.Item("File_ID")
                size = Math.Round(myDataReader.Item("Size") / 1000, 2)
                container.Controls.Add(New LiteralControl("<div class='newRow'>"))
                text = "<div class='fileName'>" & name & "</div>"
                container.Controls.Add(New LiteralControl(text))
                text = "<div class='fileType'>" & type & "</div>"
                container.Controls.Add(New LiteralControl(text))
                text = "<div class='fileSize'>" & size.ToString() & "kb</div>"
                container.Controls.Add(New LiteralControl(text))
                container.Controls.Add(New LiteralControl("<div class='fileDownload'>"))
                Dim newBtn As New Button
                newBtn.ID = "link" & id
                newBtn.Text = "Download"
                newBtn.CssClass = "newbie"
                AddHandler newBtn.Click, AddressOf Retreive_Doc
                newBtn.CommandArgument = id
                container.Controls.Add(newBtn)
                container.Controls.Add(New LiteralControl("</div>"))
                container.Controls.Add(New LiteralControl("<div class='fileDelete'>"))
                Dim newDelBtn As New Button
                newDelBtn.ID = "delete" & id
                newDelBtn.Text = "Delete"
                newDelBtn.CssClass = "delBtn"
                AddHandler newDelBtn.Click, AddressOf Retreive_Xls
                newDelBtn.CommandArgument = id
                container.Controls.Add(newDelBtn)
                container.Controls.Add(New LiteralControl("</div>"))
                container.Controls.Add(New LiteralControl("</div>"))
            Loop
        End If

    Catch ex As Exception
        MsgBox(ex.ToString())
    Finally
        myConnection.Close()
    End Try
End Sub

Protected Sub Retreive_Doc(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
    Dim button As Button = sender
    Dim id As Integer = button.CommandArgument
    Dim cmd As SqlCommand = New SqlCommand("Uploads_GetFile")
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.AddWithValue("@id", id)
    Dim dt As DataTable = GetData(cmd)
    If dt IsNot Nothing Then
        download(dt)
    End If
End Sub
公用文件夹作为字符串
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
如果不是的话,我会回来的
folder=“Main”
PopulateFile(文件夹)
如果结束
端接头
受保护的子PopulateFile(ByVal文件夹作为字符串)
Dim myConnection作为SqlConnection
Dim解析为String=ConfigurationManager.ConnectionString(“ConnectionString”).ConnectionString
将myCommand设置为SqlCommand
将myDataReader设置为SqlDataReader
将文本变暗为字符串
小尺寸为十进制
将名称设置为字符串
作为字符串的Dim类型
作为整数的Dim id
folderName.Text=文件夹
container.Controls.Clear()
myConnection=新的SqlConnection(构造)
myConnection.Open()
myCommand=New-SqlCommand(“上传\获取所有文件”,myConnection)
myCommand.CommandType=CommandType.StoredProcess
myCommand.Parameters.AddWithValue(“@folder”,文件夹)
尝试
myDataReader=myCommand.ExecuteReader()
如果myDataReader.HasRows,则
在myDataReader.Read()时执行此操作
name=myDataReader.Item(“名称”)
类型=myDataReader.Item(“类型”)
id=myDataReader.Item(“文件id”)
size=Math.Round(myDataReader.Item(“size”)/1000,2)
container.Controls.Add(新的LiteralControl(“”)
text=“&name&”
container.Controls.Add(新的LiteralControl(文本))
text=”“&type&“”
container.Controls.Add(新的LiteralControl(文本))
text=”“&size.ToString()和“kb”
container.Controls.Add(新的LiteralControl(文本))
container.Controls.Add(新的LiteralControl(“”)
将新按钮变暗为新按钮
newBtn.ID=“link”&ID
newBtn.Text=“下载”
newBtn.CssClass=“新手”
AddHandler newBtn.单击,检索文档的地址
newBtn.CommandArgument=id
container.Controls.Add(newBtn)
container.Controls.Add(新的LiteralControl(“”)
container.Controls.Add(新的LiteralControl(“”)
将newDelBtn变暗为新按钮
newDelBtn.ID=“删除”&ID
newDelBtn.Text=“删除”
newDelBtn.CssClass=“delBtn”
AddHandler newDelBtn.单击,检索地址
newDelBtn.CommandArgument=id
container.Controls.Add(newDelBtn)
container.Controls.Add(新的LiteralControl(“”)
container.Controls.Add(新的LiteralControl(“”)
环
如果结束
特例
MsgBox(例如ToString())
最后
myConnection.Close()
结束尝试
端接头
受保护的子Retrieve_文档(ByVal sender作为对象,ByVal e作为System.EventArgs)处理LinkButton1。单击
变暗按钮为按钮=发送器
Dim id为整数=button.CommandArgument
Dim cmd As SqlCommand=New SqlCommand(“上传”\u GetFile)
cmd.CommandType=CommandType.storedProcess
cmd.Parameters.AddWithValue(“@id”,id)
Dim dt As DataTable=GetData(cmd)
如果dt不是什么,那么
下载(dt)
如果结束
端接头
我可以显示从这个函数调用的函数,但是初始函数甚至没有被调用,所以我不确定有什么意义

我的HTML如下所示:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="upload1" runat="server" /><asp:Button ID="test1" runat="server" Text="Upload" />
<asp:TextBox ID="folderTag" runat="server" ></asp:TextBox>
<asp:Button ID="search" runat="server" Text="Search" />
<asp:Label ID="folderName" runat="server">General</asp:Label><br />
<div id="navContainer" runat="server">

</div>
    <div id="hiddenContent">      
    <asp:LinkButton ID="LinkButton1" CssClass="hide" runat="server" OnClick = "Retreive_Doc">Download Doc</asp:LinkButton>
    <asp:LinkButton ID="LinkButton2" CssClass="hide" runat="server" OnClick = "Retreive_Xls">Download xls</asp:LinkButton>
    </div>
<div id="container" runat="server">
</div>
</form>

一般性
下载文件 下载xls
正如我之前所说。这是工作前几天,通过添加一些其他功能的过程中不知何故我失去了它

我不确定它为什么不调用任何函数就发回


谢谢你的帮助

如果动态创建控件,则需要在回发中重新创建具有相同ID的控件。否则,它们将在回发后消失

在页面加载事件中删除
如果不是IsPostBack,则删除

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
  Handles Me.Load
   folder = "Main"
   PopulateFiles(folder)
End Sub
另一个想法

如果需要CommandParameter,是否有理由不使用CommandEvent而不是Click event

同样,是否有理由不使用
占位符而不是


ASP.NET
按钮
总是导致回发。您必须在回发上重新创建动态创建的控件。但是您有一个
如果不是回发,那么PopulateFiles()
。是的,但是我为其添加处理程序的函数在回发时没有被调用。这些控件最初位于updatepanel中,任何需要重新创建的控件都在另一个函数调用中。现在,只要回发时调用了正确的函数,我就可以丢失控件。如果没有控件,那么也不会得到事件。谢谢,这就是问题所在。不知道我是怎么搞砸的。要回答你的问题,没有理由,但这两者都有好处吗?@Malcoda我很高兴你能理解。没有真正的优势;然而,这些控件和事件都是为了达到这个目的,而且主要是易于阅读的。
AddHandler newBtn.Command, AddressOf Retreive_Doc
newBtn.CommandArgument = id

....

protected void Retreive_Doc(object sender, CommandEventArgs e)