Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a "Yes / No" alert box in C# code behind

I am trying to display a "Yes / No" messagebox from codebehind in C#. I want to call an "AddRecord" procedure if the user clicks "Yes", and do nothing if the user clicks "No".

Ideally, I want to use the code below, but from codebehind:

OnClientClick = "return confirm('Are you sure you want to delete?');"

I search on SO and google, but was not able to find anything helpful.

like image 695
user279521 Avatar asked Dec 08 '22 01:12

user279521


1 Answers

on your Add Record button, just do the following:

    <asp:button ID="AddRecordbutton" runat="server" Text="Add Record"
 onclick="AddRecordButton_Click" onclientclick="return confirm('add record?');" />

In your code behind, just put the add record code in your AddRecordButton_Click event handler. It will only be called if they click Yes on the popup.


Alternatively, you could have your codebehind assign the onclientclick code when the button is initially rendered.

For example:

protected void Page_Load(object sender, EventArgs e) {
  AddRecordButton.OnClientClick = @"return confirm('Add Record?');";
}
like image 184
NotMe Avatar answered Dec 09 '22 15:12

NotMe