473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Contact form with file upload loses viewstate

Hallo NG,
ive searched a lot in some google groups, and found many threads, but
nothing that helped me.
Here is the scenario:
I have an aspx page which loads a user control in page.onInit like this

Dim pdControl As bb.unit = CType(LoadControl("./content/unit.ascx"),
bb.unit)
phContent.Controls.Add(pdControl)

One of these controls, is a contact form. First, this form shows a
selection, where you can choose what kind of contact you want to submit.
This doesnt use a postback, this works with get requests.
After this, the actual form comes up. There you can insert some values, and
decide if you wish to upload a file. If the user selects yes, another form
comes up, with an input type=file. There the user selects a file and posts
the page back. After this postback, the values he entered in the form before
the upload, are empty.

Ive tried to make my code as short as possible, but i havn't any clue what
causes this. So heres some code:

jobs.ascx:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="jobs.ascx.vb"
Inherits="loopline_produktiv.jobs"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<!-- These fields save the status of my javascript image checkboxes -->
<input type="hidden" value="off" ID="status" Runat="server" />
<input type="hidden" value="off" ID="status2" Runat="server" />
<% If Request.Params.Get("act") = Nothing Then %>
<!-- Here the user select a value for "act", just html here -->
<% Else %>
<table width="635" cellspacing="0" cellpadding="0">
<tr>
<td class="width3 white" align="center">
<!-- lblAll is the first form -->
<asp:Panel ID="lblAll" Runat="server">
<table class="fontsmall" width="87%" cellspacing="0"
cellpadding="5">
<tr>
<td align="right">Vorname&nbsp;<asp:Label ID="errVorname"
Runat="server" ForeColor="red" Font-Bold="True" /></td>
<td><asp:TextBox ID="txtVorname" Runat="server" Width="185"
/></td>
<td></td>
</tr>
<tr>
<td align="right">Name&nbsp;<asp:Label ID="errName"
Runat="server" ForeColor="red" Font-Bold="True" /></td>
<td><asp:TextBox ID="txtName" Runat="server" Width="185" /></td>
<td></td>
</tr>
<tr>
<td valign="top" align="right"></td>
<td colspan="2">
<!-- Here are my javascript image checkboxes -->
<a href="javascript:void(0)" onclick="checkbox()"><img border="0"
src="../img/off.gif" name="bild1" alt=""></a>Ja, PDF hochladen
<a href="javascript:void(0)" onclick="checkbox2()"><img border="0"
src="../img/off.gif" name="bild12" alt=""></a>Kopie der Bewerbung an die
oben angegebene Email schicken
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="1">
<tr>
<td align="right" valign="top">
<input class="borderless" type="reset" value=" Löschen
">&nbsp;
<asp:Button CssClass="borderless" ID="btnSubmit"
Runat="server" Text="Anfrage abschicken" OnClick="Next_Click" />
<asp:ImageButton CssClass="borderless" ID="ibtnArrow"
Runat="server" BorderWidth="0px" ImageUrl="../img/arrow_button.gif"
OnClick="iNext_Click" />
</td>
</tr>
</table>
</asp:Panel>
<!-- Upload form -->
<asp:Panel Visible="False" ID="lblUpload" Runat="server">
<table cellspacing="0" cellpadding="0" class="width3" width="87%"
align="center">
<tr>
<td align="center">
<input type="file" ID="txtPfad" Runat="server" /><br><br>
<asp:Button CssClass="borderless" ID="btnUpload"
Runat="server" Text="Abschicken" OnClick="Next_Click" />
</td>
</tr>
</table>
</asp:Panel>
<!-- Last panel, just shows "Thank you"-->
<asp:Panel Visible="False" ID="lblEnde" Runat="server"></asp:Panel>
</td>
</tr>
</table>
<% End if %>

jobs.ascx.vb:
Imports System.Data.SqlClient
Imports System
Imports System.Web
Imports System.Web.Mail
Imports System.IO

Public MustInherit Class jobs
Inherits System.Web.UI.UserControl

Dim SqlConnec As New SqlConnection("Max Pool
Size=150;Password=*******;Persist Security Info=True;User ID=sa;Initial
Catalog=database;Data Source=xxx.xxx.xxx.xxx")

Protected WithEvents txtVorname As System.Web.UI.WebControls.TextBox
Protected WithEvents txtName As System.Web.UI.WebControls.TextBox
Protected WithEvents lblAll As System.Web.UI.WebControls.Panel
Protected WithEvents lblUpload As System.Web.UI.WebControls.Panel
Protected WithEvents lblAbschluss As System.Web.UI.WebControls.Label
Protected WithEvents lblEnde As System.Web.UI.WebControls.Panel
Protected WithEvents lblTest As System.Web.UI.WebControls.Label
Protected WithEvents txtPfad As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents status As
System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents status2 As
System.Web.UI.HtmlControls.HtmlInputHidden

Protected WithEvents errVorname As Label
Protected WithEvents errName As Label
Protected WithEvents errLabel As Label
Protected WithEvents lblError As Label

Dim strResumee As String
Dim strFile As String = ""
Dim savePath As String
Dim filename As String

#Region " Vom Web Form Designer generierter Code "

'Dieser Aufruf ist für den Web Form-Designer erforderlich.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: Diese Methode ist für den Web Form-Designer erforderlich
'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'script blocks for my javascript image checkboxes
Page.RegisterClientScriptBlock("checkbox", "<script>function
checkbox(){if (document.main." & status.ClientID &
".value==""off""){document.main." & status.ClientID &
".value=""on"";document.bild1.src=an.src;}else{doc ument.main." &
status.ClientID & ".value=""off"";document.bild1.src=aus.src;}}</script>")
Page.RegisterClientScriptBlock("checkbox2", "<script>function
checkbox2(){if (document.main." & status2.ClientID &
".value==""off""){document.main." & status2.ClientID &
".value=""on"";document.bild12.src=an.src;}else{do cument.main." &
status2.ClientID & ".value=""off"";document.bild12.src=aus.src;}} </script>")
End Sub

'user clicks next after the first form
Sub Next_Click(ByVal sender As Object, ByVal e As EventArgs)
MoveNext(sender.Parent.ID)
End Sub

Function sendStuff()
Dim strSummary As String
'here strsummary gets filled with the textbox values etc
strSummary =
System.Text.Encoding.GetEncoding("iso-8859-1").GetString(System.Text.Encodin
g.GetEncoding("iso-8859-1").GetBytes(strSummary))
SmtpMail.SmtpServer = "smtp.test.de"

If status2.Value = "On" Then
'if its on, a copy of the email gets send to the user
End If

'email gets send to me
Dim Mailmsg As New System.Web.Mail.MailMessage()
Dim strError As String
Mailmsg.To = "bb******@test.de"
Mailmsg.From = "\" & "loopline interactive media" & "\ <" &
"bb******@test.de" & ">"
Mailmsg.BodyFormat = MailFormat.Text
Mailmsg.Subject = "subject"
'uploaded file gets attached
If Not strFile = "" Then
Dim objAttach As New MailAttachment(savePath & strFile)
Mailmsg.Attachments.Add(objAttach)
End If
Mailmsg.Body = strSummary
Try
SmtpMail.Send(Mailmsg)
Catch
End Try

'a copy of the email text gets saved in our db
Dim SqlInsert As New SqlCommand()
SqlInsert.CommandText = "INSERT INTO Kontakt (Text) Values ('" &
strSummary & "')"
SqlInsert.Connection = SqlConnec
If Not SqlConnec.State = ConnectionState.Open Then
SqlConnec.Open()
End If
SqlInsert.ExecuteScalar()
SqlConnec.Close()
End Function

Function validate() As Boolean
Dim bolReturn As Boolean = False

If txtVorname.Text.Length < 1 Then
errVorname.Text = "*"
bolReturn = True
Else
errVorname.Text = ""
bolReturn = False
End If

If txtName.Text.Length < 1 Then
errName.Text = "*"
bolReturn = True
Else
errName.Text = ""
bolReturn = False
End If

Return bolReturn
End Function

Function MoveNext(ByVal sende As String)
If validate() Then
errLabel.Text = "Ein oder mehrere Felder wurden nicht oder nicht
richtig ausgefüllt."
status.Value = "off"
status2.Value = "off"
Else
errLabel.Text = ""
Select Case sende
Case "lblAll"
lblAll.Visible = False
If status.Value = "on" Then
lblUpload.Visible = True
lblEnde.Visible = False
Else
lblUpload.Visible = False
lblEnde.Visible = True
sendStuff()
End If
Case "lblUpload"
lblError.Text = ""
If Not txtPfad.PostedFile Is Nothing Then
If txtPfad.PostedFile.ContentType =
"application/pdf" Then
If txtPfad.PostedFile.ContentLength < 1048576
Then
Try
Dim postedFile = txtPfad.PostedFile
strFile = txtName.Text & "-" & Now.Today
& ".pdf"
filename = Path.GetFileName(strFile)

savePath = Server.MapPath("./upload/")
postedFile.SaveAs(savePath & filename)
Catch exc As Exception
lblError.Text = "Beim Upload der Datei
ist ein Fehler aufgetreten. Fehler: " & exc.Message.ToString
End Try
If lblError.Text = "" Then
lblAll.Visible = False
lblUpload.Visible = False
lblEnde.Visible = True
sendStuff()
End If
Else
lblError.Text = "Die Datei überschreitet
1024kb."
End If
Else
lblError.Text = "Die Datei ist kein
PDF-Dokument."
End If
Else
lblError.Text = "Es wurde keine Datei angegeben."
End If
End Select
End If
End Function

End Class

Any help would be greatly appreciated
Thanks in advance
MfG Benjamin Bittner
Nov 19 '05 #1
0 5470

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean Dotson | last post by:
I have a form that passes variables to an asp file and then uploads a file. For some reason the request.form is not getting the info from the form. It's returning blanks. Any insight would be...
1
by: TK | last post by:
I have a file upload control to allow a user to select/upload a file to the server. They need to upload x number of files in one shot because they have to confirm that they are uploading x number...
4
by: Robin | last post by:
On an ASP.Net page that has viewstate enabled. How do you enable viewstate for a server side file input control? Currently I have in the HTML view. <INPUT id="myID" type="file" name="myID"...
2
by: Tim::.. | last post by:
Can someone please tell me why I keep getting an error saying the page cannot be displayed in my ASP.NET app. If I upload a file that is under approx 3mb then it works file but as soon as I try to...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
0
by: Tumurbaatar S. | last post by:
My web user control (ASCX) consists of one DataGrid and several web user controls (ASCX). On page postback, the DataGrid loses its content: row/item information, i.e. cells text. Other user...
0
by: bminder | last post by:
I have a web user control that contains a serializable class. I persist this class in viewstate, but on postback, the viewstate is null. Is this behavior by design? This technique works fine on...
4
by: DMG | last post by:
I have <identity impersonate = "true" /& <authentication mode="Windows"/in the web.config. This is a 2.0 site. My Goal: I want to simply have the user browse to a file on a mapped drive and get...
4
by: vunet | last post by:
Hello, My HTML form submits some values to a hidden iframe. However, this is done for file upload fields only. After file uploading is finished I am using this form to submit all other data...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.