使用Javascript管理LinkBut​​ton?

时间:2013-02-01 21:24:19

标签: javascript asp.net

我在视图上有一个名为“退出聊天”的LinkButton。我将javascript代码嵌入OnClientClink=javascript:confirm('Are you sure you want to end the session?')。但是,我无法管理对话框

当用户点击“确定”按钮时,视图应该终止。我该怎么做?

<asp:LinkButton ID="LinkButton2" runat="server" Font-
    Bold="False" Font-Underline="False" 
    OnClientClick="javascript:confirm('Are you sure you want to end the session?')"
    ViewStateMode="Enabled" OnClick="LinkButton2_Click1" Text="Exit Chat">
</asp:LinkButton>

我写了这段代码,但没有用:(

if (LinkButton2.CommandName=="OK")
{
    MultiView1.ActiveViewIndex = -1;
}
else if(LinkButton2.CommandName=="Cancel")
{
    MultiView1.ActiveViewIndex = 1;
}

1 个答案:

答案 0 :(得分:0)

您可以检查确认结果,然后关闭窗口,如果为真。

<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
        CommandName="Delete" Text="Delete"
        OnClientClick="return showConfirm();">
</asp:LinkButton>

<script>
function showConfirm()
{
   var ok = confirm('Are you certain you want to delete this product?');

   if (ok)
   {
      window.close();
   }
}
<script>