如何使用是或否按钮获取OnClientClick返回值

时间:2014-12-19 15:28:33

标签: javascript asp.net

我知道如何在按钮点击时弹出返回确认框但在我的情况下,我只想知道用户点击了哪个按钮("是"或"否)。客户端点击AddToCartBtn后,我将它们重定向到另一个URL。我希望将它们重定向到URL,无论它们点击什么,但使用“返回确认”,它将在点击“否”时取消重定向。我应该使用&#34以外的其他内容吗?确认"在这种情况下,接收用户选择的值,然后继续将它们重定向到新页面?到目前为止,我唯一的代码仅用于简单的确认弹出窗口。

<asp:ImageButton ID="AddToCartBtn" runat="server" RowIndex='<%# Container.DisplayIndex %>'
ImageUrl="~/Pictures/ShoppingCart.png"                                          
OnClick="AddToCartBtn_Click" OnClientClick="return confirm('are you sure?')" />

我找不到其他的东西虽然我在javascript中找到了一个自定义弹出框,其中包含一个复选框,只要我可以获得该值,但是我无法使用复选框控件可以取消是或否按钮为了使这个工作,我将包括这个代码,以防这是我应该去的方向

<script type="text/javascript">
$('AddToCartBtn').click(function() {
var answer = $("#dialog").dialog()
    .find(':checkbox').unbind('change').bind('change', function(e){
    if(this.checked) alert('checked box');
    else alert('unchecked box');
});
})
</script>

<button>CLICK ME</button>

<div id='illegal'></div>

<div id='numberplateyellow'></div>

<div id='numberplatewhite'></div>

<div id='dialog'>
The registration you have entered is illegal for the uk roads. By clicking ok you are accepting          full resposibility for this plate and agreeing to use it for offroad use only.
<br/>
  Check here if you agree to the terms and conditions: <input type='checkbox'/>
</div>

我不习惯在asp.net上使用javascript,所以我不确定如何在onClick事件中为我的按钮调用此javascript。 javascript代码下面的按钮和容器是来自网站的测试示例,但在我的情况下,我需要按钮作为我的asp:ImageButton&#34; AddToCartBtn&#34;而不是HTML按钮。那么回顾一下,有没有办法使用返回确认弹出窗口并获取用户点击的值,但如果他们点击否,仍然会将它们重定向到页面?或者我应该更多地转向自定义复选框弹出窗口,如果是这样,我怎样才能获得我列出的示例,以便使用我的asp:imageButton复选框?

2 个答案:

答案 0 :(得分:0)

您可以使用ModalPopupExtender而不是js确认。

<asp:ImageButton ID="AddToCartBtn" runat="server" RowIndex='<%# Container.DisplayIndex %>'
ImageUrl="~/Pictures/ShoppingCart.png" />

<asp:Panel ID="pnlPopup" runat="server">
    <asp:Label ID="lblSure" Text="Are you sure?" />
    <asp:Button ID="btnYes" runat="server" Text="YES" OnClick="AddToCartBtn_Click" />
    <asp:Button ID="btnNo" runat="server" Text="NO" />
</asp:Panel>

<ajaxToolKit:ModalPopupExtender ID="mpe" runat="server"     
    TargetControlID="AddToCartBtn" 
    PopupControlID="pnlPopup" 
    OkControlID="btnYes"
    CancelControlID="btnNo" />

答案 1 :(得分:0)

只需用函数调用替换OnClientClick

<asp:ImageButton ID="AddToCartBtn" runat="server" RowIndex='<%# Container.DisplayIndex %>' ImageUrl="~/Pictures/ShoppingCart.png" OnClick="AddToCartBtn_Click" OnClientClick="return Redirect();" />

JS功能:

function Redirect() {
    var result = confirm('Are you sure?');

    // Do something with result.

    return true; // return true no matter what the user clicked.
}