Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all

i am using asp with C# 2010, i want to run confirm box behind the code without press any button for example:

C#
bool shouldConfirm = true;
if (shouldConfirm)
    ShowConfirmBox(); //here should show confirm box, otherwise no need to show confirm box

In case of yes the confirm box should display with yes and no option, yes will do something and no will do something else.

can you help me please


thank you all
Posted
Comments
Pete O'Hanlon 16-Oct-12 5:42am    
What condition do you expect should trigger this confirmation box? A combo box selection? A link click? What?
Medo-I 16-Oct-12 5:57am    
bool value behinde the code

if (isConfirm)
// the Confirmation box should trigger here

other wise should not trigger
Pete O'Hanlon 16-Oct-12 6:01am    
Yes, I'm quite bright you know, so I'm clever enough to figure that bit out. However, what do you want to set isConfirm (or shouldConfirm in your original post)? If what you are trying to do is trigger a confirmation box from the C# side, you are going the wrong way about it - this should be triggered at the client side.
Medo-I 16-Oct-12 6:27am    
sorry my reply was not clear.

when i press the button, i should check bool value, if it is true then the confirm box should display, else the code run something else.

in the message box yes will do something and no will do something else
Pete O'Hanlon 16-Oct-12 6:36am    
Now I'm confused. First you didn't want a button press, and now you do. What am I missing here?

Right, first of all, I wouldn't use OnClientClick to do this, rather I would use jQuery and use the click event to trigger this instead. This means you would have some code that looks like this:
JavaScript
$("#ID_OF_BUTTON").click(function() {
  shouldConfirm = GetMyCondition();
  if (shouldConfirm)
    ShowConfirmBox(); 
});
Obviously, you would need to add the client ID of your button here, but that's the basic flow for setting the click handler.
 
Share this answer
 
Comments
Medo-I 16-Oct-12 7:06am    
sorry i did not try the jQuery before

where should i add this jQuery code
$("#ID_OF_BUTTON").click(function() {
shouldConfirm = GetMyCondition();
if (shouldConfirm)
ShowConfirmBox();
});

*how i can run it in C# to get 'shouldConfirm' value to the jQuery code?
*also how i will run the code of yes or no option in that code?

do you think that difficult to do because i did not try jQuery before?
Pete O'Hanlon 16-Oct-12 7:12am    
Just add it into your JavaScript, it's not that complicated to do (you just need place it in a JavaScript script section). Then all you need to do for your confirm box is something like this var answer = confirm('Some question'); if (answer) { /* Do this * / } else { /* Do that */ }
Medo-I 16-Oct-12 7:20am    
two last question :
how i can pass the bool valuein 'shouldConfirm' from C# to the jQuery function in this line:
shouldConfirm = GetMyCondition();

how i can pass the result of bool value from that function to the bool variable in the c# again

thank you
You can use confirm box in JS like this

var ans = confirm ('Record already exist.Do you want to proceed?');
if(ans==true)
{
}
else
{
}
Secondly, to get the response in code behind, you can store the Yes/No value into a hidden field e.g.

document.getElementById('<%= hiddenField.ClientID %>').value = ans;
 
Share this answer
 
Comments
Medo-I 16-Oct-12 5:47am    
how could i run this js code var ans = confirm ('Record already exist.Do you want to proceed?'); without press button?

because as i know if i want the previous code run i have to use OnClientClick event in the button to run it.

thank you
Pete O'Hanlon 16-Oct-12 6:02am    
That's not what the OP asked. When answering questions, you should always look at what they asked, rather than putting in a random answer.
DialogResult dgResult = MessageBox.Show("Do you want to close the application?", "Heading of confirm box", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);

if (dgResult == DialogResult.No)
{
// do something
}
if (dgResult == DialogResult.Yes
{
// do something
}



Dont forget to add the namespace System.Windows.Forms
 
Share this answer
 
Comments
narutoluffy01 16-Oct-12 6:11am    
i hope this is what u r asking for...!!
Pete O'Hanlon 16-Oct-12 6:32am    
It's ASP.NET, not Windows Forms.

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