问题
I am working with ModalPopupExtender. Onclick of button (which is TargetControlID of ModalPopupExtender), calls javascript which checks for some conditions. My requirement is, i want to show popup only if certain condition is true, otherwise hide it.
Here is the code. Hide function is not working here. Popup appears even if hide() is called.
function ShowAlert()
{
if (selBtn == "" || selBtn == null)
{
alert("Please select scrip/scheme first");
var modalPopup = $find('<%=ModalPopupExtender2.ClientID %>');
if (modalPopup != null)
{
modalPopup.hide();
}
return false;
}
}
-----------------------------------------------
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" align="center">
<asp:Button ID="btnSet" runat="server" Text="Set Alerts" OnClientClick="ShowAlert();" />
<cc1:ModalPopupExtender ID="ModalPopupExtender2" runat="server" BehaviorID="ModalBehaviour2" TargetControlID="btnSet" PopupControlID="pnlPopupU" DropShadow="false" X="100" Y="200" BackgroundCssClass="Inactive"></cc1:ModalPopupExtender>
</td>
</tr>
</table>
回答1:
I had a similar situation and could not get it working with Javascript. Instead, I did the conditions check and modalpopup display on server side. Solution was not as clean as with client side javascript but at least I got it working and moved on.
If Condition == true then modalPopUp.Show()
In your panel pnlPopU, place a button(CancelControlID of modalpopupextender) and add onclientclick event to it to hide the popup.
var modalPopup = $find('<%=ModalPopupExtender2.ClientID %>');
modalPopup.hide();
Edit: Another work around: 1) Add a hidden button(display:none) and assign it to modalPE's TargetControlID 2) On btnSet client click event, do your condition check and if condition is met then execute the hidden button click event(that triggers the modalPE)
回答2:
In code behind you can make:
if (true)
{
var script = @"Sys.Application.add_load(function() { $find('behavoirIDModal').show(); });";
ScriptManager.RegisterStartupScript(this, GetType(), "ShowPopup", script, true);
}
and in aspx you make:
<asp:Panel runat="server" ID="pnlSalvo">
<asp:Button ID="btnClose" runat="server" Text="Fechar" />
</asp:Panel>
<ajaxToolkit:ModalPopupExtender ID="ModalResultado" BehaviorID="acao" runat="server" PopupControlID="pnlSalvo"
TargetControlID="btnShow"
CancelControlID="btnClose">
</ajaxToolkit:ModalPopupExtender>
<asp:Button ID="btnShow" runat="server" Text="Modal" Visible="true" />
来源:https://stackoverflow.com/questions/6185226/modalpopupextender-how-to-show-only-when-certain-conditions-are-met