简体   繁体   中英

ASP.NET - Edit CommandName on LinkButton using Javascript

I have a series of link buttons in a repeater that switch pages

<asp:Repeater ID="rptPageLinks" runat="server"><ItemTemplate>
    <asp:LinkButton ID="lnkNewPage" runat="server" Text='<%# Eval("Page") %>'
    OnCommand="SomeEventHandler" CommandArgument='<%# Eval("Page") %>' CommandName="Redirect"
    OnClientClick="return confirm('Do you want to save your chanes before you navigate to a new page?');" />
</ItemTemplate></asp:Repeater>

What I want to be able to do is pass the result from the OnClientClick into the EventHandler, ideally by changing the CommandName (ie if they click 'yes' on the confirmation it changes the CommandName to "SaveAndRedirect", if they click 'no' it stays as 'Redirect').

Can anyone help? Thanks if you can.

Unfortunately, what you ask is not possible in that way. OnCommand is an ASP.Net server side attribute of the asp control. On the client once rendered, you only have the client side html to work with.

The solution, however, is to save the value of the confirm dialog to your command event in a hidden field that you then read in your command. To do this, I would suggest something like:

<script type="text/javascript">
    function confirmSave() {
        document.getElementById('confirmChoice').value=confirm("Do you want to save your changes...");
    }
</script>
<input type='hidden' id='confirmChoice' runat="server" />

Then, within your page behind code called on the command event, you can access the value that was set into the hidden field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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