User control event problem

M

mitdej

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.

Thanks in advance,
mitdej
 
P

Peter Duniho

[...]
So the question is: Is there a way to forbid the creation of a new
object of the user control.

There's nothing in C# that would implicitly create a new object, so you
must be doing that yourself somehow.

Of course, since you didn't post a concise-but-complete code example that
reliably demonstrates that problem, there's no way for anyone else to
suggest a specific fix. Until such time, I'll suggest a general-purpose
answer: don't create a new control instance if you don't want to.

Note, of course, that you may in fact have incorrectly diagnosed the
problem. Again, without an actual code example, there'd be no way for
anyone to know that though.

Pete
 
M

mitdej

[...]
So the question is: Is there a way to forbid the creation of a new
object of the user control.

There's nothing in C# that would implicitly create a new object, so you  
must be doing that yourself somehow.

Of course, since you didn't post a concise-but-complete code example that 
reliably demonstrates that problem, there's no way for anyone else to  
suggest a specific fix.  Until such time, I'll suggest a general-purpose  
answer: don't create a new control instance if you don't want to.

Note, of course, that you may in fact have incorrectly diagnosed the  
problem.  Again, without an actual code example, there'd be no way for  
anyone to know that though.

Pete

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" />
 
P

Peter Duniho

[...]
Of course, since you didn't post a concise-but-complete code example
that  
reliably demonstrates that problem, there's no way for anyone else to  
suggest a specific fix. [...]

Here is the User control code: [incomplete code snipped]

Here are some links you may find helpful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/ (some Java-centric stuff, but mostly applicable to any
programming questions)

That said, from what you did post, it's apparent that you're working on an
ASP.NET program. You really need to post your question in the ASP.NET
newsgroup, where people who know a lot more about ASP.NET are present and
who can help you. It's entirely possible that whatever you're seeing is
just "par for the course" in ASP.NET, even as it's impossible to happen
implicitly using a plain-vanilla C#/Forms application.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top