KBatz KBatz - 3 years ago 103
Vb.net Question

Response.Redirect(Request.Url.AbsoluteURI) Clearing all user entries

I have a page where a user fills in some text boxes which get saved to a SQL database using a Submit button. The page also contains a button that allows them to upload attachments. If the user uploads an attachment BEFORE clicking the submit button to save the other data, the values in the text boxes are cleared once the upload routine executes the Response.Redirect(Request.Url.AbsoluteUri). I have tried saving the values I want to restore into the Session, but I don't seem to be able to restore them. The debugger shows they are there, but once the Response.Redirect is executed, the next lines are never executed. I'm brand new to ASP.NET, so I may just be missing something obvious. Here is the code for the upload procedure:

Protected Sub Upload(sender As Object, e As EventArgs) Handles btnUpload.Click
Session("Phone") = txtPhone.Text
Session("Name") = txtName.Text
Session("Email") = txtEmail.Text
Session("StartDate") = txtStartDate.Text
Session("EndDate") = txtEndDate.Text
Session("Subject") = txtSubject.Text
Session("Description") = txtDescription.Value
Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim contentType As String = FileUpload1.PostedFile.ContentType
Using fs As Stream = FileUpload1.PostedFile.InputStream
Using br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
Dim constr As String = ConfigurationManager.ConnectionStrings("EngineeringRequestsConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Dim query As String = "insert into Attachments values (@id, @Name, @ContentType, @Data)"
Using cmd As New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.Add("@id", SqlDbType.Int).Value = nextId
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("@ContentType", SqlDbType.NVarChar).Value = contentType
cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value = bytes
End Using
End Using
End Using
End Using
hasUpload = True

End Sub

The BindGrid() procedure attempts to restore the values from Session but never gets executed.

If hasUpload Then
txtPhone.Text = CType(Session("Phone"), String)
txtName.Text = CType(Session("Name"), String)
txtStartDate.Text = CType(Session("StartDate"), String)
End If

This is my first post on SO. I apologize if in advance if it is not clear enough.

Answer Source

If you are new to ASP.NET webforms it's probably worth checking out the Page Lifecycle as this dictates the order in which events are fired when a page is loaded. The issue is that you are effectively taking the user from page A to page B but expecting them to see results on page A.

In your method

Protected Sub Upload(sender As Object, e As EventArgs) Handles btnUpload.Click
  .. skip ..

When you call Response.Redirect() the browser will redirect to a new page (e.g. from A -> B), this will start the page lifecycle over again, anything that happens after Response.Redirect() won't be rendered. I think what is confusing you is that you are redirecting from (A -> A), however this will still cause the page to be reloaded.

One option is to call BindGrid() and reload the data from session in one of the page load events, or remove the call to Response.Redirect() all together and instead leave the page as-is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download