Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

How to access the code return confirm('Are you sure to Delete?') results (c# asp.net)

Hi All,

I am using an asp:Button with the CommandName=Delete in the <ItemTemplate> in the <FormView> controls.   The button works and the (JavaScript) prompt appears and deletes the selected record. I am not using the code behind file to populate the button in the template.  I just added it to the .aspx page.  It works and it's great.

The problem is that I want to access the results of the button on the prompt.  I would like to know if the users selected ok or if the user selected cancel.  If the user selected cancel, I don't want to do anything.  If the user selected ok:

I want to create a function to do a calculation and conversion and access a textbox in the page similar to the one iin the code behind file attached but I want to do the reverse and subtract 1).  I am using c#.

Thanks,

J
===========================================================
Delete Button in .aspx page:
===========================================================
<asp:Button ID="btnDelete" runat="Server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?');" Font-Size="10px" Font-Names="tahoma" />

============================================================
code behind file for similar task
============================================================
Insert command code in code behind file from the insert button (with no prompt):

protected void Button_Clicked(object sender, EventArgs e)
    {

        string newRows = RecordCounter.Text;
        int myRows = Int32.Parse(newRows);
        myRows = myRows + 1;
        newRows = myRows.ToString();
        RecordCounter.Text = newRows;
    }

Open in new window

0
Justice75
Asked:
Justice75
  • 3
  • 2
1 Solution
 
krunal_shahCommented:
Hi,

you can do it like this,


in your aspx file,

<asp:Button ID="btnDelete" runat="Server" CommandName="btnDelete" OnCommand="Button1_Command" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?');" Font-Size="10px" Font-Names="tahoma" />


and in your code file,

protected void Button1_Command(object sender, EventArgs e)
 {

        string newRows = RecordCounter.Text;
        int myRows = Int32.Parse(newRows);
        myRows = myRows + 1;
        newRows = myRows.ToString();
        RecordCounter.Text = newRows;
 }

in this case Button1_Command will fire only if user clicks OK

Thanks
Krunal
0
 
Justice75Author Commented:
Thank you so much.. This was straight to the point with no flaws!
0
 
Justice75Author Commented:
This was incredible and fast.. Can you explain to me what we just did tough a bit..

J
0
 
Justice75Author Commented:
This was incredible and fast.. Can you explain to me what we just did though a bit..

J
0
 
krunal_shahCommented:
Hi,

Sorry for the late reply,

In this we just have Called one Javascript method to take the confirmation of the use, if the user clicks on OK then it will return true and then only the OnCommand event will fire.
If the user clicks on cancel then the method will return false and the OnCommand event will not fire.

Thanks
Krunal
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now