-4

I want to open a confirm window (like confirm() in javascript) when a button clicked, and if it says YES, I want to do some c# code. How can I do this?

EDIT: this is my code.

SCRIPT:

<script type = "text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to save data?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>

THE REST OF MY HTML CODE:

<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" CssClass="Table">
<Columns>
    <asp:BoundField DataField="ID" HeaderText="Client ID" />
    <asp:BoundField DataField="Name" HeaderText="שם הלקוח" />
    <asp:BoundField DataField="PropertyLocation" HeaderText="מיקום הנכס" />
    <asp:BoundField DataField="PropertyType" HeaderText="סוג הנכס" />
    <asp:BoundField DataField="PropertyArea" HeaderText="שטח הנכס" />
    <asp:BoundField DataField="WorkType" HeaderText="סוג העבודה" />
    <asp:BoundField DataField="Remarks" HeaderText="שדה חופשי" />
    <asp:BoundField DataField="E-mail" HeaderText="מייל" />
    <asp:BoundField DataField="Phone" HeaderText="טלפון" />
    <asp:BoundField DataField="Password" HeaderText="סיסמא" />
    <asp:BoundField DataField="Status" HeaderText="סטטוס" />

    <asp:TemplateField>
        <ItemTemplate>


                <asp:ImageButton ID="btnDelete" runat="server"  ImageUrl="~/Images/ic_delete.png" OnClick="btnDelete_Click" ToolTip="מחיקה" OnClientClick="Confirm()" />
                <asp:ImageButton ID="btnTransfer" runat="server" ImageUrl="~/Images/ic_transfer.png" OnClick="btnTransfer_Click" ToolTip="העברה למעקב" />

        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView> 

MY C# CODE FROM BEHIND (WHEN THE DELETE BUTTON CLICKED):

protected void btnDelete_Click(object sender, ImageClickEventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
        Response.Redirect("Index.aspx", true);

    //connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
    //connection = new SqlConnection(connectionString);
    //commandString = "SELECT * FROM [Users]";
    //command = new SqlCommand(commandString, connection);
}

I am getting this error when I click one of the options in the confirm window:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

  • what did you try so far? I bet there are examples online. Basically you want a HTML modal dialog with a form which posts back to the server when submitted – ADyson Jan 15 '18 at 15:56
  • you will have to do something besides c# here. – Daniel A. White Jan 15 '18 at 15:56
  • you want it in webform? – Negi Rox Jan 15 '18 at 15:57
  • Possible duplicate of [Is there a builtin confirmation dialog in Windows Forms?](https://stackoverflow.com/questions/3845695/is-there-a-builtin-confirmation-dialog-in-windows-forms) – Anand Jan 15 '18 at 15:58
  • 1
    how will you do it with C# while it will appear on the client browser? – Amr Elgarhy Jan 15 '18 at 15:58
  • @ADyson I tried this example from here: https://www.aspsnippets.com/Articles/Server-Side-Code-Behind-Yes-No-Confirmation-Message-Box-in-ASPNet.aspx but its giving me an error about postback – Ohayon Daniel Jan 15 '18 at 16:00
  • @DanielA.White what can you do? I want to finally go to a c# code and do there all the work – Ohayon Daniel Jan 15 '18 at 16:01
  • 2
    @Anand - ASP.NET and Windows Forms are totally different. – Broots Waymb Jan 15 '18 at 16:01
  • @Anand Can I use MessageBox.Show in asp.net? Will it work also on web development? – Ohayon Daniel Jan 15 '18 at 16:02
  • no it will not work – Negi Rox Jan 15 '18 at 16:02
  • @OhayonDaniel Anand hasn't noticed the question is about web forms not windows forms, you can ignore that suggestion – ADyson Jan 15 '18 at 16:03
  • @AmrElgarhy Do you have any idea how to maybe show the messagebox from javascript and than if he clicks yes move to code behind in c#? – Ohayon Daniel Jan 15 '18 at 16:03
  • Please show what research you have done, and what you have tried before – Horkrine Jan 15 '18 at 16:03
  • @OhayonDaniel if you tried something, show your implementation of the code and show exactly what error your'e getting. Then we can help you fix it. Without that, we can't help you much other than vague comments to a vague question. – ADyson Jan 15 '18 at 16:03
  • @OhayonDaniel you can do that by many ways, may be ajax, submit a form, set a cookie and read it from server side, it is all based on your scenario. – Amr Elgarhy Jan 15 '18 at 16:04
  • @ADyson the thing is that my code also contain gridview which make things pretty hard to notice but I will post it in few minuets – Ohayon Daniel Jan 15 '18 at 16:06
  • just reduce it to the bits necessary for this issue – ADyson Jan 15 '18 at 16:08
  • I have updated the topic with what I tried and my current code – Ohayon Daniel Jan 15 '18 at 16:14
  • the error unfortunately is because you're trying to retrieve a value from a field which wasn't there when the page was created. I suggest you make a hidden field using ` – ADyson Jan 15 '18 at 16:19
  • @ADyson I still keep getting this error no matter what I try, when Im clicking on one of the selections (even on the X) I am getting this error. – Ohayon Daniel Jan 15 '18 at 16:24
  • In that case maybe something else on your page is also causing the same problem, difficult to be sure. As well as trying the suggestion in Horkine's answer did you remove your own code above? – ADyson Jan 15 '18 at 16:33
  • @ADyson I deleted the js code and replaced it, replaced the code in the button and the code behind. But this error keeps bugging me. – Ohayon Daniel Jan 15 '18 at 16:37

1 Answers1

2

First you'll need a hidden field like this:

<input type="hidden" runat="server" id="AreYouSureBox" />

Then add your button:

<asp:Button runat="server" OnClientClick="AreYouSureBox()" OnClick="CSharpMethod" />

JavaScript for a dialog on your page:

function AreYouSureBox() {
    document.getElementById("areYouSure").value = confirm('Are you sure?');
}

Then have this run on your backend in your C# method:

protected void CSharpMethod(object sender, EventArgs E)
{
    var dialogResult = Boolean.Parse(areYouSure.Value);

    // Put your code here
}
Horkrine
  • 1,365
  • 12
  • 24
  • 1
    A slightly better way might be to only post back if the user answer Yes/OK to the confirm. You can do it by returning false from the JS code I think. – ADyson Jan 15 '18 at 16:17
  • @ADyson I have already tried to return false from the method but still getting this error :/ other ideas? – Ohayon Daniel Jan 15 '18 at 16:20