Question

I have the following code i an ascx, where i try to prevent a button from doing a postback, and then making a alert to verify (is supposed to call a handler later, but for now the alert will do), the postback is prevented, but the alert doesn't show, if i remove the $("#pollvote").... and just have the alert, it shows up fine on page load.

<script type="text/javascript">
        $(document).ready(function () {
            $("#pollVoteBtn").click(function (event) {
                event.preventDefault();
                alert("Clicked");
            });

        });
</script>

<asp:Label ID="pollQuestion" runat="server" /><br />
<asp:RadioButtonList ID="pollRbt" runat="server" /><br />
<asp:Button ID="pollVoteBtn" runat="server" Text="Stem"/>
<asp:Label ID="pollTestLabel" runat="server" />
Was it helpful?

Solution

You do not have ClientIDMode="static" for button and id of button will be changed on client, so use ClientID to bind javascript event.

$("#<%= pollVoteBtn.ClientID %>").click(function (event) {
     event.preventDefault();
     alert("Clicked");
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top