467,074 Members | 918 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

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
  • viewed: 5040
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by TK | last post: by
4 posts views Thread by Robin | last post: by
2 posts views Thread by Tim::.. | last post: by
7 posts views Thread by Mark Waser | last post: by
reply views Thread by Tumurbaatar S. | last post: by
reply views Thread by bminder@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.