State is always empty, or how to store values for the uploader

Oct 23, 2008 at 3:20 PM
Hello again,

I have the following setup: Inside of a TabPanel i have a DropDown Menu for selecting persons. This dropwdown is inside of an updatepanel, in order to avoid a page reload every time i select something, which would cause the hosting tabcontainer to jump to the first page. Outside of the updatepanel resides my user control which contains the file uploader accompanied by a custom saver- adapter.

I must provide my adapter the value that is set in the dropdown. For doing this I tried to add the value to the uploader's state in code-behind. I have to make the value available to my custom adapter finally.

My problem now is that every time i upload something this state collection is empty, my values gone. I tried to workaround this by storing it in viewstate, which turned out to be empty also after clicking upload. my next guess would be to store it in the session, which basically works by setting the stored value in the adapter's property on each page_load of my control, but I have to initialize the my control with the fist element in the dropdown. I do this in the page_load of the page that hosts the tabpanel, but this is always called with Postback and !allback set to false when i hit upload, so each time i hit upload my selection stored in the session is overwritten with the defaut value...

So I'm completely stuck here. Since i need the value from the dropdown in my custom Adapter i cannot directly access it from session or viewstate. I'd like to have it in the Hashtable that is a parameter of the Adapter's ProcessFile Method, but I can't find a solution for this.
Coordinator
Oct 23, 2008 at 4:16 PM
So the call is not postback but there is a special way to pass state to the server:

FileUploader1.State.Add("MyKey", "MyValue");

then when th file is transfered you can get it as

if(FileUploader1.FileIsPosted){
    string stateInfo = this.Request.QueryString["MyKey"];
}

or here is a full example

<%@ Page Language="C#" AutoEventWireup="false" %>
<%@ Register TagPrefix="fjx" Namespace="com.flajaxian" Assembly="com.flajaxian.FileUploader" %>
<%@ Import Namespace="System.Collections.Generic" %>
<SCRIPT language="C#" runat="server">
override protected void OnInit(EventArgs e) {
     this.Load += new System.EventHandler(this.Page_Load);
}
protected void Page_Load(Object sender, EventArgs e){
   if(!this.IsPostBack){
      ddl1.DataSource = new KeyValuePair<string, string>[]{
           new KeyValuePair<string, string>("UploadFolder1", "UploadFolder1"),
           new KeyValuePair<string, string>("UploadFolder2", "UploadFolder2"),
           new KeyValuePair<string, string>("UploadFolder3", "UploadFolder3")
      };
      ddl1.DataBind();
     }

     if(FileUploader1.FileIsPosted){
         ((com.flajaxian.FileSaverAdapter)FileUploader1.Adapters[0]).FolderName = this.Request["SelectedFolder"];
     }else{
         FileUploader1.State.Add("SelectedFolder", ddl1.SelectedValue);
      }
}
</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>
<asp:DropDownList ID="ddl1" runat="server" DataValueField="Key" DataTextField="Value" AutoPostBack="true" />

<div> 
Oct 24, 2008 at 7:20 AM
This is what I tried already.

In the OnItemChanged of my dropdown I set a value like FileUploader1.State.Add("MyKey", "MyValue"). The State collection contains 1 Item after that. Fine so far.
When I hit "Upload" the State Collection is empty again, and in the ProcessFile Method of my custom Upload Adapter the Hashtable also contains no items.
Oct 24, 2008 at 3:30 PM
Ok, I was not able to solve it with the State of the uploade.r My workaround was to put the upload control in its own page and display it as a Popup. Together with "Sesion" I was able to do what I wanted. But it is still very strange that both Viewstate and uploader state did not survive the click on the "upload" button...
Coordinator
Oct 27, 2008 at 4:36 AM
If your state is determened without postback you have to pass it on the client side with JavaScript.

Use:

<%=FileUploader1.ClientID%>.setStateVariable("someVar", "state");

Call this when your state changes