Showing posts with label jQuery PostBack. Show all posts
Showing posts with label jQuery PostBack. Show all posts

Tuesday, June 26, 2012

Asp.net jQuery Dialog control events not firing


When you display a DIV tag with Asp.Net server side controls, in a jQuery Dialog the server side events will get disabled and the events will not get fired, this happens because the controls are moved out of the form to display them as a Dialog box, to enable the events we need to move the controls back to the form, this can be done by adding the following code to the open event of the Dialog.

open: function(type, data)
{
    $(this).parent().appendTo("form");
}


Once this is done, the controls will be added back to the form and the events triggered by the controls will start to fire.

Full example code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>jQuery Dialog with Controls</title>
    <script type="text/javascript" src="JavaScript/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="JavaScript/jquery-ui-1.8.21.custom.min.js"></script>
    <link type="text/css" href="css/smoothness/jquery-ui-1.8.21.custom.css" rel="Stylesheet" />
    <style type="text/css">
        .ui-dialog .ui-dialog-buttonpane
        {
            text-align: left;
        }
        .ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset
        {
            float: none;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function() {       
        $('#addUser').dialog(
        {
            width: 350,
            height: 160,
            autoOpen: false,
            modal: true,
            open: function(type, data)
{
                $(this).parent().appendTo("form");
            }
        });
        //
        $('#cmdAddUser').click(function(event) {
            event.preventDefault();
            $('#addUser').dialog('open');
        });
        });
    </script>
</head>
<body>    
    <form id="jQueryDialog" runat="server">
        <div id="myDialog" style="display:none;" title="Hello Dialog">
            Hello !!! <br /><br />
        </div>
        <div id="addUser" style="display:none;" title="Add User">
            <table>
                <tr>
                    <td>Name:</td>
                    <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Age:</td>
                    <td><asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="cmdSave" runat="server" Text="Save" />  
                        <asp:Button ID="cmdCancel" runat="server" Text="Cancel" />
                    </td>
                </tr>
            </table>
        </div> 
        <asp:Button ID="cmdAddUser" runat="server" Text="Add User" />             
    </form>
</body>
</html>

Run the application, click on the Add User button, the Dialog will get displayed.

Open jQuery Dialog on Asp.net buttons Click.




To open a jQuery Dialog on an Asp.net button click, we need to disable the default post back events, this can be done by adding the following line.
event.preventDefault();


Now we will have to open the Dialog on the click event of the Asp.net button, as follows

$(document).ready(function() {
$('#cmdHello').click(function(event)
{
event.preventDefault(); // To Prevent PostBack
$('#myDialog').dialog   // Open the Div Tag 'myDialog' as Dialog
(     
{
    width: 150,
    height: 150
}
);
}
);
});

<div id="myDialog" style="display:none;" title="Hello Dialog">           
Hello !!! <br /><br />
</div>
<asp:Button ID="cmdHello" runat="server" Text="Say Hello" />


Full example code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>jQuery Dialog with Controls</title>
    <script type="text/javascript" src="JavaScript/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="JavaScript/jquery-ui-1.8.21.custom.min.js"></script>
    <link type="text/css" href="css/smoothness/jquery-ui-1.8.21.custom.css" rel="Stylesheet" />
    <style type="text/css">
        .ui-dialog .ui-dialog-buttonpane
        {
            text-align: left;
        }
        .ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset
        {
            float: none;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function() {
       
        $('#cmdHello').click(function(event) {
            event.preventDefault();    
            $('#myDialog').dialog(  
            {
                width: 150,
                height: 150
            }
            );
        });
        });
    </script>
</head>
<body>    
    <form id="jQueryDialog" runat="server">
        <div id="myDialog" style="display:none;" title="Hello Dialog">
            Hello !!! <br /><br />
        </div>
        <asp:Button ID="cmdHello" runat="server" Text="Say Hello" />
    </form>
</body>
</html>

Run the application, click on the Say Hello button, the Dialog will get displayed.


Monday, June 25, 2012

Asp.net jQuery Dialog buttons do Post back.


For the jQuery Dialog to appear, we need to disable the default post back events, we would have already done this by adding the line.
event.preventDefault();


But now, once the user clicks the confirm button we need to invoke the server side event to process the data, to do so we need to initiate a postback, we can do so by adding the following line.

<%= Page.ClientScript.GetPostBackEventReference(this.cmdShowDialog, string.Empty) %>;

Full example code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Dialog</title>
    <script type="text/javascript" src="JavaScript/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="JavaScript/jquery-ui-1.8.21.custom.min.js"></script>
    <link type="text/css" href="css/smoothness/jquery-ui-1.8.21.custom.css" rel="Stylesheet" />
    <style type="text/css">
        .ui-dialog .ui-dialog-buttonpane
        {
            text-align: left;
        }
        .ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset
        {
            float: none;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function() {
            var confirm = function()
{
$(this).dialog("close");
<%= Page.ClientScript.GetPostBackEventReference(this.cmdShowDialog, string.Empty) %>;
}
            var cancel = function() { alert('No Button Clicked'); }
            $('#cmdShowDialog').click(function(event) {
            event.preventDefault();
                $('#myDialog').dialog
                    (
                        {
                            modal: true,
                            close: function(event, ui){alert('Dialog Closed');},
                            buttons:
                            [
                                {
                                    text: "Yes",
                                    click: confirm
                                },
                                {
                                    text: "No",
                                    click: cancel
                                }
                            ]                           
                        }
                    );
            });
            $('#cmdHideDialog').click(function() {
                $('#myDialog').dialog('close');
            });
        });
    </script>
</head>
<body>
    <div id="myDialog" style="display:none;" title="Hello Dialog">
        Please Select Yes/No to Proceed. <br /><br />
    </div>
    <form id="frmDialog" runat="server">       
        <div>
<asp:Button
ID="cmdShowDialog"
runat="server"
Text="Show Dialog" />          
<input type="button"
                id="cmdHideDialog"                
                value="Hide Dialog" />
        </div>
    </form>
</body>
</html>

Run the application, click on the Show Dialog button, the Dialog will get displayed.