Problems with variables

Aug 24, 2009 at 12:16 AM

I'm trying to send some variables to the server with my files and things aren't working as expected.

My code is:

<form id="form1" runat="server">
    <div>
        Camera:
        <asp:TextBox ID="CameraName" runat="server"></asp:TextBox>
        <br />
    <fjx:FileUploader ID="FileUploader1" runat="server" RequestAsPostBack="true" >
    <Adapters>
    <fjx:FileSaverAdapter FolderName="fullsize" />
    <fjx:ThumbGeneratorAdapter FolderName="thumbnails" Suffix="_thumb" MaximumHeight="250" MaximumWidth="250"/>
    <cust:cDatabaseInserter />
    </Adapters>
    </fjx:FileUploader>
    <%FileUploader1.State.Add("FullSizeFolder", "fullsize"); %>
    <%FileUploader1.State.Add("CameraName", CameraName.Text); %>
    <%FileUploader1.State.Add("ThumbsFolder", "thumbnails"); %>
    <%FileUploader1.State.Add("TEST", CameraName.Text); %>
    </div>
    </form>
Of these State variables, only the CameraName variable is being sent to the server. Can anyone explain why?

Aug 24, 2009 at 12:21 AM

Oh yeah... I'm determining that variables aren't being passed in the ProcessFile() method of the cust:cDatabaseInserter adapter in the above code. My adapter has the following code:

foreach (string key in HttpContext.Current.Request.Form.AllKeys)
                {
                    System.Diagnostics.Debug.WriteLine("key: " + key + " value: " + HttpContext.Current.Request.Form[key]);
                }

and it doesn't show the FullSizeFolder, the ThumbsFolder, or the TEST variables at all. The output from the above loop is:

key: Filename value: myfile.jpg
key: __VIEWSTATE value: /wEPDwUKMTc1MDUzOTA2NWRkDCdJUbHpy4OajI4kxl+/orXj1jQ=
key: CameraName value: tf
key: __EVENTVALIDATION value: /wEWAgL+v7jPBwKe9suNDnOwMrfTDT/6Ftyt6hPVyMIho8mn
key: FileUploader1 value: null
key: Upload value: Submit Query

 

Coordinator
Aug 24, 2009 at 6:33 AM
Edited Aug 24, 2009 at 6:34 AM

If you have RequestAsPostBack="true" then you can just use hidden fields.

Otherwise you should not throw code in between markup. This is bad ASP practice. In .NET we don't do that. Insetead do:

 <script runat="server">
protected override void OnInit(EventArgs e){
    base.OnInit(e);

    FileUploader1.State.Add("FullSizeFolder", "fullsize");
    FileUploader1.State.Add("CameraName", CameraName.Text);
    FileUploader1.State.Add("ThumbsFolder", "thumbnails");
    FileUploader1.State.Add("TEST", CameraName.Text);
}
</script>
<form id="form1" runat="server">
    <div>
        Camera:
        <asp:TextBox ID="CameraName" runat="server"></asp:TextBox>
        <br />
    <fjx:FileUploader ID="FileUploader1" runat="server" RequestAsPostBack="true" >
    <Adapters>
    <fjx:FileSaverAdapter FolderName="fullsize" />
    <fjx:ThumbGeneratorAdapter FolderName="thumbnails" Suffix="_thumb" MaximumHeight="250" MaximumWidth="250"/>
    <cust:cDatabaseInserter />
    </Adapters>
    </fjx:FileUploader>

    </div>
    </form>

 

Aug 24, 2009 at 12:38 PM

Thanks, Vlad. I'm fairly proficient at C# but ASP.NET is new to me. I've moved my code to the OnInit method as you suggested.

Even after moving my code to the OnInit method, however, I only get the CameraName variable. Why is this? If I dump the dictionary in the OnInit method, it's correct. If I dump the dictionary in my adapter, I'm missing entries. (Even if my adapter is the first adapter in the chain.)  I'd really like to understand this, even if using hidden fields is the ultimate solution I go with.

Coordinator
Aug 24, 2009 at 3:21 PM

Actually I meant OnLoad not OnInit because OnInit is used to set the state. So this works fine (I just tested it):

<%@ Page Language="C#" AutoEventWireup="false" Trace="true" %>
<%@ Import Namespace="com.flajaxian"%>
<%@ Register TagPrefix="fjx" Namespace="com.flajaxian" Assembly="com.flajaxian.FileUploader"%>
<script type="text/C#" runat="server">
protected override void OnLoad(EventArgs e){
    base.OnLoad(e);

    FileUploader1.State.Add("FullSizeFolder", "fullsize");
    FileUploader1.State.Add("CameraName", CameraName.Text);
    FileUploader1.State.Add("ThumbsFolder", "thumbnails");
    FileUploader1.State.Add("TEST", CameraName.Text);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>test</title>
</head>
<body>
    <form id="form1" runat="server">
    
    <div>
        Camera:
        <asp:TextBox ID="CameraName" runat="server"></asp:TextBox>
        <br />
        <fjx:FileUploader ID="FileUploader1" runat="server" RequestAsPostBack="true" >
          <Adapters>
            <fjx:FileSaverAdapter FolderName="UploadFolder" />
            <fjx:ThumbGeneratorAdapter FolderName="UploadFolder" Suffix="_thumb" MaximumHeight="250" MaximumWidth="250"/>
          </Adapters>
        </fjx:FileUploader>

    </div>




    </form>
</body>
</html>
Aug 24, 2009 at 3:32 PM

It's very interesting that this works for you. It is most definitely not working for me. Hidden fields has allowed me to progress on this project, though, and since it's a skunkworks project I'll just slog on with what I've got.

 

Thanks for taking the time to look at this with me, Vlad!