472,803 Members | 979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,803 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 5382

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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.