我有以下HTML代码:
<a id="PopUp" runat="server" href="#openModal">Open me</a>
// this is a way to open the pop up window via HTML
<div id="openModal" runat="server" class="modalDialog">
<div>
<a href="#close" title="Close" class="close">X</a>
<h2 style="text-align:center">Error</h2>
<p style="text-align:center"> Please select the product first!</p>
</div>
</div>
正如你所看到的,我正在设置我的href链接到#openModal,以便我可以打开用CSS设计的弹出窗口...我现在的问题是,有什么方法可以动态地执行此操作代码背后的C#?
我尝试过以下代码:
if(something..)
{
do something...
}
else
{
// now we need to open the pop up message if the if didn't pass first term...
// I tried the following:
openModal.Attributes.CssStyle.Add(HtmlTextWriterStyle.Display, "block");
// or
PopUp.Href="#openModal";
}
但这些都不起作用,有人可以帮我解决这个问题,我怎么能从代码背后做到这一点?谢谢!
P.S。当用户单击listview中的按钮(普通的asp.net按钮)时,代码被设置为触发...
编辑:( Roberth ,你的意思是这样吗?):
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="DropDownList1" eventname="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger controlid="Button1" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" runat="server"></asp:DropDownList>
<cc1:ModalPopupExtender runat="server" ID="kontroller" PopupControlID="popUpController" TargetControlID="PopUp">
</cc1:ModalPopupExtender>
<asp:Button ID="PopUp" runat="server" CommandName="AddToCart" CommandArgument='<%# Eval("ProductID")%>' Text="Add to cart" />
</ContentTemplate>
</asp:UpdatePanel>
代码背后:
ModalPopupExtender mpe = e.Item.FindControl("kontroller") as ModalPopupExtender;
mpe.Show();
return;
但没有任何反应......
答案 0 :(得分:2)
您无法在javascript中以代码隐藏的方式在浏览器中启动某些操作。您只能在回发的响应期间更新客户端。
要在不刷新整个页面的情况下更新客户端,您应该使用UpdatePanel。要启动PopUp,您可以使用ModalPopupExtender(NuGet中的AjaxControlToolkit的一部分)。
所以,整个过程如下:
以下是一个示例:您可以使用 LabelMsgBox.Text =
设置对话框的文本<asp:Button ID="HiddenButtonPopUpMsgBox" runat="server" Style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="MsgBoxModalPopupExtender" runat="server" DropShadow="True"
PopupDragHandleControlID="DragPanelMsgBox" BackgroundCssClass="popUpbackground"
CancelControlID="ButtonMsgBoxClose" PopupControlID="PanelMsgBox" TargetControlID="HiddenButtonPopUpMsgBox">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="PanelMsgBox" runat="server" CssClass="popUpPanel" Style="display: none"
DefaultButton="ButtonMsgBoxClose">
<asp:Panel ID="DragPanelMsgBox" runat="server">
<div class="popUpDragPanel">
</div>
</asp:Panel>
<fieldset>
<legend class="legendlist">Message</legend>
<asp:UpdatePanel ID="UpdatePanelMsgBox" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="PanelMsgScroll" runat="server" CssClass="PopUpPanelMsgPanel">
<asp:Label ID="LabelMsgBox" runat="server" Text="-" CssClass="MsgBoxLabel"></asp:Label>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<div align="center">
<asp:Button ID="ButtonMsgBoxClose" runat="server" Text="OK"
CssClass="button" />
</div>
</fieldset>
</asp:Panel>
你可以用这个打开它:
public static void MsgBox(string title, Page page)
{
AjaxControlToolkit.ModalPopupExtender ModalPopupExtender =
page.FindControl("ctl00$MainContent$MsgBoxModalPopupExtender") as AjaxControlToolkit.ModalPopupExtender;
System.Web.UI.WebControls.Label Label =
page.FindControl("ctl00$MainContent$LabelMsgBox") as System.Web.UI.WebControls.Label;
string message = "<b>" + title + "</b>";
Label.Text = message;
UpdatePanel UpdatePanel = page.FindControl("ctl00$MainContent$UpdatePanelMsgBox") as UpdatePanel;
UpdatePanel.Update();
ModalPopupExtender.Show();
}