Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends,

I'm New to jQuery/

In my asp.net page I have
C#
<asp:Button ID="btn" runat="server" Text="Button" OnClick="btn_Click"/>
protected void btn_Click(object sender, EventArgs e)
{
    Response.Write(DateTime.Now.ToString());
}

and in jquery
C#
$(document).ready(function () {
    $("#btn").click(function (e) {
        e.preventDefault();
        if (confirm("Hello World!!")) {
            $("#btn").click();
        }
    });
});



I want to make post back if user press yes on confirm dialog.
How can I implement this?
Yes I'm doing something wrong in the above code.
Or is there any other way I can accomplish this?


Thanks and Regards
Posted

The ID value of a control with runat="server" is for server side programming only. If you need to get at the client side ID of the container control you'll have to do something like this:
ASP
var buttonIDSelector = "#<%= btn.ClientID %>";
$(document).ready(function () {
    $(buttonIDSelector).click(function (e) {
        e.preventDefault();
        if (confirm("Hello World!!")) {
            $(buttonIDSelector).click();
        }
    });
});

Cheers!

—MRB
 
Share this answer
 
Here I got the solution.

C#
$(document).ready(function () {
    $("#btn").click(function (e) {
        if (!confirm("Hello World!!")) {
        e.preventDefault();
        }
    });
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900