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"));
}