views:

17

answers:

2

I have a ListView that has a FileUpload control and a button in each ListViewItem.
I have an OnClick event on my button where i try and pull information from the FileUpload control, but when I try to access the control all of the values that were set are gone (FileName etc).

What do I need to do differently here to access the information I just entered?

            <asp:ListView ID="lv_Uploads" runat="server" OnItemDataBound="GetThumbs" EnableViewState="true" >
                <LayoutTemplate>
                    <div id="itemPlaceholder" runat="server" />
                 </LayoutTemplate>
                <ItemTemplate>
                    <div style="width:500px;>
                        <asp:FileUpload runat="server" ID="fu_Upload" />
                        <asp:Button ID="btn_Save" runat="server" Text="Save File"  OnClick="SaveFile" />
                        <br />
                    </div>
                </ItemTemplate>
            </asp:ListView>

Code behind:

        protected void SaveFile(object sender, EventArgs e)
        {
            //This always evaluates to an empty string...
            string myFile = ((FileUpload)((Button)sender).Parent.FindControl("fu_Upload")).FileName;
        }
A: 

Is the ListView control being rebound before SaveFile is fired on PostBack? If so, it would wipe out any values the user entered.

Rex M
No, I only DataBind on `PageLoad` when `!Page.IsPostBack`
Abe Miessler
+1  A: 

I tested the code you provided for the aspx and the following as the code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        lv_Uploads.DataSource = data;
        lv_Uploads.DataBind();
    }

}
protected void SaveFile(object sender, EventArgs e)
{
    //This always evaluates to an empty string...
    string myFile = ((FileUpload)((Button)sender).Parent.FindControl("fu_Upload")).FileName;
}

protected void GetThumbs(object sender, ListViewItemEventArgs e)
{

}

protected IEnumerable<string> data = new string[] { "test1", "test2", "test3" };

The FileUpload control had data for me on PostBack.

Are you using an UpdatePanel around the ListView? FileUpload controls are not compatible with UpdatePanels.

See:

http://stackoverflow.com/questions/35743/fileupload-control-inside-an-updatepanel-without-refreshing-the-whole-page

and

http://msdn.microsoft.com/en-us/library/bb386454.aspx#UpdatePanelCompatibleControls

sgriffinusa
Gah!!!! I am using an update Panel. This is probably the 20th time the update panel has got me. Thanks for your help!
Abe Miessler
Its gotten me more than once too, that's why I suspected it might be the issue here. ;)
sgriffinusa