0

I have submit button. On its click I have to check a paricular field's value from database and then give a pop up for confirmationa and then depending on yes or no allow/disallow save.How can i do so.. On using confirm JS function it passes the statement and runs the Save procedure anyway.

my button's event

        protected void btnSubmit_Click(object sender, EventArgs e) 
{

                    string ListExam = string.Empty;
                    DataTable dt = BussObjGoalCert.CheckPlannedStatus(custObjGoalCert);
                    if (dt.Rows.Count != 0)
                    {
                        for (int i = 0; i < (dt.Rows.Count); i++)
                        {
                            ListExam = ListExam + (dt.Rows[i]["MultipleExamPlanned"]) + ";";
                        }
                        lblExamMultiple.Text = ListExam;
                        string myScript2 = "confirm('Unselected exam planned under other certification will also be unplanned.Do you wish to continue?');";
                        ClientScript.RegisterStartupScript(this.GetType(), "myScript", "confirm('Unselected exam planned under other certification will also be unplanned.Do you wish to continue?');", true);

                    }
                    Result = BussObjGoalCert.InsertGoalCertification(custObjGoalCert);
}

SO what I am doing is checking some data from backend and then trying to conditionally call the confirm function. It IS CALLED but then Insert statement is also run irresepective of what the person chooses. How can the database check be done within JS function.I need to do it in code behind. and yet allow/disallow complete save.How is this to be accomplished. I am not using OnCLientCLick as suggested in the answers.

Richa
  • 407
  • 1
  • 10
  • 22

4 Answers4

1

Try this

If you have asp button

<asp:Button runat="server" ID="btn" 
OnClientClick="return confirm('Are you surely want to submit (server button) ?');"
Text="Server button" />

If you have submit HTML button

<input type="submit" 
onclick="return confirm('Are you surely want to submit (client button) ?');"
value="Client button" />

UPDATE

If you want to do this

  1. Execute some server side code
  2. Ask user for some confirmation
  3. On user's confirmation execute some more server side code

Then should do following

  1. Create a web service.
  2. Use Jquery or Javascript to execute your server side code through that web service.
  3. Display confirm box on complete(success) event of your web service request.
  4. If user click's on YES perform a server side potback or perform one more web service request to execute the code to be executed on user's confirmation.
yogi
  • 19,175
  • 13
  • 62
  • 92
  • @Richa have a look at this answer, This may give you some clue.http://stackoverflow.com/a/11061343/1432033 – yogi Nov 06 '12 at 09:21
0

I think you are trying onclick event on submit button. try onsubmit="return confirm('Are You sure to submit')" within form element

Amit Garg
  • 3,867
  • 1
  • 27
  • 37
0

There is one solution for your problem.please follow these steps :

1.)On button's OnClientclick event call pagemethod and pass value in it which you want to check in Database.

2.)pagemethod is used to call server side static methods on client side and return result.

3.)Then return bool value from that in true if data exist and false if new.

4.)When flag is true then show confirmation box if confirmation is true then call server method again and update info and then return true to OnClientClick event.

OR

if confirmation is false then return false to OnClientClick event.

You can check my artical HERE.Hope this helps you.If you need more detail then please let me know.

Sunny
  • 3,185
  • 8
  • 34
  • 66
0
try this

 <asp:Button ID="btnsave" runat="server" Text="Save" OnClientClick="javascript:return Checkdelete();"/>


function Checkdelete() {
return confirm("Are you sure you want to Save the records");
}