0

I am attempting to submit form data from within a javascript popup window. The popup opens fine and displays all elements as expected. When I click the asp:button, before inputting anything, it performs required field validation as expected. All of that is working just fine. The problem I am having is in getting the OnClick to work and submit the form data. To say I am a beginner to javascript/JQuery is an understatement, so requesting a bit of hand-holding on this one. Thanks in advance for the help.

<asp:Button ID="submitRes" CssClass="button" runat="server" ClientIDMode="Static" Text="Submit This Form Data" OnClick="Submit_Click" />

$( document ).ready(function() {

    $(document).on("click", ".divPopupAdd", function(event){
        showPopup()
    });

    function showPopup(){
        $("#popup_add").dialog({
            show: { effect: "blind", duration: 200 },
            resizable: false,
            modal: true,
            width: 750,
            height: 450,
            open: function(){
            $('#<%=submitRes.ClientID %>').click();
            }
        });
    }
});
4
  • 1
    I dont see this function Submit_Click and your button don't have the class divPopupAdd Commented Sep 10, 2020 at 13:47
  • The divPopupAdd is applied to a separate image on my page. It's function is to just simply "open" the popup window. That is working fine. The Submit_Click is in my vbscript code behind and that all works perfectly fine. I'm just trying to get it to fire from my button in the popup window. When using a button outside of the popup, everything works great. Commented Sep 10, 2020 at 13:55
  • You haven't showed us the code for the Submit_click Commented Sep 10, 2020 at 13:57
  • The Submit_Click is in my vbscript code behind and that has been working perfectly fine. I'm just trying to get it to fire when I place the button in the popup window. When using that same button "outside of the popup", everything works great. Commented Sep 10, 2020 at 14:02

2 Answers 2

1

Ok, this is how you post back - click a button to run your code behind.

We going to lay this out step by step.

We assume, that you click a button on your form, and it launches the jQuery dialog? Beyond important here that this “is” in fact your flow in this form.

So, I’m going to assume user clicks a button, and the dialog is launched. (and avoid that document on-ready. Please dump it).

Now the dialog is displayed. In that dialog, we assume user enters data, does whatever, and THEN either clicks a button for ok, or clicks a button to cancel?

Next up: You CAN NOT do/use an asp.net button for a post back in that dialog. It WILL NOT work – end of story!

And if you did get the post back to work, it would mess up your current page “dom” that is holding the current web page, and also that of the dialog. It just does not work – don’t do it, ok? (so no post backs in that dialog!!)

But we can still happy make this work!!!

So, how do we setup a button in the dialog?

Lets assume you need to click a button inside the dialog?

Button MUST be client side JavaScript (js). That button will: Close the dialog, and THEN do the post back.

So, we thus need this flow:

Display dialog on button click (client side js).

User does whatever in that dialog.

User now clicks on a button (say ok, or cancel).

We CAN and will for the sake of going crazy? We will drop on the form and use standard asp.net buttons. There is really no need to adopt HTML “input” buttons. (You can, but no real need here).

I am going to use BOTH built in jQuery buttons and ALSO two standard buttons in that dialog (the reason is many, but that way YOU can choose either way, and learn this. (boy, do I with someone had laid out how this works for me!!!). So, I going to save you much pain and suffering here.

So, lets start from the top: Our button code to pop the dialog.

The asp button will be this:

<asp:Button ID="showdialog" runat="server" Text="Show the dialog" 
        OnClientClick="showpop();return false;"/>

Note close in above!!!! We use OnClientClick=showpop();return false. This will thus run js code, and NOT do a post back. The return = false is VERY important here, since if you leave that out, then the standard asp button will post back like it “normally” does. And we break our new rule!! – no post backs inside the dialog!!.

Ok, so that is the button to launch the dialog.

The js code to launch the popup is similar to what you have.

Eg this:

  function showpop() {
        var mydiv = $('#popdialog');
        mydiv.dialog({
            autoOpen: false, modal: true, title: 'My cool dialog, width: '30%'
        });
        // Open the dialog
        mydiv.dialog('open');
    }

Now, again for this example, we assume that you dropped some standard asp buttons in that dialog. But, OFTEN you want to use the built in dialog buttons. So let’s do BOTH for this example.

So, the above code thus becomes this:

    function showpop() {
        var mydiv = $('#popdialog');
        mydiv.dialog({
            autoOpen: false, modal: true, title: 'My cool other page', width: '30%',
            position: { my: 'top', at: 'top+150' },
            buttons: {
                'ok': mycustomok,
                'cancel': mycustomcancel
                }
            
        });
        // Open the dialog
        mydiv.dialog('open');
    }

Note the two built in buttons we added. (ok, and cancel). Note how I also broke out the 'setup' and THEN the one line that pops the dialog.

And in our pop up div, we also have two custom buttons. As noted, I am including both custom and built in dialogs. This will take you “hours” to get nice examples of this, and now you can choose either road.

So, our div has some text, and two custom buttons, and a simple text box.

The div looks like this:

<div id="popdialog" runat="server" style="display:none">
    <h2>My cool pop dialog</h2>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />

    <asp:Button ID="MyOk" runat="server" Text="My custom ok" 
        OnClientClick="mycustomok();return false;"/>
    <br />

    <asp:Button ID="MyCancel" runat="server" Text="My custom cancel" 
        OnClientClick="mycustomcancel();return false;"/>

</div>

The only real thing to note in above is the style=”display:none” for the WHOLE “div”. This will thus hide the div, and it will not display when the page loads.

So, when we click on the button, it will show the dialog. You will see this:

enter image description here

So, we have our two custom buttons, and the two jQuery but in buttons. If you don't want the built in ones then use my first sample code. As I stated, your choice as to which buttons you want, like, or feel to use here. The main point is that BOTH sets of buttons do the SAME thing.

So, for this example, you click “ok”, or “my custom ok”. Either way? We want our post back to run for that choice.

And for my custom cancel, or the built in cancel, we want code behind (server code) to run for that cancel choice. Do keep in mind you often don't need any code - but JUST the dialog to close and cancel and do nothing.

So, the two js stubs we have for this are:

    function mycustomok() {
        // first, close the dialog
        $('#popdialog').dialog('close');
        #('#HiddenOk').click();
    }

    function mycustomcancel() {
        // first, close the dialog
        $('#popdialog').dialog('close');
        $('#HiddenCancel').click();
    }

As noted, if you use BUILT IN buttons for the dialog, then you do NOT need the “close” of the dialog I have above. But your using (we assume) custom buttons, and not the built in jQuery ones.

And as noted, based on either choice, we will do a post back and run code behind. As I stated, the short way, easy way is to drop two hidden buttons on the form, they will look like this, and of course our outside the above div

    <asp:Button ID="HiddenOk" runat="server" Text="hidden ok"
        style="display:normal"/>

    <asp:Button ID="HiddenCancel" runat="server" Text="hidden cancel" 
        style="display:normal"/>

Note careful (note BEYOND carefull here). I have display = normal for the two buttons. The reason is that then with the designer you can double click on the button, and write your code behind. When you are DONE and have both code behind stubs written, then change the above display:normal to display:none to hide them. So we left the buttons visible for easy development. Once you wired up the code behind (simply double click on the buttons in the designer), then you are jumped to the code behind editor, and can write that code.

I have this for now:

Protected Sub HiddenOk_Click(sender As Object, e As EventArgs) Handles HiddenOk.Click
    Debug.Print("dialog ok code run")
End Sub

Protected Sub HiddenCancel_Click(sender As Object, e As EventArgs) Handles HiddenCancel.Click
    Debug.Print("dialgo cancel code")
End Sub

Of course you write whatever you need. As noted, you may well not need any code for the cancel button – you can leave that out of this design if you wish. So, with the two code subs automatic wired up for you (this is why we all love asp.net forms, right???).

The “click” button trick as noted is rather nice. It solves a LONG list of issues, and does so with great ease. You get the needed post back. You get to run your own cute little code stub behind. You don't have to write up ajax calls to do this!!!

So this follows the whole asp.net design pattern in which you drop buttons on a form, click them, and you get to run that nice little short code behind stub. And it all wired up automatic for you.

Thus in summary: Don’t try + attempt post backs in the dialog – you REALLY can’t do this. And you find they don’t work anyway! So buttons on that dialog WILL run “js” code. you can use asp buttons if you want, just remember the extra return=false part.

If you must set/send some information to the server in that dialog before you close? Well, that is a different question and answer. But the jQuery "ajax" method works REALLY nice, and you can directly call functions in your existing web page code behind, and do so with next to no effort. You don't even have to know how to setup web methods - asp.net will do all the dirty work. but lets leave that example for another day.

Try the above idea - the "click" button trick in js really is the magic key to making this all oh so very easy to write and setup.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you... you went above and beyond my expectations. Puts me on the path and opens up many more possibilities. Bravo!!!
0

If you wish to call the JavaScript popup from Backend side you can use ClientScript.RegisterStartupScript

Syntax:

ClientScript.RegisterStartupScript(Type type, string key, string script, bool addScriptTags)

Parameters

Type: The type of the startup script to register. This is of type 'Type'

key: The key of the startup script to register. This is of type String

script: The startup script literal to register. This is of type String

addScriptTags: A Boolean value indicating whether to add script tags. This is of type bool - true or false.

You can write like this:

protected void Button_clilck(Object sender, EventArgs e){
  ClientScript.RegisterStartupScript(GetType(), "Key","showPopUp(),true);

}

And if you want to call your pop up method from Front End side you can simply call the JS method in asp:button by writing OnClientClick

<asp:Button ID="submitRes" CssClass="button" runat="server" ClientIDMode="Static" Text="Submit This Form Data" OnClick="Submit_Click" OnClientClick="showPopup" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.