开发者

Information disappears on button click

开发者 https://www.devze.com 2023-01-07 04:54 出处:网络
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

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 eva开发者_运维知识库luates to an empty string...
            string myFile = ((FileUpload)((Button)sender).Parent.FindControl("fu_Upload")).FileName;
        }


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:

FileUpload control inside an UpdatePanel without refreshing the whole page?

and

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号