0

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

我在页面加载时使用生成的按钮的示例,但突然间下载功能停止在回发时被调用,现在所有按钮点击(对于任何按钮)都会导致页面回发。

我的代码:

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

我可以显示从此函数调用的函数,但初始函数甚至没有被调用,所以我不确定是否有道理。

我的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>

正如我之前所说。几天前这是可行的,并且在添加其他一些功能的过程中不知何故我失去了它。

我不确定为什么它在不调用任何函数的情况下发回。

谢谢你的帮助!

4

1 回答 1

1

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

If Not IsPostBack Then在页面加载事件中删除。

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

另一个想法

如果您想要 CommandArgument,是否有理由不使用 CommandEvent 而不是 Click 事件?

同样,有理由不使用PlaceHolder而不是<div id="container" runat="server">吗?

AddHandler newBtn.Command, AddressOf Retreive_Doc
newBtn.CommandArgument = id

....

protected void Retreive_Doc(object sender, CommandEventArgs e)
于 2013-04-24T14:43:45.427 回答