3

I'm having a problem opening a JQuery Dialog from C# codebehind. It works on other pages I did, but on this one it's not working. I've already tried this, this, this and this. But I'm missing something.

Basically, there's a table with a GridView on the left and a counter with image buttons on the right. What I'm trying to do is to show a dialog to the user to confirm if he wants to reset the counter when Stop button (btnZerar) is clicked.

I put a breakpoint on #StopDialogConfirm and it's only reached when the page is loaded, but it's not reached when the button is clicked. There's no javascript error.

Follow my code samples:

ASPX:

<asp:Content ID="scriptsHeader" ContentPlaceHolderID="FeaturedContent" runat="server">
<script type="text/javascript" src="/Scripts/Pages/EstudeCiclos.js"></script>
<script type="text/javascript" src="/Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery-ui-1.8.20.min.js"></script>
</asp:Content>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">    
<asp:UpdatePanel ID="updPanelEstudeCiclos" UpdateMode="Conditional" runat="server"> <ContentTemplate>
...
                <div>
                    <asp:ImageButton ID="btnPlay" runat="server" OnClick="btnPlayClick" ImageUrl="~/Images/play.jpg" Height="40px" Width="40px" AlternateText="Começar a contar o tempo" />
                    <asp:ImageButton ID="btnPause" runat="server" OnClick="btnPauseClick" ImageUrl="~/Images/pause.jpg" Height="40px" Width="40px" AlternateText="Parar tempo" />
                    <asp:ImageButton ID="btnZerar" runat="server" OnClick="btnZerarClick" ImageUrl="~/Images/stop.jpg" Height="40px" Width="40px" AlternateText="Zerar tempo" />
                    <asp:ImageButton ID="btnSalvar" runat="server" OnClick="btnSalvarClick" ImageUrl="~/Images/save.jpg" Height="40px" Width="40px" AlternateText="Salvar tempo" />
                </div>
...
    <div>
        <asp:Button ID="btnStop" runat="server" OnClick="btnStop_Click" Style="display: none;" ClientIDMode="Static" />
    </div>
</ContentTemplate> </asp:UpdatePanel>

CodeBehind - code commented out are things I have tried. When I try to open an alert, it works.

    private void OpenQuestionDialog(string functionName, string question)
    {
        string s = "$(function(){$('#" + functionName + "').dialog('open').text('" + question + "');});";

        //ScriptManager.RegisterStartupScript(Page, this.GetType(), "Dialog", s, true);

        ScriptManager requestSM = ScriptManager.GetCurrent(this);
        if (requestSM != null && requestSM.IsInAsyncPostBack)
        {
            ScriptManager.RegisterClientScriptBlock(this,
                                                    typeof(Page),
                                                    Guid.NewGuid().ToString(),
                                                    s,
                                                    true);
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                   Guid.NewGuid().ToString(),
                                                   s,
                                                   true);
        }
    }


    protected void btnZerarClick(object sender, EventArgs e)
    {
        OpenQuestionDialog("StopDialogConfirm", "Are you sure you want to reset the counter?");
    }

Script (EstudeCiclos.js):

$(function () {
$('#StopDialogConfirm').dialog({
    autoOpen: false,
    width: 450,
    modal: true,
    buttons: {
        "Não": function ()
        {
            $(this).dialog("close");
        },
        "Sim": function ()
        {
            $(this).dialog("close");
            $('#btnStop').click();
        }
    }
});
});

Please, help! Thanks!

Community
  • 1
  • 1
Andre
  • 31
  • 3

2 Answers2

0

The likely issue is that the script generated by RegisterClientScriptBlock is added at the start of the page, so it could be that the appropriate jQuery libraries are not loaded or initialized.

There are a few possible things to try:

  1. Use RegisterStartupScript instead, since it is executed after the page is loaded. This still may not work if jquery is initialized in the window onload method.

  2. Add the method to be executed to window.onload or the intrinsic pageLoad method instead of directly in the page.

  3. (My preference) Change the button execution code to be completely client-side. If you are just opening a popup and don't need anything from the page, there is no reason to perform a full postback.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • thanks for your answer.. regarding #1 unfortunately it didn't work.. regarding #2, you mean that on .js file that function should be declared inside $(window).load(function () { $('#StopDialogConfirm').dialog ... } ? regarding #3, what can I use to execute it client-side? – Andre Dec 29 '13 at 22:52
  • 1
    @Andre: You can put your javascript to execute in a function directly within the page, then change your button's code to call that function in OnClientClick (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.onclientclick%28v=vs.110%29.aspx). You will want to remove the OnClick property from the button. – competent_tech Dec 30 '13 at 00:56
0

This need be in document.ready function

function FlceSaveConfirmation() {
            $(function () {
                $("#dialog-flce").dialog({
                    resizable: false,
                    height: 200,
                    modal: true,
                    buttons: {
                        Ok: function () {

                            $("#tabs").tabs('option', 'enable', [4, 5, 6]);
                            FlceCeLevelInfo();
                            $(this).dialog("close");
                            //SelectParticularTab();
                        }
                    }
                });
            });
        }

In code behind

Page . ClientScript . RegisterStartupScript ( this . GetType ( ) , "CallMyFunction" , "FlceSaveConfirmation()" , true );
Sumant
  • 163
  • 1
  • 8