Skip to content

SourceControl Problem (Dynamic SplitButton with ContextMenu) - Bug? #901

Closed

Description

@itg-assistant

consystems asked 2017-06-16 09:44:25 UTC

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);

flpAttatchments.Controls.Add(sbtn); //FlowLayoutPanel

}

}

}

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?

Activity

  1. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2017-06-16 16:09:23 UTC

    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

  2. added
    BugSomething is broken or behaving unexpectedly.
    on Jun 12, 2026
  3. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Frank (ITG) answered 2017-06-16 20:31:37 UTC

    Thanks.

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

    Best regards
    Frank

  4. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Felix CHAN answered 2017-08-02 06:35:46 UTC

    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

  5. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2017-08-02 22:37:42 UTC

    Thanks. Magic of copy/paste.

  6. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Frank (ITG) answered 2017-08-13 22:21:06 UTC

    Hi Felix,

    WJ-8298 is fixed in Wisej build 1.3.82.

    Best regards
    Frank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething is broken or behaving unexpectedly.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions