Hi,
I'm building a user control with two buttons and a FileUpload control
for oploading/downloading files.
I've created 3 properties for keeping information about a file
(content, name and mime type) as bindable properties.
I'm calling explicit DataBind() in the Page_Load event of the hosting
control of my user control.
When the button is clicked, first the Page_Load event of the hosting
page is fired, the binding is happening then the Page_Load event of
the user control is fired. Until now everithing is OK.
The problem is when event handler for the button click is executing.
It seems like the execution of the event is running in a new object
instance of the user control.
This means that all the bound properties are lost.
So the question is: Is there a way to forbid the creation of a new
object of the user control.

Here is the User control code:
public partial class UpDownAttachment : System.Web.UI.UserControl
    {
        public event EventHandler NewFileUploaded;

        [Bindable(true)]
        public byte[] Content
        {
            get;            set;
       }
        [Bindable(true)]
        public string MIMEType
        {
            get;            set;
        }
        [Bindable(true)]
        public string FileName
        {
            get;            set;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
        }


        protected void btnUploadAttachment_Click(object sender,
EventArgs e)
        {
            if(fuUploadAttachment.HasFile)
            {
                try
                {
                    Content = fuUploadAttachment.FileBytes;
                    MIMEType =
fuUploadAttachment.PostedFile.ContentType;
                    FileName = fuUploadAttachment.FileName;
                    StatusLabel.Text = "Upload status: File
uploaded!";
                    if (NewFileUploaded != null)
                        NewFileUploaded.Invoke(this, new EventArgs
());
                }
                catch(Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could
not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }


        protected void btnDownloadAttachment_Click(object sender,
EventArgs e)
        {
            if (Content!=null&&Content.Count() > 0)
            {
                Response.AppendHeader("Content-disposition",
"attachment; filename=" + FileName);
                Response.ContentType = MIMEType;
                Response.BinaryWrite(Content);
            }


        }
    }


And parts of the host page:


<%@ Register TagPrefix="uc1" TagName="download" Src="~/
CustomControls/
UpDownAttachment.ascx" %>
.....
<asp:FormView ID="EditFormView" runat="server" DefaultMode= "Insert"
DataSourceID="ProjectAttDataSource"
        oniteminserting="EditFormView_ItemInserting"
DataKeyNames="ID"
onitemupdating="EditFormView_ItemUpdating" BackColor="#284775"
ForeColor="WhiteSmoke" >
.......
<uc1:download ID="udaAttachment" runat="server" Content='<%# Eval
("Attachment") %>' MIMEType='<%# Eval("MIME_Type") %>' FileName='<%#
Eval("Title") %>' OnNewFileUploaded="udaAttachment_NewFileUploaded" /
>




Thanks in advance,
mitdej

Reply via email to