SourceControl Problem (Dynamic SplitButton with ContextMenu) - Bug?

0
0

Hello

I got a problem with the “FileUpload” and dynamic “SplitButton” control which were added at runtime to a form.
Just to clarify what I’m trying to achieve. I got a form which is slightly similar to an email application. So I want to add attachments to those mails. I use the “FileUpload” to get those attachments and let them be shown in an “FlowLayoutPanel” as “SplitButton” with an “ContextMenu” that provides the options “print, save and remove”. As I said, those “SplitButton” where added to the Panel dynamically in the “UploadedEvent” of the “FileUpload”.

Dictionary<string, byte[]> _byteArrayList = new Dictionary<string, byte[]>();
private void fuAttachements_Uploaded(object sender, UploadedEventArgs e)
{

string x = string.Empty;
System.Web.HttpFileCollection d = e.Files;

for(int i = 0; i < e.Files.Count; i++)
{

System.Web.HttpPostedFile file = (System.Web.HttpPostedFile)e.Files[i];

byte[] fileData = null;
bool optischHinzufuegen = false;
using (var binaryReader = new System.IO.BinaryReader(e.Files[i].InputStream))
{

fileData = binaryReader.ReadBytes(e.Files[i].ContentLength);
if (!_byteArrayList.ContainsKey(e.Files[i].FileName))
{
_byteArrayList.Add(e.Files[i].FileName, fileData);
optischHinzufuegen = true;
}

}

if (optischHinzufuegen)
{

// Create the MenuItems for the ContextMenu
Wisej.Web.MenuItem miPrint = new MenuItem(“Drucken”, miPrint_Click);
miPrint.Name = “miPrint_” + file.FileName;
Wisej.Web.MenuItem miSave = new MenuItem(“Speichern”, miSave_Click);
miPrint.Name = “miSave” + file.FileName;
Wisej.Web.MenuItem miDelete = new MenuItem(“Entfernen”, miDelete_Click);
miPrint.Name = “miDelete” + file.FileName;

//Wisej.Web.ContextMenu conMenu = new ContextMenu();
//conMenu.MenuItems.Add(miPrint);
//conMenu.MenuItems.Add(miSave);
//conMenu.MenuItems.Add(miDelete);

Wisej.Web.SplitButton sbtn = new Wisej.Web.SplitButton {
Text = file.FileName,
Width = 100,
Height = 50 };

sbtn.Name = file.FileName;

sbtn.MenuItems.Add(miPrint);
sbtn.MenuItems.Add(miSave);
sbtn.MenuItems.Add(miDelete);

}

}

}

private void miDelete_Click(object sender, EventArgs e)
{

Wisej.Web.MenuItem mi = (Wisej.Web.MenuItem)sender;
Wisej.Web.ContextMenu m = (Wisej.Web.ContextMenu)mi.Parent;
Wisej.Web.SplitButton spbtn = (Wisej.Web.SplitButton)m.SourceControl;

if (spbtn != null)
{
_byteArrayList.Remove(spbtn.Text);
flpAttatchments.Controls.Remove(spbtn);
}

}

My problem now is, that in 95% of all cases that I got expected access to the “SplitButton” in the “miDelete_Click” event. But in some cases the “SourceControl” property returns “null”. I can’t find why this happens or what it causes that the “SourceControl” property isn’t set to the “SplitButton”.
Maybe the way how I create the “SplitButton” isn’t correct or shall be done in another way?

  • You must to post comments
0
0

Hi Felix,

WJ-8298 is fixed in Wisej build 1.3.82.

Best regards
Frank

  • You must to post comments
0
0

Hi,

Just find an minor issue, the documentations for the property UploadedEventArgs.Files is wrong.

It say “The error text.“, it should be something like “The collection of uploaded files.

Thanks and regards,
Felix CHAN

Attachment
  • Luca (ITG)
    Thanks. Magic of copy/paste.
  • You must to post comments
0
0

Thanks.

We have logged this problem as WJ-8298 and it will be fixed in the next Wisej build.

Best regards
Frank

  • You must to post comments
0
0

Your code is correct. The issue seems to be timing and our obsession with releasing references 🙂

The ContextMenu class has this:

case “disappear”:
OnCollapse(EventArgs.Empty);
this.SourceControl = null;
break;

So when the context menu “disappear” event fires before the menu item click event we get the issue you described. In wisej/qooxdoo “appearing” and “disappearing” of widgets is managed by an async fast dom manager and will occur out of sync to speed up browser rendering. We’ll log and fix.

As a workaround you can use the UserData property, on any single MenuItem after adding it:

miDelete.GetContextMenu().UserData.Button = sbtn;

Then in the click event you’ll find mi.GetContextMenu().UserData.button.

You can use Parent or GetContextMenu(). Parent works for first level menu items only.

Also, if the button can only perform one of the 3 actions,  you may use a Button instead of a SplitButton.

HTH

/Luca

 

 

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.