简体   繁体   中英

Jquery Dialog is not firing when call from the C# code behind

This is my .aspx :

 <div id="divAddrCandidates" class="selector"   style="display:none">
    <div style="margin-left:10px ; margin-top:10px">
        <asp:ListBox ID="lstCandidates" runat="server" ></asp:ListBox>
    </div>
     <br />         
    <div style="margin-left:10px">
        <asp:Button ID="btnSelect" runat="server" Text="Select Address" />
       <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" onclientclick="CloseAddrCandidatesWin(); return false;" />
    </div>
</div>  

This the jQuery:

    function OpenAddrCandidatesWin() {
        $("#divAddrCandidates").dialog({
            resizable: true,
            width: 650,
            heigh: 450,
            modal: true,
            draggable: true,
            resizable: true                
        });
        $(".selector").dialog({ dialogClass: 'no-close' });
        jQuery("#divAddrCandidates").parent().appendTo(jQuery("form:first"));
    }


    function CloseAddrCandidatesWin() {
        $("#<%=lstCandidates.ClientID %>").val("");
        $("#<%=lstCandidates.ClientID %>").hide();
        $("#divAddrCandidates").dialog("close");
    }

This is my code behind:

protected void Save_Click(object sender, System.EventArgs e)
 {
         JsonAddresses = WSJson.GetAddressCandidate(physicalAddrToProcess);
                int count = JsonAddresses.candidates.Length;
                for (int i = 0; i < count; i++)
                {
                    lstCandidates.Items.Add(JsonAddresses.candidates[i].address.ToString());
                    }


        string key = "_OpenAddrCandidatesWin";
        string script = "OpenAddrCandidatesWin();";
        if (!Page.ClientScript.IsStartupScriptRegistered(key))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), key, script, true);
        }
}

What did I miss here? The code went through without error but the dialog doesn't pop up, although I set a break at the jquery but it never stops there. Any help would be appreciated.

It is probably because you are executing the method before even dom is loaded.

Try changing string script = "OpenAddrCandidatesWin();"; to string script = "$(function () { OpenAddrCandidatesWin(); })"; which will wait until the dom is loaded and then execute the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM