3

First of all I should mention that I spend whole day on searching and can't figure out whats wrong with my code and why it doesn't work ...

I need a modal dialog for confirmation in my code , so I use this jQuery modal dialog : JQuery Confirm

What I need is to embed this functionality with a <asp:Button>...

My problem is that when I click on the button the dialog popup but the yes/no buttons doesn't work and both just disappear the dialog.

Also I use code below but __doPostBack() didn't do anything at all but it runs simple js codes like alert('') perfectly:

$("#complexConfirm").confirm({
     title: "Redirect confirmation",
     text: "This is very dangerous, you shouldn't do it! Are you really really sure?",
     confirm: function (button) {
         __doPostBack('complexConfirm', 'OnClick');
     },
     cancel: function (button) {
         return false;
     },
     confirmButton: "Yes",
     cancelButton: "No"
});

I search and dialog cause element comes outside of the form element so I should bring it back to the form but I don't know how I try below code and it doesn't

$("#editEventModal").parent().appendTo(jQuery("form:first"));

Another probelm is I try to postback with javascript event without the dialog box, I use 2 button one for calling javascript and another for calling that , but when I use __doPostBack it refresh the page but doesn't comes to my button event in the code-behind, I try several ways to calling button onClick event but it just refresh the page and doesn't call my butoon OnClick Mehtod !!!

The buttons declaration are like this :

<asp:Button ID="complexConfirm" ClientIDMode="Static" runat="server" OnClick="TestBTN_Click" Text="Test" />
<asp:Button ID="checkit" ClientIDMode="Static" runat="server" OnClientClick="__doPostBack('<%=complexConfirm.UniqueID%>', '');" Text="CHECK" />

What should I do for these two problems ?

I search a lot and non of the solutions worked for me !!!

I will appreciate any solution, thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohamad MohamadPoor
  • 1,350
  • 2
  • 14
  • 35
  • avoid the `__doPostBack` and use this trick http://stackoverflow.com/questions/5448825/is-it-ok-to-use-dopostback – Aristos Jul 30 '14 at 16:07
  • I tried it before, it's the same, just refresh the page without running button event handler :( – Mohamad MohamadPoor Jul 30 '14 at 17:20
  • Are you using a static client ID mode for the asp:button? You need to use the button's uniqueID (html name attribute) for the `__doPostBack` function to work. Something like `__doPostBack('<%=myButton.UniqueID%>', '');` – Eric Jul 30 '14 at 19:59
  • I add button declaration, please check and tell me what is wrong with it... – Mohamad MohamadPoor Jul 31 '14 at 12:53

1 Answers1

1

Without knowing the declaration of your asp:button, here is my best guess.

You need to use the uniqueID of the button, the button's "name" attribute, not it's ID.

__doPostBack('<%=complexConfirm.UniqueID%>', '');

also, since its a button, you don't actually need to specify OnClick

EDIT: I created a working example demonstrating what i think you're trying to accomplish. You can tailor this to your jQuery confirm as needed.

aspx:

<asp:Button ID="btnTest1" runat="server" Text="Test1" OnClick="btnTest1_Click" />

javascript:

<script>
    $('#<%=btnTest1.ClientID%>').confirm({
        text: "This is very dangerous, you shouldn't do it! Are you really really sure?",
        title: "Confirmation required",
        confirm: function (button) {
            // do something
            __doPostBack('<%= btnTest1.UniqueID%>', '');
        },
        cancel: function (button) {
            // do something
        },
        confirmButton: "Yes I am",
        cancelButton: "No",
        post: true
    });
</script>

code behind:

protected void btnTest1_Click(object sender, EventArgs e)
{
    Response.Write("You clicked YES");
}

I think part of the problem was that you're event name wasn't named with the name of your button. If using autoEventWireup, which I think you are, then your event wont trigger because it can't find an event with a matching name.

So, first thing I did was eliminate the StaticClientID. you can still use jquery with asp.net generated IDs. Then I made sure the event was named btnTest1_Click because the name of the button is btnTest1

I'm not sure why you needed the other button, because it seems all you're trying to do is confirm before the button submits.

Eric
  • 930
  • 1
  • 6
  • 14
  • thanks for your answer, I try your code but it like before, I edit my question and add my buttons declaration ... – Mohamad MohamadPoor Jul 31 '14 at 12:47
  • you need to remove this from your button i think: `OnClick="TestBTN_Click"`. if you are triggering post back manually, you won't want this there. – Eric Aug 02 '14 at 03:30
  • no it is for another button !!! I have two button , and need to fire postback event of one by another ! if I remove OnClick how can I set code-behind event handler for button ?? – Mohamad MohamadPoor Aug 02 '14 at 10:15
  • Please see my edit. I got this working for you using jquery.confirm – Eric Aug 02 '14 at 14:53
  • thank you a lot for your answer, I just copy your solution in a new fresh web form page, but it doesn't work either !!! what is wrong with it ? is it possible that the problem is with localhost? – Mohamad MohamadPoor Aug 03 '14 at 13:49
  • I figure out something strange !!! when I place script part below the button declaration in aspx, the dialog showed but the postback doesn't work! when I put it above of the button it just do the postback and the dialog doesn't show up !!! how strange is this :-s can you give me the whole solution so I can check the position ?! although I Know this is so strange !!! – Mohamad MohamadPoor Aug 03 '14 at 14:13
  • I did this example with the script below the button on the page. When get home later, I will give you the working solution. The reason the button just posts back when you put the script above the button, is because jQuery cannot find the button element. If you want to put it above, you have to wrap in a document.ready jQuery event. Otherwise, the DOM isn't ready yet. – Eric Aug 03 '14 at 15:34
  • I am very thankful for you answering, I use document.ready jQuery and it become like the state when it is below, show the dialog but doesn't do the response.write() part ! I am waiting for you solution, thanks a lot. – Mohamad MohamadPoor Aug 03 '14 at 20:42
  • http://www.neptunecentury.com/Files/jQueryConfirm.zip Let me know when you get this and try it out – Eric Aug 04 '14 at 12:13
  • finally I find my mistake, I don't have any ScriptManager in my asp page, when I add this it worked ... Thank you so much Eric :-) – Mohamad MohamadPoor Aug 04 '14 at 15:22