Skip to content

Invoke Upload control from a button? #186

Closed

Description

@itg-assistant

Alex Prinias asked 2016-07-26 16:51:32 UTC

Hi,

Is there the possibility to invoke the upload functionality by clicking a button (not the upload control button obviously). What I mean is click a button and do something like UploadControl.PerformClick(), so that the file selection dialog shows up to choose which file is to be uploaded.

Best,
Alex

Activity

  1. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2016-07-26 19:20:42 UTC

    Hi Alex,

    It is possible from the client side only. Browsers block any attempt at initiating the upload action when executing javascript that was not initiated by a direct user action, ie. a click.

    If you drop the JavaScript extender you can add this script to the button that you want to use:

    this.addListener("execute", function(e){this.upload1.upload();});
    

    Remember that if upload1 is not visible and you are using the experimental build I sent you, it will not be sent to the client. You have explicitly call this.upload1.CreateControl();

    HTH

    Best,

    Luca

  2. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Alex Prinias answered 2016-07-26 19:40:10 UTC

    Thanks, Luca, I'll give it a try.

    Alex

  3. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Alex Prinias answered 2016-07-26 21:04:03 UTC

    Hi Luca,

    I can't make it work, and I suppose it has to to with "this". If I set the javascript to the button, then then "this", I suppose, refers to the button. But the button does not have an upload control, the upload control belongs to the parent panel which contains both the button and the upload control. I tried also to set the javascript to the parent panel (usercontrol) like

    this.button.addListener("execute"/"click", function(e) { this.uploadControl.upload(); }

    Also tried

    function(e) { this.uploadControl.fireEvent("Uploaded"); } (inspired from your example about the default button).

    But no result... The upload control is visible and I am using your experimental build...

    Alex

  4. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2016-07-26 22:17:58 UTC

    Yes you are right. I tried it in the chrome console and I assume it worked, but "this" refers to the button as you said.

    Try the script below attaching to the button that you want to click, otherwise I will set up a test tomorrow:

    this.addListener("execute", function(e){
    
       this.getParent().upload1.upload();
    
    });
    

    Assuming that upload1 has the same parent as this. Otherwise you need to tell the button wich upload control to
    use, which is also possible.

  5. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Alex Prinias answered 2016-07-26 22:29:44 UTC

    Yes, it works! Great! thanks.

    Alex

  6. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2016-07-27 16:39:10 UTC

    I have a better solution for you without using the JavaScript component.

    this.upload1.CreateControl();
     this.button.Eval(
            String.Format(@"
                this.uploadButton = Wisej.Core.getComponent(""id_{0}"");
                this.addListener(""execute"", function(e)
                  {{
                     this.uploadButton.upload();
                  }});", this.upload1.Handle));
    

    You can also put this in a Button class and add a property that lets you connect it to any upload control. This script saves the reference to the upload widget in line "this.uploadButton = ..." and calls upload() when clicked. This way you can connect the two dynamically or in a button class.

  7. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Alex Prinias answered 2016-07-28 16:15:43 UTC

    Yes, this version in the C# code works too!

    This means that a server control's .Handle property reflects at runtime the widget's id_xxx ? Nice...

    How "unique" are these id_'s ? I mean, if my button and the Upload control are encapsulated within a usercontrol, then the button has id_1 and the upload control has id_2, are these id_'s unique in the usercontrol or also in the parent component of the usercontrol and finally in the top level Form?

    Best,
    Alex

  8. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2016-07-28 16:27:02 UTC

    They are unique throughout the session and assigned in order of creation starting from 1 up to 2,147,483,647 (0x7FFFFFFF or int.MaxValue).

    Once the counter reaches max value it restarts from 1 but it also starts checking if the id is in use and skips the used ones. So it's always guaranteed to be unique and fast. The chances of an app to create and destroy 2 billion controls are slim and even it does it's still unique.

    You can also get the actual id string like this ((IWisejComponent)control).Id - it's the same as "id_" + control.Handle.

    However, if the control is not created yet (control.Created == false) the Handle is 0 and the id string is null. The only way to force the creation of the control is to call control.CreateControl(), but it will create all children and all parents.

    Creating a control simply means that the control is fully registered and sent to the client to be turned into a widget. In the new build (the one that Alex is using as an experimental build) this will happen on when the control is made visible the first time.

    The new update is coming up with a very substantial speed increase in the UI code (server and client).

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

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions