简体   繁体   English

如何使用是或否按钮获取OnClientClick Return的值

[英]How to get the value of OnClientClick Return with yes or no buttons

I know how to get the return confirm box to popup on the button click but in my case, I only want to get which button was clicked by the user ("yes" or "no). Once the client clicks on the AddToCartBtn I am redirecting them to another url. I want them to be redirected to the URL no matter what they click on but with the Return Confirm, it will cancel the redirect at the moment when the click no. Should I be using something other than "Confirm" in this case to receive the value that the user chose and continue to redirect them to a new page afterwards? The only code I have so far is only for the simple confirmation popup. 我知道如何在按钮单击上弹出返回确认框,但在我的情况下,我只想获取用户单击了哪个按钮(“是”或“否”)。一旦客户端单击AddToCartBtn,我就是将它们重定向到另一个URL。无论他们单击什么,我都希望将它们重定向到该URL,但使用“返回确认”,它将在单击“ No”时取消重定向。我应该使用“ Confirm”以外的其他名称在这种情况下,要接收用户选择的值并在以后继续将其重定向到新页面?到目前为止,我仅有的代码仅用于简单的确认弹出窗口。

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

I have been unable to find much else although I found a custom popup in javascript that includes a checkbox which the checkbox control could take place of the yes or no buttons just as long as I can get the value but I am not able to get this to work, I will include this code also just in case this is the direction I should be going in 尽管我在javascript中找到了一个自定义弹出窗口,其中包含一个复选框,但只要我能获得该值,但我无法获得此复选框,则该复选框控件可以代替yes或no按钮,所以我一直找不到其他东西工作时,我还将包括此代码,以防万一这是我应该前进的方向

<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>

I am not use to using javascript with asp.net so I am not exactly sure how I call this javascript in the onClick event for my button. 我不习惯在asp.net上使用javascript,因此我不确定在按钮的onClick事件中如何调用此javascript。 The button and containers below the javascript code are test examples from a site but in my case I need the button to be my asp:ImageButton "AddToCartBtn" instead of the html button. javascript代码下面的按钮和容器是一个站点的测试示例,但就我而言,我需要将该按钮作为asp:ImageButton“ AddToCartBtn”而不是html按钮。 So to recap, Is there a way to use the return confirm popup and get the value that the user clicked on, but still redirect them to the page if they click no? 因此,有一种方法可以使用返回确认弹出窗口并获取用户单击的值,但是如果他们单击否,仍然可以将其重定向到页面? Or should I be going more towards the custom checkbox popup, and if so how can I get the example I listed for the checkbox to work with my asp:imageButton? 还是我应该更着重于自定义复选框弹出窗口,如果可以,如何获取列出的示例以使其与asp:imageButton一起使用?

You can use an ModalPopupExtender rather than a js confirm. 您可以使用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" />

Just replace your OnClientClick with the call to a function: 只需将OnClientClick替换为对函数的调用即可:

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

The JS function: JS函数:

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

    // Do something with result.

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM