I'm trying to write code for a page using VB and as I'm new to this, I'm needing some assistance.

I have a button on a web page for uploading a .csv file but I also want that button to function as taking people to a destination page as well. How can I do that with the button controls?

Thank you

Doug

Dani AI

Generated

Both replies in the thread point in the right direction, but for a CSV upload plus navigation the most reliable pattern is: handle the uploaded file on the server in the button's server-side click handler, finish saving/validation, then send the user to the next page. That keeps full access to the uploaded stream and lets the server validate/rename the file before navigation. described the server-side approach, and 's PostBackUrl suggestion is useful for ordinary form posts — but it is not recommended as the primary mechanism for file uploads.

A minimal VB.NET pattern:

Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
    If FileUpload1.HasFile Then
        Dim ext As String = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower()
        If ext = ".csv" Then
            Dim saveName As String = Guid.NewGuid().ToString() & "_" & FileUpload1.FileName
            Dim savePath As String = Server.MapPath("~/uploads/" & saveName)
            FileUpload1.SaveAs(savePath)
            Session("UploadedFile") = saveName
            Response.Redirect("~/destination.aspx")
        Else
            ' invalid extension handling
        End If
    Else
        ' no-file handling
    End If
End Sub

Troubleshooting and hardening notes: ensure the page form has enctype="multipart/form-data", the uploads folder has write permission for the app pool, set maxRequestLength in web.config for larger files, validate file extension/size and sanitize filenames (use GUID prefix), and consider server-side content checks on CSVs. Avoid relying on cross-page postbacks (PostBackUrl) to carry the uploaded file stream to the destination page; instead save the file first and pass the saved filename via QueryString, Session, or other state.

This approach implements 's recommendation while clarifying the limitation of 's PostBackUrl for file uploads and gives practical checks to prevent common failures.

Recommended Answers

All 2 Replies

Hi Doug,

I think you may call uploading method in the on_click event and then take to the destination page when the uploading is finished.

Thanks,

I'm trying to write code for a page using VB and as I'm new to this, I'm needing some assistance.

I have a button on a web page for uploading a .csv file but I also want that button to function as taking people to a destination page as well. How can I do that with the button controls?

Thank you

Doug

Use the PostBackUrl property of the button to go to whatever page you wish after the postback. As the above poster said, code the uploading in the button event and then set this property and you should be good to go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.