1

<asp:Button ID="btnShowPopup" runat="server" Text="Show popup" />
    <div id="dialog" style="display: none">
        <table>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                </td>
            </tr> 
            <tr>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </div>

<script type="text/javascript">
    $(function () {
        $("[id*=btnShowPopup]").click(function () {
            ShowPopup();
            return false;
        });
    });

    function ShowPopup() {
        $("#dialog").dialog({
            title: "Test",
            width: 500,
            buttons: {
                Close: function () {
                    $(this).dialog('close');
                }
            },
            modal: true
        });
     }
</script>
I'm trying to pass the textbox1 value to label1 on my button1 click event.But unfortunately asp button click events not firing on modal dialog.Give me a solution to correct it in a right way.
rrk
  • 15,677
  • 4
  • 29
  • 45
thilim9
  • 227
  • 2
  • 8
  • 17
  • how many buttons of the same type do you have in your page? – rrk Aug 23 '15 at 06:55
  • Currently i'm having two buttons on my page.One for show modal dialog and other one for submitting the values – thilim9 Aug 23 '15 at 06:58
  • Can you please show onclick funtion – vijayP Aug 23 '15 at 07:12
  • @vijayP this is my onclick function [ Label1.Text = TextBox1.Text; ] assigning textbox1 value to label1.Do I need to write a client script? – thilim9 Aug 23 '15 at 07:14
  • 1
    possible duplicate of [jQuery UI Dialog with ASP.NET button postback](http://stackoverflow.com/questions/757232/jquery-ui-dialog-with-asp-net-button-postback) – Taleeb Aug 23 '15 at 08:36

2 Answers2

0

Try the following code:

function ShowPopup() {
            var dlg = $("#dialog").dialog({
                title: "Test",
                width: 500,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
            dlg.parent().appendTo($("form:first"));
        }

Due to postback the popup will be lost. To show the popup again on postback we can maintain a hidden field

<head runat="server">

    <title></title>
    <script type="text/javascript" src="content/js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script type="text/javascript">
$(function () {
            $("[id*=btnShowPopup]").click(function () {
                ShowPopup();
                return false;
            });

            $('#Button1').click(function () {
                $('#hdnPostback').val(true);
            });

            if ($('#hdnPostback').val() == 'true') {
                $('#hdnPostback').val(false);
                ShowPopup();
            }
        });

        function ShowPopup() {
            var dlg = $("#dialog").dialog({
                title: "Test",
                width: 500,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
            dlg.parent().appendTo($("form:first"));
        }
    </script>
</head>
<body >
    <form id="form1" runat="server">
    <asp:Button ID="btnShowPopup" runat="server" Text="Show popup" />
    <div id="dialog" style="display: none">
        <table>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                </td>
            </tr> 
            <tr>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
                </td>
                <td>
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        <asp:HiddenField runat="server" ID="hdnPostback" Value="false" />
    </div>
    </form>
</body>

In case of master pages - the client id is dynamically generated (so the id of hidden control is no longer hdnPostback)

Based on this the code needs to be modified something like this:

$(function () {
            $("[id*=btnShowPopup]").click(function () {
                ShowPopup();
                return false;
            });

            var hdnPostbackID = '<%= hdnPostback.ClientID %>'

            $('#Button1').click(function tt() {
                $('#' + hdnPostbackID).val(true);
            });

            if ($('#' + hdnPostbackID).val() == 'true') {
                $('#' + hdnPostbackID).val(false);
                ShowPopup();
            }
        });


        function ShowPopup() {
            var dlg = $("#dialog").dialog({
                title: "Test",
                width: 500,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
            dlg.parent().appendTo($("form:first"));
        }
Taleeb
  • 1,888
  • 18
  • 25
0

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <asp:Button ID="btnShowPopup" runat="server" Text="Show popup" />
    <div id="dialog" style="display: none">
        <table>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                </td>
            </tr> 
            <tr>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        <asp:HiddenField runat="server" ID="hdnPostback" Value="false" />
    </div>
</asp:Content>

<script type="text/javascript">
        $(function () {
            $("[id*=btnShowPopup]").click(function () {
                ShowPopup();
                return false;
            });

            $('#Button1').click(function tt() {
                $('#hdnPostback').val(true);
            });

            if ($('#hdnPostback').val() == 'true') {
                $('#hdnPostback').val(false);
                ShowPopup();
            }
        });


        function ShowPopup() {
            var dlg = $("#dialog").dialog({
                title: "Test",
                width: 500,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
            dlg.parent().appendTo($("form:first"));
        }

</script>
thilim9
  • 227
  • 2
  • 8
  • 17